Inscription: Most members of the team use two git tools, sourceTree and github. However, everyone does not know much about the reset, checkout, and revert functions provided by the graphical tool, and even confuses them, and then uses them by guessing. The hard work pays off. After trying many conflict handling or branch development pits, I finally formed my own set of usage methods, which is gratifying. However, the solution to the problem is not the most efficient, and we do not know the internal execution process, which is unacceptable for a self-disciplined procedural artist. Based on this question, translate this blog to provide a reference for the undo operation of advanced users in git. It is encouraged to use graphical tools to improve development efficiency after familiarizing yourself with command line operations.水平有限,释疑为主,翻译为辅
Reset Checkout and Revert
Original address: https://www.atlassian.com/git/tutorials/resetting-checking-out-and-reverting
The git reset, git checkout, and git revert commands are the three most useful git commands. They can help you undo some operations on the repo, and the first two commands can be used both at the commit level and at the file level.
Because they are similar, it is easy to confuse them. In this article, we will compare their similarities and differences.
Before reading this article, you need to understand the three components of git repo, namely working directory
(code repository) staged snapshot
(snapshot: add cache repository) commit history
(commit history), which will help you better understand these three commands.
commit-level operations
The arguments passed to git reset
and git checkout
determine the scope of the command. When the command does not contain a file path, the command acts on the whole commit
.
Reset:
At the commit
level , the git reset
command moves HEAD
to one of the current branches commit
, which can be used to undo some of the current branches commit
.
For example, the following command will make the 'hotfix' branch back two commits
git checkout hotfix
git reset HEAD~2
The two previous commits that were before HEAD are now after HEAD, which means they will be removed as garbage on the next git commit, in other words, both commits will be discarded. As shown below:
git reset is used to undo changes that have not been committed to the remote. In addition to moving the HEAD of the current branch, you can choose to modify the staged snapshot or working directory through different tags
-
--soft
:staged snapshot
andworking directory
have not been changed (it is recommended to enter git status to check the status after the command line is executed) -
--mixed
:staged snapshot
updated,working directory
not changed. [This is the default option] (recommended as above) -
--hard
:staged snapshot
andworking directory
both will fallback.
--hard is very dangerous, it will directly roll back all your previous modifications, you can save the commit id in advance before using it.
[These marks are often HEAD
used with . For example, you git reset --mixed HEAD
can undo all cached changes, but keep them in the working directory. git reset --hard HEAD
Uncommitted changes can be completely removed. 】
checkout
By now, you should be familiar with commit
levels git checkout
. When you send a branch name
name , you will change the current branch.
git checkout hotfix
The above command will switch HEAD to a different branch and update the current one working directory
to match. Because it will overwrite the current local changes, it git
forces you to completely abandon or commit to store the current changes before changing branches. Unlike git reset
, git checkout
no branches or commits will be deprecated.
You can also go checkout
to any one time commit
, by supplying commit Id
as an argument.
For example the following command.
git checkout HEAD~2/[commit id]
This review
is useful for code in a certain version of the repo. However, if a new commit is added again, there is no way to go back to the original state. Therefore, you should always create a new branch before making changes.
Revert
git revert
The command undoes the modifications made by a commit
by one. commit
This method of revocation is safe because it does not modify it . For commitm history
example, the following command will find out the modification of the penultimate commit (that is, the previous commit of the current commit), and create a new commit for revocation. The last commit of the current one commit
.
git checkout hotfix
git revert HEAD~2
As shown below:
File-level operations
git reset
The and git checkout
command can also accept an optional file path as an argument, which limits the operation to a single file.
Reset:
When invoked with a file path, the git reset
command updates staged snapshot
to match a certain time commit
. The following command will rewind the file one by one commit
.
git reset HEAD~1 [file] (deprecated)
[--soft, --mixed, --hard flags do not work at this time, the staged snapshot will be updated, but the working directory will not be updated]
Checkout
git checkout
The command is git reset
similar to , except it updates working directory
, instead ofstaged snapshot
The following command will update working directory
to match a certain timecommit
Summary:
git reset | Commit | Discard commits in a private branch or throw away uncommited changes |
git reset | File | Unstage a file |
git checkot | Commit | switch between branches or inspect old snapshot |
git checkout | File | Discard changes in the working directory |
git revert | commit | Undo commits in a public branch |
git revert | File | not support |
Blog original address: http://blog.mexiqq.com/index.php/archives/3/
From: git reset, git checkout, git revert