I am trying to learn how to restore or rollback files and projects to a prior state, and don't understand the difference between git revert
, checkout
, and reset
. Why are there 3 different commands for seemingly the same purpose, and when should someone choose one over the other?
相关问题
- Why does recursive submodule update from github fa
- Extended message for commit via Visual Studio Code
- Emacs shell: save commit message
- Can I organize Git submodules in a flat hierarchy?
- Upload file > 25 MB on Github
相关文章
- 请教Git如何克隆本地库?
- GitHub:Enterprise post-receive hook
- Git Clone Fails: Server Certificate Verification F
- SSIS solution on GIT?
- Is there a version control system abstraction for
- ssh: Could not resolve hostname git: Name or servi
- Cannot commit changes with gitextensions
- git: retry if http request failed
These three commands have entirely different purposes. They are not even remotely similar.
git revert
This command creates a new commit that undoes the changes from a previous commit. This command adds new history to the project (it doesn't modify existing history).
git checkout
This command checks-out content from the repository and puts it in your work tree. It can also have other effects, depending on how the command was invoked. For instance, it can also change which branch you are currently working on. This command doesn't make any changes to the history.
git reset
This command is a little more complicated. It actually does a couple of different things depending on how it is invoked. It modifies the index (the so-called "staging area"). Or it changes which commit a branch head is currently pointing at. This command may alter existing history (by changing the commit that a branch references).
Using these commands
If a commit has been made somewhere in the project's history, and you later decide that the commit is wrong and should not have been done, then
git revert
is the tool for the job. It will undo the changes introduced by the bad commit, recording the "undo" in the history.If you have modified a file in your working tree, but haven't committed the change, then you can use
git checkout
to checkout a fresh-from-repository copy of the file.If you have made a commit, but haven't shared it with anyone else and you decide you don't want it, then you can use
git reset
to rewrite the history so that it looks as though you never made that commit.These are just some of the possible usage scenarios. There are other commands that can be useful in some situations, and the above three commands have other uses as well.
Let's say you had commits:
git revert B
, will create a commit that undoes changes inB
.git revert A
, will create a commit that undoes changes inA
, but will not touch changes inB
Note that if changes in
B
are dependent on changes inA
, the revert ofA
is not possible.git reset --soft A
, will change the commit history and repository; staging and working directory will still be at state ofC
.git reset --mixed A
, will change the commit history, repository, and staging; working directory will still be at state ofC
.git reset --hard A
, will change the commit history, repository, staging and working directory; you will go back to the state ofA
completely.git checkout
modifies your working tree,git reset
modifies which reference the branch you're on points to,git revert
adds a commit undoing changes.Reset - On the commit-level, resetting is a way to move the tip of a branch to a different commit. This can be used to remove commits from the current branch.
Revert - Reverting undoes a commit by creating a new commit. This is a safe way to undo changes, as it has no chance of re-writing the commit history. Contrast this with git reset, which does alter the existing commit history. For this reason, git revert should be used to undo changes on a public branch, and git reset should be reserved for undoing changes on a private branch.
You can have a look on this link- Reset, Checkout and Revert
If you broke the tree but didn't commit the code, you can use
git reset
, and if you just want to restore one file, you can usegit checkout
.If you broke the tree and committed the code, you can use
git revert HEAD
.http://book.git-scm.com/4_undoing_in_git_-_reset,_checkout_and_revert.html
git revert
is used to undo a previous commit. In git, you can't alter or erase an earlier commit. (Actually you can, but it can cause problems.) So instead of editing the earlier commit, revert introduces a new commit that reverses an earlier one.git reset
is used to undo changes in your working directory that haven't been comitted yet.git checkout
is used to copy a file from some other commit to your current working tree. It doesn't automatically commit the file.