git reset, git checkout, git revert difference (translation)

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

hero

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.

01

 

commit-level operations

The arguments passed to git resetand git checkoutdetermine the scope of the command. When the command does not contain a file path, the command acts on the whole commit.

 

Reset:

At the commitlevel , the git resetcommand moves HEADto 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:

02

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 snapshotand working directoryhave not been changed (it is recommended to enter git status to check the status after the command line is executed)
  • --mixed: staged snapshotupdated, working directorynot changed. [This is the default option] (recommended as above)
  • --hard: staged snapshotand working directoryboth 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.

03

[These marks are often HEADused with . For example, you git reset --mixed HEADcan undo all cached changes, but keep them in the working directory. git reset --hard HEADUncommitted changes can be completely removed.

 

checkout

By now, you should be familiar with commitlevels git checkout. When you send a branch namename , 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 directoryto match. Because it will overwrite the current local changes, it gitforces you to completely abandon or commit to store the current changes before changing branches. Unlike git reset, git checkoutno branches or commits will be deprecated.

04

You can also go checkoutto any one time commit, by supplying commit Idas an argument.

For example the following command.

git checkout HEAD~2/[commit id]

05

This reviewis 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 revertThe command undoes the modifications made by a commitby one. commitThis method of revocation is safe because it does not modify it . For commitm historyexample, 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:

06

 

File-level operations

git resetThe and git checkoutcommand 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 resetcommand updates staged snapshotto match a certain time commit. The following command will rewind the file one by one commit.

git reset HEAD~1 [file] (deprecated)

07

[--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 checkoutThe command is git resetsimilar to , except it updates working directory, instead ofstaged snapshot

The following command will update working directoryto match a certain timecommit

git checkout HEAD~1 [文件]
08

Summary:

commands scope common user cases
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

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326689437&siteId=291194637