git reset --hard和git reset --merge有什么区别? [英] What's the difference between git reset --hard and git reset --merge

查看:3303
本文介绍了git reset --hard和git reset --merge有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的实验中,我一直无法找到任何功能差异。

  git reset --hard 

  git reset --merge 

使用说明不提供任何提示

   - 硬重置HEAD,索引和工作树
--merge重置HEAD,索引和工作树

我经常使用 - hard 选项,所以了解它的工作原理。 - merge - hard 选项有什么区别?



干杯,
奥利



也许一个例子在这里会有所帮助,我们使用以下顺序:

  cd git_repo 
touch file_one
git add file_one
git commit -m提交一个#sha1 123abc
echoone>> ./file_one
git commit -a -mcommit two#sha1 234bcd
echotwo>> ./file_one
git add。 #填充索引并更改
echothree>> ./file_one#使用变更填充工作区域

现在,如果我尝试

  git reset --merge 123abc 

我得到

 错误:条目'file_one'不是最新的。无法合并。 
fatal:无法将索引文件重置为修订版'123abc'

原因是file_one在工作领域和指数上都有变化。为了弥补这一点,我做了

pre $ $ $
$ b $ p code> git add。
git reset --merge 123abc

然而,这次它可以工作,结果为 git reset --hard 。索引是空的,工作区是空的,file_one是空的,因为它是在第一次提交之后。



有人可以想出说明差异的步骤吗? > git reset manpage :

 
--hard将工作树和索引匹配到树的
切换到。自
< commit>开始,在工作树中跟踪文件的任何更改迷路了。

--merge
重置索引以匹配指定提交记录的树,
更新指定提交和
之间不同的文件在工作树中提交。

git reset --merge 是一个更安全的 git reset --hard ,当您的更改和其他人的更改混合在一起时,尝试执行我们的更改。


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?

解决方案

From git reset manpage:

--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.

The git reset --merge is meant to be a safer version of git reset --hard, when your changes and somebody else changes are mixed together, trying to carry our changes around.

这篇关于git reset --hard和git reset --merge有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆