git reset, git checkout, git revert allows you to undo some of your local warehousing changes, and the first two commands can be used for commit or a file.
Action at commit level
Note that git revert can only be used for commits that do not work on files.
Reset
At the commit level, reset moves the top of your branch to another commit. Can be used to delete commits for your current branch. For example, the following command backs up the hotfix branch two times.
git checkout hotfixgit Reset head~2
Hotfix the last two commits are dangling commits, which means that they can be deleted the next time git specifies garbage collection.
This use of reset can be used to undo changes that have not been shared to others.
In addition to moving the current branch, you can change the snapshot of the stage area and modify the working directory.
- There will be no changes to the snapshot and working directory of the--soft stage area.
- The snapshot update for the--mixed stage zone matches the state of the specified commit, and the working directory is not modified. This is the default option.
- --hard the snapshot and working directory of the stage area are updated to the specified commit state
The above three markers are often used in conjunction with head. git reset--mixed HEAD works on the stage area, but does not affect changes to the working directory. If you want to completely throw away no commit changes, you can use git reset--hard HEAD. This is the two most common methods of git reset.
Be very careful when you pass a commit to git reset, because it modifies the history of the current branch. This is very bad when the history has been push.
Checkout
When you pass a branch name to checkout, the branch is switched.
Git checkout Hotfix
The above command moves head to another branch and updates your working directory.
You can also checkout commit. The head will be pointed to the commit you specified. The following command checkout to the current commit's grandfather:
git checkout head~2
This is useful for checking the old version of your project. Since no one branch points to the current head, checkout commit is in the state of a detached head. The new commit added in this state is not visible for many other branches. Therefore, you should create a new branch before adding a commit to detached head.
Revert
Revert to undo a commit by creating a new commit. This is a safe way to undo. The following example revokes the third commit by creating a new commit.
git checkout hotfixgit revert head~2
Git revert should be used to revoke a public branch, and git reset is used to revoke a private branch.
FILE-level operations
git reset and Git checkout can accept a file path as a parameter. When you specify a path to a file, reset and checkout only operate on a single file.
Reset
Git reset updates the snapshot of the stage area to match the version specified in the commit. The following command restores foo.py to the state of the 2nd-to-last commit and puts it into the stage area:
git reset head~2 foo.py
Running git reset HEAD foo.py will remove the foo.py from the stage area. Changes under the working directory are not changed.
--soft,--mixed,--hard tag does not play any role in the git reset file, the stage snapshot will always be updated and the working directory will not be updated.
Checkout
Checkout is somewhat similar to reset, but checkout does not update the stage area, but instead updates the working directory.
The following command updates the contents of the foo.py in the workspace to the content of the third-to-last commit:
git checkout head~2 foo.py
The effect of git checkout HEAD foo.py is to discard foo.py without changes in the stage area. The effect is the same as git reset HEAD--hard, but checkout only works on a single file.
Summarize
Command |
Scope |
Common use case |
git reset |
Commit-level |
Discard a commit of a private branch or discard a modification without a commit |
git reset |
File-level |
Unstage a file |
Git checkout |
Commit-level |
Switch branches or check for an old snapshot |
Git checkout |
File-level |
Discard modifications to the working directory |
git revert |
Commit-level |
Revoke a commit from a public branch |
git revert |
File-level |
Revert no file-level operations |
Reset, checkout and revert