解决 git 中“两者都添加”的合并冲突?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9823692/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 13:16:58  来源:igfitidea点击:

Resolving a 'both added' merge conflict in git?

gitmergerebase

提问by Jez

I'm rebasing in git, and one conflict I get is 'both added' - that is, exactly the same filename has been added independently in my branch, and in the branch I'm rebasing on. git statustells me:

我在 git 中变基,我得到的一个冲突是“两者都添加”——也就是说,在我的分支中独立添加了完全相同的文件名,在我正在变基的分支中。 git status告诉我:

# Unmerged paths:
#   (use "git reset HEAD <file>..." to unstage)
#   (use "git add/rm <file>..." as appropriate to mark resolution)
#
#       both added:         src/MyFile.cs

My question is, how do I resolve this? Must I use a merge tool or is there a way I can do it just from the commandline? If I git rm src/MyFile.cs, how does git know which file version I want to remove and which I want to keep?

我的问题是,我该如何解决这个问题?我必须使用合并工具还是有一种方法可以仅从命令行执行此操作?如果我git rm src/MyFile.cs,git 怎么知道我想删除哪个文件版本以及我想保留哪个版本?

回答by CB Bailey

If you use git rmgit will remove all versions of that path from the index so your resolve action will leave you without either version.

如果您使用git rmgit 将从索引中删除该路径的所有版本,因此您的解析操作将使您没有任何一个版本。

You can use git checkout --ours src/MyFile.csto choose the version from the branch onto which you are rebasing or git checkout --theirs src/MyFile.csto choose the version from the branch which you are rebasing.

您可以使用git checkout --ours src/MyFile.cs来从要重新定位的分支git checkout --theirs src/MyFile.cs中选择版本,或者从要重新定位的分支中选择版本。

If you want a blend you need to use a merge tool or edit it manually.

如果您想要混合,则需要使用合并工具或手动编辑。

回答by Anas Alkhatib

I sometimes find it confusing using the --theirsand --oursoptions to identify where the file will come from. Most of the time mine will be in the branch I am rebasing which is referred to by --theirs!

我有时会发现使用--theirs--ours选项来确定文件的来源很混乱。我的大部分时间都在我正在变基的分支中,该分支由--theirs!

You can also use git checkout <tree-ish> -- src/MyFile.cs

你也可以使用 git checkout <tree-ish> -- src/MyFile.cs

Where the <tree-ish>can be replaced either by the branch name or commit-id that contains the file you wish to keep.

<tree-ish>既可以由分公司名称或更换提交-ID,其中包含你想保留的文件。

git checkout 6a363d8 -- src/MyFile.cs

git checkout 6a363d8 -- src/MyFile.cs

git checkout my_branch -- src/MyFile.cs

git checkout my_branch -- src/MyFile.cs

git checkout HEAD -- src/MyFile.cs

git checkout HEAD -- src/MyFile.cs

回答by Tom

When doing ...

当做...

git checkout --ours someFile

git checkout --ours someFile

It may seem like it didn't do anything when doing git status.

在执行 git status 时,它似乎没有做任何事情。

Just Remember to do this afterwards.

请记住之后再执行此操作。

git add someFile
git status