git reset --hard 和 git reset --merge 之间有什么区别
在我的实验中,我无法发现
git reset --hard
和
git reset --merge
之间的任何功能差异。使用说明没有给出任何提示,
--hard reset HEAD, index and working tree
--merge reset HEAD, index and working tree
我经常使用 --hard
选项,因此了解其工作原理。 --merge
和 --hard
选项有什么区别?
干杯, Olly
也许一个例子会有所帮助,让我们使用以下序列:
cd git_repo
touch file_one
git add file_one
git commit -m "commit one" # sha1 of 123abc
echo "one" >> ./file_one
git commit -a -m "commit two" # sha1 of 234bcd
echo "two" >> ./file_one
git add . # populate index with a change
echo "three" >> ./file_one # populate working area with a change
现在,如果我尝试
git reset --merge 123abc
,我得到的
error: Entry 'file_one' not uptodate. Cannot merge.
fatal: Could not reset index file to revision '123abc'
原因是 file_one 在工作区域和索引中都有更改
为了解决这个问题,我这样做了
git add .
git reset --merge 123abc
这次它有效,但是,我得到了与 git reset --hard 的结果相同。索引为空,工作区为空,file_one 为空,就像第一次提交后一样。
有人可以想出说明差异的步骤吗?
In my experiments I haven't been able to find any functional difference between
git reset --hard
and
git reset --merge
The usage instructions don't give any hint either
--hard reset HEAD, index and working tree
--merge reset HEAD, index and working tree
I regularly use the --hard
option so understand how that works. What's the difference between the --merge
and the --hard
options?
Cheers,
Olly
Perhaps an example would help here, let's use the following sequence:
cd git_repo
touch file_one
git add file_one
git commit -m "commit one" # sha1 of 123abc
echo "one" >> ./file_one
git commit -a -m "commit two" # sha1 of 234bcd
echo "two" >> ./file_one
git add . # populate index with a change
echo "three" >> ./file_one # populate working area with a change
Now if I try
git reset --merge 123abc
I get
error: Entry 'file_one' not uptodate. Cannot merge.
fatal: Could not reset index file to revision '123abc'
the reason being that file_one has changes in both the working area and the index
To remedy this I do
git add .
git reset --merge 123abc
This time it works, however, I get the same result as git reset --hard
. The index is empty, working area is empty, file_one is empty, as it was after first commit.
Can someone come up with the steps that illustrate the difference?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
来自 git Reset 手册页:
git reset --merge
是旨在成为 git reset --hard 的更安全版本,当您的更改和其他人的更改混合在一起时,试图携带我们的更改。From git reset manpage:
The
git reset --merge
is meant to be a safer version ofgit reset --hard
, when your changes and somebody else changes are mixed together, trying to carry our changes around.文章“Git 撤消、重置或恢复?”总结了与
ORIG_HEAD
一起使用时的不同用法:如 manojlds的答案,并由 博客文章,当您看到如下错误消息时,后者特别有用:
线程“[PATCH] 在合并期间拒绝合并" 也详细说明了这一点:
The article "Git undo, reset or revert?" summarizes the different usages, when used with
ORIG_HEAD
:As mentioned by manojlds's answer, and illustrated by the blog post, the latter is especially useful when you see an error message like:
The thread "[PATCH] refuse to merge during a merge" also details that point:
tl;dr
git reset --merge
是用于撤消合并的git reset --keep
。git reset --keep
是git reset --hard
尝试保留未暂存的更改。此外, git reset --hard 可能会更改未暂存的文件(我认为您通常想了解的有关 git reset --merge 的内容已由 @manojlds 的 @manojlds(合并之前工作树中的更改;冲突或干净合并)。因为这看起来像 git reset --merge 的用例。
但您可能还需要考虑以下两种情况:
如果在合并之前工作树没有发生任何更改,并且您正在解决冲突,
git reset --merge ORIG_HEAD< /code> 与 git reset --hard ORIG_HEAD 没有太大区别。除非您此时开始开发新功能(进行与解决冲突无关的更改)。也就是说,如果您想要撤消合并,您很可能想要撤消所有内容。在这种情况下,您只需执行 git merge --abort 即可。
但在这种情况下, git reset --merge ORIG_HEAD 会保留未暂存的更改。对有冲突的文件进行的更改(丢弃)和对暂存文件进行的更改(中止)除外。
尽管从理论上讲它可能很方便,但听起来不像
git reset --merge
的用例。如果您没有在解决冲突,并且想要将 git reset --merge 到任意提交,它将尝试保留未暂存的更改。但 git reset --keep 也会如此。后者在合并过程中根本不起作用,并且会忽略分阶段的更改。
如果无法保留未暂存的更改,它们将中止。如果您更改的文件在
HEAD
和
之间也发生过更改,就会发生这种情况。tl;dr
git reset --merge
isgit reset --keep
for undoing a merge.git reset --keep
isgit reset --hard
that tries to preserve unstaged changes. Also,git reset --hard
may change unstaged files (git-reset):I think what you generally want to know about
git reset --merge
is covered by by @manojlds (changes in the working tree before a merge; a conflict or a clean merge). Because that looks like the use case forgit reset --merge
.But you may also want to consider the following 2 cases:
If you had no changes in the working tree before a merge, and you're in the middle of resolving a conflict,
git reset --merge ORIG_HEAD
is not much different fromgit reset --hard ORIG_HEAD
. Unless you start working on a new feature at this point (make changes unrelated to resolving a conflict). That is, if you want to undo the merge, you most likely want to undo everything. And in this case you can simply dogit merge --abort
.But
git reset --merge ORIG_HEAD
will preserve unstaged changes in this case. Except for changes made to files with conflicts (discard), and changes made to staged files (abort).Although theoretically it may be handy, it doesn't sound like a use case for
git reset --merge
.If you're not in the middle of resolving a conflict, and want to
git reset --merge
to an arbitrary commit, it will try to preserve unstaged changes. But so willgit reset --keep
. The latter simply doesn't work in the middle of a merge, and ignores staged changes.They will abort if they can't preserve unstaged changes. That will happen if you changed a file that was also changed between
HEAD
and<commit>
.显然根据:
http://www.kernel。 org/pub/software/scm/git/docs/git-reset.html
Apparently according to:
http://www.kernel.org/pub/software/scm/git/docs/git-reset.html
当您对工作树中的更改进行拉取并发现合并未按预期进行时(您可能期望提交不会影响您正在处理的文件),这会很有帮助。此时,如果您执行 git reset --hard ORIG_HEAD ,则会清除所有内容,包括本地更改。如果您执行
git reset --merge ORIG_HEAD
,您将保留本地更改。This is helpful when you do a pull with changes in working tree, and find that the merge is not as expected ( you might have been expecting that the commits would not affect the files you were working on ). At this point, if you do
git reset --hard ORIG_HEAD
, you blow away everything, including your local changes. If you dogit reset --merge ORIG_HEAD
, you will keep your local changes.