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?
This question is related to
git
git-checkout
git-reset
git-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 use git 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 checkout
modifies your working tree,git reset
modifies which reference the branch you're on points to,git revert
adds a commit undoing changes.Let's say you had commits:
C
B
A
git revert B
, will create a commit that undoes changes in B
.
git revert A
, will create a commit that undoes changes in A
, but will not touch changes in B
Note that if changes in B
are dependent on changes in A
, the revert of A
is not possible.
git reset --soft A
, will change the commit history and repository; staging and working directory will still be at state of C
.
git reset --mixed A
, will change the commit history, repository, and staging; working directory will still be at state of C
.
git reset --hard A
, will change the commit history, repository, staging and working directory; you will go back to the state of A
completely.
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
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.Source: Stackoverflow.com