在我的实验中,我无法找到任何功能上的区别
git reset --hard
Run Code Online (Sandbox Code Playgroud)
和
git reset --merge
Run Code Online (Sandbox Code Playgroud)
使用说明也没有给出任何提示
--hard reset HEAD, index and working tree
--merge reset HEAD, index and working tree
Run Code Online (Sandbox Code Playgroud)
我经常使用该--hard选项,所以了解它是如何工作的.选项--merge和--hard选项之间有什么区别?
干杯,奥利
也许一个例子在这里会有所帮助,让我们使用以下顺序:
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
Run Code Online (Sandbox Code Playgroud)
现在,如果我尝试
git reset --merge 123abc
Run Code Online (Sandbox Code Playgroud)
我明白了
error: Entry 'file_one' not uptodate. Cannot merge.
fatal: Could not reset index file to revision '123abc'
Run Code Online (Sandbox Code Playgroud)
原因是file_one在工作区和索引中都有变化
为了解决这个问题,我做了
git add .
git reset --merge 123abc
Run Code Online (Sandbox Code Playgroud)
这次它有效,但是,我得到了相同的结果git reset --hard.索引为空,工作区为空,file_one为空,与第一次提交后相同.
有人能提出说明差异的步骤吗?
Jak*_*ski 28
--hard Matches the working tree and index to that of the tree being
switched to. Any changes to tracked files in the working tree since
<commit> are lost.
--merge
Resets the index to match the tree recorded by the named commit, and
updates the files that are different between the named commit and
the current commit in the working tree.
这git reset --merge是一个更安全的版本git reset --hard,当你的变化和其他人的变化混合在一起时,试图改变我们的变化.
Von*_*onC 12
文章" Git undo,reset or revert? "总结了不同的用法,当用于ORIG_HEAD:
# Reset the latest successful pull or merge
$ git reset --hard ORIG_HEAD
# Reset the latest pull or merge, into a dirty working tree
$ git reset --merge ORIG_HEAD
Run Code Online (Sandbox Code Playgroud)
正如manojlds的回答所提到的,并通过博客文章说明,后者在您看到如下错误消息时特别有用:
fatal: You have not concluded your merge. (`MERGE_HEAD` exists)
Run Code Online (Sandbox Code Playgroud)
线程" [PATCH]在合并期间拒绝合并 "也详细说明了这一点:
git reset --merge HEAD
Run Code Online (Sandbox Code Playgroud)
它填充了一个完全不同的情况,你在工作树中进行了一些干净的合并,但是然后想要再次丢弃合并,而不会丢失未提交的更改.
如果没有这些变化,你只需要使用--hard,但是在这里你要移动分支尖端,同时将它们合并,类似于git checkout -m移动的功能HEAD.
当您使用工作树中的更改进行拉动时,这会很有用,并且发现合并不符合预期(您可能一直希望提交不会影响您正在处理的文件).此时,如果你这样做git reset --hard ORIG_HEAD,你会吹走所有东西,包括你当地的变化.如果您这样做git reset --merge ORIG_HEAD,您将保留您当地的更改.
显然根据:
http://www.kernel.org/pub/software/scm/git/docs/git-reset.html
--hard - 将工作树和索引与正在切换到的树相匹配。此后对工作树中跟踪文件的任何更改
<commit>都会丢失。--merge - 重置索引以匹配指定提交记录的树,并更新工作树中指定提交和当前提交之间不同的文件。