我一直认为 git reset 和 git checkout 是相同的,从某种意义上说,两者都将项目带回到特定的提交。但是,我觉得它们不可能完全相同,因为那是多余的。两者之间的实际区别是什么?我有点困惑,因为 svn 只有 svn co 来恢复提交。
ADDED
VonC 和 Charles 很好地解释了 git reset 和 git checkout 之间的区别。我目前的理解是 git reset 将所有更改恢复到特定提交,而 git checkout 或多或少为分支做准备。我发现以下两个图表对于理解这一点非常有用:
添加了 3 个
来自 http ://think-like-a-git.net/sections/rebase-from-the-ground-up/using-git-cherry-pick-to-simulate-git-rebase.html,签出并重置可以模拟rebase。
git checkout bar
git reset --hard newbar
git branch -d newbar
I've always thought of git reset
and git checkout
as the same, in the sense that both bring the project back to a specific commit. However, I feel they can't be exactly the same, as that would be redundant. What is the actual difference between the two? I'm a bit confused, as the svn only has svn co
to revert the commit.
ADDED
VonC and Charles explained the differences between git reset
and git checkout
really well. My current understanding is that git reset
reverts all of the changes back to a specific commit, whereas git checkout
more or less prepares for a branch. I found the following two diagrams quite useful in coming to this understanding:
ADDED 3
From http://think-like-a-git.net/sections/rebase-from-the-ground-up/using-git-cherry-pick-to-simulate-git-rebase.html, checkout and reset can emulate the rebase.
git checkout bar
git reset --hard newbar
git branch -d newbar
发布评论
评论(8)
git reset
专门用于更新索引< /strong>,移动头部。git checkout
是关于更新工作树< /strong> (到索引或指定的树)。仅当您检出分支时,它才会更新 HEAD(如果没有,您最终会得到一个分离的 HEAD)。(实际上,对于 Git 2.23 Q3 2019,这将是
git Restore
,不一定是git checkout
)相比之下,由于 svn 没有索引,只有工作树,
svn checkout
会将给定的修订版本复制到单独的目录中。与 git checkout 更接近的等效项是:
svn update
(如果您在同一个分支,表示相同的 SVN URL)svn switch
(如果您签出同一分支,但来自另一个 SVN 存储库 URL )所有这三个工作树修改(
svn checkout
、update
、switch
)在 git 中只有一个命令:git checkout.
但由于 git 也有索引的概念(存储库和工作树之间的“暂存区域”),因此您还可以使用 git Reset。
Thinkeye 提到在评论中文章“重置揭秘”。
在这些点上,不过:
LarsH< /a> 在评论:
De Novo 同意在评论中:
git reset
is specifically about updating the index, moving the HEAD.git checkout
is about updating the working tree (to the index or the specified tree). It will update the HEAD only if you checkout a branch (if not, you end up with a detached HEAD).(actually, with Git 2.23 Q3 2019, this will be
git restore
, not necessarilygit checkout
)By comparison, since svn has no index, only a working tree,
svn checkout
will copy a given revision on a separate directory.The closer equivalent for
git checkout
would:svn update
(if you are in the same branch, meaning the same SVN URL)svn switch
(if you checkout for instance the same branch, but from another SVN repo URL)All those three working tree modifications (
svn checkout
,update
,switch
) have only one command in git:git checkout
.But since git has also the notion of index (that "staging area" between the repo and the working tree), you also have
git reset
.Thinkeye mentions in the comments the article "Reset Demystified ".
On those points, though:
LarsH adds in the comments:
De Novo concurs in the comments:
在最简单的形式中,
reset
重置索引而不触及工作树,而checkout
更改工作树而不触及索引。重置索引以匹配
HEAD
,单独保留工作树:从概念上讲,这会将索引签入工作树。要让它真正执行任何操作,您必须使用 -f 来强制它覆盖任何本地更改。这是一项安全功能,可确保“无参数”形式不会具有破坏性:
一旦开始添加参数,确实会存在一些重叠。
checkout
通常与分支、标签或提交一起使用。在这种情况下,它将重置HEAD
和给定提交的索引,并将索引签出到工作树中。另外,如果您为
reset
提供--hard
,您可以要求reset
覆盖工作树并重置索引。如果您当前签出了一个分支,那么当您提供替代分支或提交时,重置和签出之间存在重大差异。
reset
将更改当前分支以指向选定的提交,而checkout
将保留当前分支,但会签出提供的分支或提交。其他形式的重置和提交涉及提供路径。
如果您提供
reset
路径,则无法提供--hard
并且reset
只会将所提供路径的索引版本更改为提供的提交(如果未指定提交,则为HEAD
)。如果您提供
checkout
的路径,例如reset
,它将更新所提供路径的索引版本以匹配所提供的提交(或HEAD
),但是它总是将所提供路径的索引版本签入工作树。In their simplest form,
reset
resets the index without touching the working tree, whilecheckout
changes the working tree without touching the index.Resets the index to match
HEAD
, working tree left alone:Conceptually, this checks out the index into the working tree. To get it to actually do anything you would have to use
-f
to force it to overwrite any local changes. This is a safety feature to make sure that the "no argument" form isn't destructive:Once you start adding parameters it is true that there is some overlap.
checkout
is usually used with a branch, tag or commit. In this case it will resetHEAD
and the index to the given commit as well as performing the checkout of the index into the working tree.Also, if you supply
--hard
toreset
you can askreset
to overwrite the working tree as well as resetting the index.If you current have a branch checked out out there is a crucial different between
reset
andcheckout
when you supply an alternative branch or commit.reset
will change the current branch to point at the selected commit whereascheckout
will leave the current branch alone but will checkout the supplied branch or commit instead.Other forms of
reset
andcommit
involve supplying paths.If you supply paths to
reset
you cannot supply--hard
andreset
will only change the index version of the supplied paths to the version in the supplied commit (orHEAD
if you don't specify a commit).If you supply paths to
checkout
, likereset
it will update the index version of the supplied paths to match the supplied commit (orHEAD
) but it will always checkout the index version of the supplied paths into the working tree.恢复更改时的一个简单用例:
1. 如果您想撤消已修改文件的暂存,请使用重置。
2. 如果您想放弃对未暂存文件的更改,请使用 checkout。
One simple use case when reverting change:
1. Use reset if you want to undo staging of a modified file.
2. Use checkout if you want to discard changes to unstaged file/s.
简而言之,关键区别在于
reset
移动当前分支引用,而checkout
则不会(它移动 HEAD)。正如 Pro Git 书籍 Reset Demystified 中所解释的那样,
另请参阅 VonC 对同一篇文章中非常有用的文本和图表摘录的回答,我不会在此重复。
当然,还有更多关于
checkout
和reset
对索引和工作树产生什么影响的细节,具体取决于使用的参数。这两个命令之间可能有很多相似之处和不同之处。但在我看来,最关键的区别是它们是否移动当前分支的尖端。The key difference in a nutshell is that
reset
moves the current branch reference, whilecheckout
does not (it moves HEAD).As the Pro Git book explains under Reset Demystified,
See also VonC's answer for a very helpful text and diagram excerpt from the same article, which I won't duplicate here.
Of course there are a lot more details about what effects
checkout
andreset
can have on the index and the working tree, depending on what parameters are used. There can be lots of similarities and differences between the two commands. But as I see it, the most crucial difference is whether they move the tip of the current branch.简短的助记符:
brief mnemonics:
这两个命令(重置和检出)完全不同。
checkout X
不是reset --hard X
如果 X 是分支名称,
checkout X
将更改当前分支而
reset --hard X
则不会。The two commands (reset and checkout) are completely different.
checkout X
IS NOTreset --hard X
If X is a branch name,
checkout X
will change the current branchwhile
reset --hard X
will not.git重置
->从暂存区域中删除所有文件,即撤消git add
git reset
->撤消提交并暂存指定提交后的所有文件。如果使用重置命令添加
--hard
,则它会删除暂存区域中的文件以及删除 从您的目录中获取。git checkout
->您将恢复到指定的提交状态,但您不在任何分支中。如果你输入
gitbranch-a
,你会看到你在。
根据控制台:
您将获得两个选项:
git switch -c
->它基本上是撤消结帐命令,将您带回旧分支。git reset
-> removes all the files from the staged area i.e it undosgit add <files>
git reset <commit_ID>
-> undos commits and staging of all files after the specified commit.if added
--hard
with the reset command, then it removes the files from the staged area as well as deletes it from your directory.git checkout <commit_ID>
-> you are reverted back to that specified commit state but you are not in any branch.if you type
git branch -a
, you will see that you are in.
as per the console:
and you will be given two options:
git switch -c <new-branch-name>
: a new branch will be created from that commit and it will become your current branch.git switch -c
-> it is basically undoing the checkout command which brings you back to your old branch.这是对歧义的澄清:
示例:
git reset 也移动 HEAD,但是,有两个区别< /强>:
它也会移动指向当前分支尖端的提交的指针。例如,假设指向当前分支的指针名为“main”,然后执行 git-reset,现在,主指针将指向另一个提交,并且 HEAD 也将指向该提交(基本上, HEAD 通过指向主指针间接指向该提交,它仍然是一个附加头(!),但在这里没有任何区别)。 p>
Git-reset 不一定使暂存区域和工作目录保持在执行重置之前的相同状态。如您所知,重置分为三种类型:软重置、混合重置(默认)和硬重置:
示例:
Here's a clarification of the ambiguity:
Examples:
git reset also moves the HEAD, however again, with two differences:
It moves the pointer that points to the commit at the tip of the current branch too. For instance, let's say the pointer to the current branch is named "main", then you perform a git-reset, now, the main pointer will point to another commit, and the HEAD will point to that commit as well(well basically, HEAD points to that commit indirectly through pointing to the main pointer, it is still an attached head(!), but it doesn't make any difference here).
Git-reset doesn't necessarily leave the staging area and the working directory on the same state they were in before the reset was performed. As you know, there are three types of reset: soft, mixed(default) and hard:
Examples: