Home >Backend Development >PHP Tutorial >Summary of commonly used commands for git operations

Summary of commonly used commands for git operations

零下一度
零下一度Original
2017-07-17 15:11:171467browse

Commonly used commands Summary

一 1. Return to a certain node and pass the following command:

– Modifications such as:

Git reset –hard HASH returns to a certain node and retains the modifications.

2. All local modifications. Anything that has not been submitted will be returned to its original state.

Git checkout .

Git checkout repository means switching the repository, such as: git checkout dev switches to the dev repository

Git checkout file address means canceling the modification of the file, such as :git checkout backend/controller/site

3. View the commit log

Git log

4. View the branch

Git branch without parameters: column Exit the local branch. There will be an * sign in front of the current branch. 5. Dealing with the LF problem As follows:

Configure global variables git config --global core.autocrlf false

View global variables git config –global –l to view global variables

mkdir learngit //Create A folder

cd learngit //Switch the current directory

pwd //Display the full path of the current directory

git init //Initialize the directory

ls - ah //Display all files in the current directory, including hidden files

cd.>readme.txt //Create an empty file

git add readme.txt //Add the file to the git repository

git commit -m “wrote a readme file” //Submit the file and add the change description

git status //View the change status of the current warehouse file

git diff / / is a comparison between the work area (work dict) and the temporary storage area (stage)

git diff --cached // is a comparison between the temporary storage area (stage) and the branch (master)

After modifying the file content, git status will prompt use "git add" and/or "git commit -a". Please add first and then commit. You cannot commit directly.

git log //View git submission record, including time and Submitter and other detailed information

git log --pretty=oneline //Only view the version number and submission description

git reset --hard HEAD^ //Roll back the previous version

git reset --hard HEAD^^ //Rollback to the previous version

git reset --hard HEAD~100 //Rollback to the previous 100 versions

git reset - -soft HEAD //Do not reset the cache area and workspace when rolling back

git reset --mixed HEAD //Reset the cache area when rolling back, default option

git reset -- hard HEAD //Reset the cache area and work area when rolling back

git reset //No HEAD specified, used to clear the cache area modifications

git reset filename //Clear the cache area specified File modification

git reset --hard //Do not specify HEAD, used to clear the workspace and cache area.

git reset --hard filename //Clear the workspace and cache area Modification of the specified file

cat readme.txt //View file content

If you want to withdraw after rolling back, there are two ways

1) You need to retrieve the latest version number without closing the original terminal window, enter the first few digits, for example

git reset --hard ec6980a

2) Close the terminal window and reopen it , enter git reflog, view each record of the operation, retrieve the version number and roll back

git checkout -- file //Undo the workspace operation, there are two situations, 1、Not Before adding to the cache area, the local workspace modifications are undone. 2. After the cache area has been added, the cached modifications are undone and the cached version is restored.

git checkout -- -- in the file command is very important , without --, it becomes a "switch to another branch" command

git checkout branch //Switch branch, reset the cache area and work area at the same time, if the work area has been modified but not submitted, it is necessary Commit or stash first

git checkout branch --force //Switch branch, reset cache area and work area at the same time

git checkout --force //Not specify branch, used to clear work Modification of the area (the cache area remains unchanged, if there was an add before, the work area is consistent with the cache area)

git reset HEAD fileName //You can undo the modifications to the temporary storage area ( unstage)

rm test.txt //Delete the file on the file manager, please note that the local deletion must correspond to the warehouse

git rm text.txt //Delete the remote Warehouse file, then submit git commit

git checkout -- test.txt //Suppose the local rm deletes the file by mistake, you can use the command to copy the latest copy from the warehouse to the local, and replace the workspace version with the version in the repository, regardless of whether the workspace is modified or Deleted, you can "restore it with one click"

ssh-keygen -t rsa -C "liwenxin@foreveross.com" //Set the locally associated account information, press Enter all the way, no Set the password directly to blank.

open ~/.ssh //mac opens ssh in the home directory

cd ~/.ssh //If you accidentally entered the password in the previous steps, you can reset it to empty by following the following methods Password

ssh-keygen -p -f id_rsa //Enter the old password once and the new password twice as required

git remote add origin git@github.com:gz -jam/learngit.git //Replace with your own GitHub account name and associate the remote library locally

git remote rm origin //If the association is wrong or needs to be rebind

git remote add origin git@github.com:michaelliao/learngit.git //You can rebind

git push -u origin master //Push all the contents of the local library to the remote library, enter Confirm yes and push the current branch master to the remote. Since the remote library is empty, when we pushed the master branch for the first time, we added the -u parameter. Git will not only push the contents of the local master branch to the remote new master branch , it will also associate the local master branch with the remote master branch, which can simplify the commands when pushing or pulling in the future.

git push origin master //You don’t need to -u

git clone git@github.com:michaelliao/gitskills.git //Clone the remote warehouse ,Git supports multiple protocols. The default git:// uses ssh, but other protocols such as https can also be used. For example https://github.com/gz-jam/gitskills.git

git checkout -b dev //Create a branch and switch the current branch to dev, which is equivalent to executing two Instructions, such as git branch dev and git checkout dev

git branch //View all branches of the current project, the * in front represents the currently effective branch

git merge dev //For merge specification Branch to the current branch

git branch -d dev //After merging, you can consider deleting redundant project branches

git branch -D dev //The branch submission file has not been merged. If deleted, it will prompt that it has not yet been merged. , do you want to force delete? Note the capital D

When both branches have submission content in the same file, Git cannot perform "quick merge". At this time, you need to execute git status first. Git uses < <<<<<<, ======, >>>>>>> Mark the contents of different branches, and re-git add the file after adjustment. Then git commit submits the file to resolve the conflict

git log --graph --pretty=oneline --abbrev-commit //You can see the merge status of the branch

git log --graph //This command can also see the branch merge diagram

git merge --no-ff -m "merge with no-ff" dev //Disable Fast forward mode, Git will Generate a new commit during merge

git stash //The current branch work is not completed but you don’t want to submit it to the warehouse. You can save it with instructions first to ensure that switching to other branches will not cause code loss.

git stash list //View the stash content. There are two recovery methods. One is to use git stash apply stash@{0} to recover. However, after recovery, the stash content is not deleted. You need to use git stash. drop to delete; another way is to use git stash pop, which deletes the stash content while restoring

git remote //View the information of the remote library

git remote -v //Display more detailed information

git push origin master //Pushing a branch means pushing all local submissions on the branch to the remote library. When pushing, you must specify the local branch

If the newly created local branch is not pushed to the remote, it will not be visible to others.

To push the branch locally, use git push origin branch-name

git checkout -b dev origin /dev //Create the dev branch of the remote origin to the local, the default branch master

git push //When a conflict occurs, first git pull the code

git pull //If it fails, it will prompt "no tracking information". The reason may be that the link between the local dev branch and the remote origin/dev branch is not specified

git branch --set-upstream-to=origin/dev dev //Set the link between dev and origin/dev

There is a conflict in the merge and needs to be resolved manually. The solution is as mentioned above, git status, then manually repair, then git add and git commit, and finally git push

git tag v1.0 //The commit number is too long to remember, you can tag it on the branch

git tag v0.9 6224937 //On the branch it is The specified commit numbers are tagged

. The tags are not listed in chronological order, but in alphabetical order. You can use git show to view tag information

For example git show v0.9

git tag -a v0.1 -m "version 0.1 released" 3628164 //Create with description For tags, use -a to specify the tag name and -m to specify the description text

git tag -d v0.1 //If the tag is mistyped, you can also delete it

The created tags are only stored locally and will not be automatically pushed to the remote. If you want to push a tag to the remote, use the command git push origin

For example: git push origin v1.0

Or, push all the locals that have not been pushed to the remote at once Tag

For example: git push origin --tags

If the tag has been pushed to the remote, it is a little more troublesome to delete the remote tag. First delete it locally, git tag -d v0.9; then , delete from remote. The delete command is also push, but the format is as follows git push origin :refs/tags/v0.9 or git push origin --delete tag v0.9

The above is the detailed content of Summary of commonly used commands for git operations. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn