为什么我在执行git revert时会发生冲突? [英] Why do I get conflicts when I do git revert?

查看:1348
本文介绍了为什么我在执行git revert时会发生冲突?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Git,我知道:

I use Git, and I know that:

git revert <hash-code>

用于创建一个新的提交,该提交与哈希码中的先前提交相同.

is used to create a new commit that will be identical to the past commit in the hash-code.

例如,我有以下提交:

1f74a0e second commit
e72d8b8 first commit  

我想还原第一次提交,所以我使用了:

I wanted to revert the first commit, so I used:

git revert 1f74a0e

仍然,出现以下错误:

错误:无法还原1f74a0e ...第一个提交提示:解决后 冲突时,标记更正的路径提示:使用'git add' 或'git rm'提示:并使用'git commit'提交结果

error: could not revert 1f74a0e... first commit hint: after resolving the conflicts, mark the corrected paths hint: with 'git add ' or 'git rm ' hint: and commit the result with 'git commit'

对于冲突,我输入:

$ git diff --name-only --diff-filter=U
file.txt

当我打开file.txt时,看不到任何冲突迹象.

When I open file.txt I see no signs for conflicts.

当然会有冲突.我希望git接受首次提交"并将其复制到第二个提交的顶部.我该怎么办?

Of course there will be conflicts. I expect git to take the "first commit" and copy it on top of the second commit. How can I do it?

推荐答案

实际上不是还原的作用.还原不会将您带回"该提交,并假装后续的提交没有发生.它对单个提交进行逻辑否定-和单独提交-将后续提交保留在原处.

That's actually not what revert does. Revert doesn't "take you back to" that commit and pretend that subsequent commits didn't happen. It applies a logical negation of a single commit - and that commit alone - leaving subsequent commits in place.

假设您对某个文件进行了一些初始提交-为了简单起见,将其称为提交#1-文件看起来像这样:

Let's say you have some initial commit of some file - let's call it commit #1 for simplicity - and the file looks like this:

One
Two
Three
Four

现在,假设您的提交#2更改了一行:

Now let's say you have a commit #2 that changes one line:

One
2
Three
Four

最后,提交#3来更改另一行:

And finally, commit #3 that changes a different line:

One
2
Three
4

如果您尝试还原提交#2,它将仅撤消该提交中更改的行,并保留在提交#3中引入的更改,因此结果将是:

If you try to revert commit #2, it will undo only the line changed in that commit, and leave the changes introduced in commit #3, so the result will be:

One
Two
Three
4

现在,如果以后的提交更改了与您要还原的提交相同的行,那么您将发生冲突.例如,假设您的提交#4也更改了第二行:

Now, if there was a subsequent commit that had changed the same line as the commit that you're trying to revert, then you'll have a conflict. For example, let's say you have a commit #4 that also changed the second line:

One
TWO
THREE
4

现在,如果您的HEAD是4号提交,而您尝试还原2号提交,则将发生冲突. Revert希望返回第二行-撤消在提交#2中所做的更改.因此,它希望第二行当前为2,然后将其恢复为上一次提交时的状态,并将其设置为Two.

Now if your HEAD is commit #4 and you try to revert commit #2, you will have a conflict. Revert expects to take second line back - to undo the changes made in commit #2. So it expects the second line to currently be 2, and it will then revert it to what it was in the previous commit, setting it to Two.

但是,由于第4次提交也更改了该期望,因此该期望无效.所以你有冲突.

However, that expectation was invalidated, since commit #4 had also changed it. So you have a conflict.

如果您的目标根本不是还原,而是回到提交#1并忽略 all 自那时以来发生的更改,那么您想要reset而不是revert.

If your goal isn't to revert at all, but to get back to commit #1 and ignore all the changes that have gone on since then, then you want to reset instead of revert.

git reset --hard 1 

这篇关于为什么我在执行git revert时会发生冲突?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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