Git Revert、Checkout 和 Reset 之间有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8358035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 12:25:24  来源:igfitidea点击:

What's the difference between Git Revert, Checkout and Reset?

gitgit-checkoutgit-resetgit-revert

提问by haziz

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?

我想学习如何回滚文件和项目或恢复到以前的状态,不明白之间的差别git revertcheckoutreset。为什么有 3 种不同的命令用于看似相同的目的,什么时候应该选择一个而不是另一个?

回答by Dan Moulding

These three commands have entirely different purposes. They are not even remotely similar.

这三个命令的目的完全不同。他们甚至远非相似。

git revert

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

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

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 revertis the tool for the job. It will undo the changes introduced by the bad commit, recording the "undo" in the history.

如果在项目历史中的某个地方进行了提交,并且您后来认为提交是错误的并且不应该完成,那么git revert就是该工作的工具。它将撤消由错误提交引入的更改,在历史记录中记录“撤消”。

If you have modified a file in your working tree, but haven't committed the change, then you can use git checkoutto checkout a fresh-from-repository copy of the file.

如果您修改了工作树中的文件,但尚未提交更改,则可以使用git checkout检出该文件的新存储库副本。

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 resetto rewrite the history so that it looks as though you never made that commit.

如果您进行了提交,但尚未与其他任何人共享,并且您决定不想要它,那么您可以使用git reset重写历史记录,使其看起来好像您从未进行过该提交。

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.

这些只是一些可能的使用场景。在某些情况下还有其他命令可能有用,并且上述三个命令还有其他用途。

回答by Akavall

Let's say you had commits:

假设你有提交:

C
B
A

git revert B, will create a commit that undoes changes in B.

git revert B, 将创建一个撤销B.

git revert A, will create a commit that undoes changes in A, but will not touch changes in B

git revert A, 将创建一个撤销 中更改的提交A,但不会触及 中的更改B

Note that if changes in Bare dependent on changes in A, the revert of Ais not possible.

请注意,如果 中的更改B取决于 中的更改AA则无法还原。

git reset --soft A, will change the commit history and repository; staging and working directory will still be at state of C.

git reset --soft A, 将更改提交历史和存储库;暂存目录和工作目录仍将处于C.

git reset --mixed A, will change the commit history, repository, and staging; working directory will still be at state of C.

git reset --mixed A, 将更改提交历史记录、存储库和暂存;工作目录仍将处于C.

git reset --hard A, will change the commit history, repository, staging and working directory; you will go back to the state of Acompletely.

git reset --hard A, 将更改提交历史、存储库、暂存和工作目录;你会回到A完全的状态。

回答by Jonathan

  • git revertis 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 resetis used to undo changes in your working directory that haven't been comitted yet.
  • git checkoutis used to copy a file from some other commit to your current working tree. It doesn't automatically commit the file.
  • git revert用于撤消先前的提交。在 git 中,您不能更改或删除较早的提交。(实际上您可以,但它可能会导致问题。)因此,revert 不是编辑较早的提交,而是引入了一个新的提交来反转较早的提交。
  • git reset用于撤消工作目录中尚未提交的更改。
  • git checkout用于将文件从其他提交复制到当前工作树。它不会自动提交文件。

回答by dan_waterworth

  • git checkoutmodifies your working tree,
  • git resetmodifies which reference the branch you're on points to,
  • git revertadds a commit undoing changes.
  • git checkout修改你的工作树,
  • git reset修改您所指向的分支的引用,
  • git revert添加提交撤消更改。

回答by Sachchidanand Singh

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.

还原 -还原通过创建新提交来撤消提交。这是撤消更改的安全方法,因为它没有机会重写提交历史。将此与 git reset 进行对比,后者确实会更改现有的提交历史记录。出于这个原因, git revert 应该用于撤消公共分支上的更改,而 git reset 应该保留用于撤消私有分支上的更改。

You can have a look on this link- Reset, Checkout and Revert

您可以查看此链接- 重置、结帐和还原

回答by LostMohican

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.

如果你破坏了树但没有提交代码,你可以使用git reset,如果你只想恢复一个文件,你可以使用git checkout.

If you broke the tree and committed the code, you can use git revert HEAD.

如果您破坏了树并提交了代码,则可以使用git revert HEAD.

http://book.git-scm.com/4_undoing_in_git_-_reset,_checkout_and_revert.html

http://book.git-scm.com/4_undoing_in_git_-_reset,_checkout_and_revert.html