简体   繁体   English

如何解决与git merge冲突而无需提交不需要的文件?

[英]How to resolve conflicts with git merge without committing unwanted files?

I have a problem resolving a conflict with git merge. 我在解决与git merge冲突时遇到问题。 When I tried the command git merge branch_1 a conflict with a file appeared. 当我尝试使用命令git merge branch_1 ,出现了与文件的冲突。 I was able to succesfully resolve it using git mergetool with Meld. 我能够使用带有Meld的git mergetool成功解决它。 The problem is, after I have done it, I tried again to merge but it says that I have to commit my changes to conclude the merge. 问题是,完成此操作后,我再次尝试合并,但它表示我必须提交更改以结束合并。 So before doing that I checked the git status and I found that there is a bunch of files in the staging area that I haven't modified. 因此,在执行此操作之前,我检查了git status ,发现在暂存区域中有一堆文件尚未修改。 I guess those files where brought by the failed merge, but now how am I supposed to finish the merge without committing those files? 我猜这些文件是由失败的合并带来的,但是现在我应该如何在不提交这些文件的情况下完成合并呢? Thanks! 谢谢!

I have a problem resolving a conflict with git merge. 我在解决与git merge冲突时遇到问题。 When I tried the command git merge branch_1 a conflict with a file appeared. 当我尝试使用命令git merge branch_1 ,出现了与文件的冲突。

This is normal enough. 这很正常。 Remember that git merge branch_1 means: 记住git merge branch_1意思是:

  1. Identify the current commit (the tip of the current branch). 标识当前提交(当前分支的尖端)。 I call this L for Left or Local or --ours (I know l does not appear in ours ...). 我称其L为Left或Local或--ours (我知道l不会出现在ours ...中)。
  2. Identify one other commit (the tip of branch_1 ). 标识另一个提交( branch_1的尖端)。 I call this R for Right or Remote or otheR or --theirs . 我将此R称为Right或Remote或--theirs--theirs
  3. Find, automatically, a third commit. 自动查找第三次提交。 This third commit is the merge base and it's the point where the two branches first come back together: 第三次提交是合并的基础 ,这是两个分支首先重新结合的点:

      o--L <-- current_branch (HEAD) / ...--o--o--B \\ o--R <-- branch_1 

    Commit B here is the merge base commit. 提交B是合并基础提交。

  4. Make two diffs. 做两个差异。 One finds out what we changed since the merge base: 找出自合并基础以来我们所做的更改:

     git diff --find-renames BL # what we did 

    and the other finds out what they changed since the merge base: 另发现了他们 ,因为合并基础改变:

     git diff --find-renames BR # what they did 
  5. Combine the two change-sets found with the two git diff commands. 将找到的两个变更集与两个git diff命令合并 Take one copy of each change to each file. 将每个更改复制一份到每个文件。 Build up, for committing, a new set of files that are like what was in B , except that each line of each file that we changed in B-vs-L is changed, and each line of each file that they changed in B-vs-R is also changed. 建立,对于犯,一组新的是喜欢什么是B,除了每个文件的每一行的文件,我们在B-VS-L变化改变了,他们在B-更改的每个文件的每一行vs-R也改变了。

  6. If none of these changes conflict, commit the result. 如果这些更改都没有冲突,请提交结果。 Otherwise, stop and complain about the conflicting changes. 否则,请停止并抱怨发生冲突的更改。 Leave, ready to be committed, the non-conflicting files. 保留不冲突的文件,准备提交。 Leave the conflicted files in conflicted state, ready for manual merging or merging using some sort of merge tool. 使冲突文件处于冲突状态,准备进行手动合并或使用某种合并工具进行合并。

I was able to succesfully resolve it using git mergetool with Meld. 我能够使用带有Meld的git mergetool成功解决它。

What git mergetool does is find the files left unmerged in step 6. In your case there was just one such file. git mergetool作用是找到在步骤6中未合并的文件。在您的情况下,只有一个这样的文件。 Then git mergetool runs the tool on each such file, and if the tool claims that you have successfully resolved the conflict, git mergetool will add the file to the set of files ready to commit, thus resolving the conflict. 然后git mergetool在每个这样的文件上运行该工具,如果该工具声称您已成功解决了冲突,则git mergetool会将文件添加到准备提交的文件集中,从而解决了冲突。

[Git] says that I have to commit my changes to conclude the merge. [Git]说,我必须提交更改以完成合并。 So before doing that I checked the git status and I found that there is a bunch of files in the staging area that I haven't modified. 因此,在执行此操作之前,我检查了git状态,发现在暂存区域中有一堆文件尚未修改。 I guess those files where brought by the failed merge, 我想那些合并失败带来的文件,

Those files were, in fact, successfully merged . 这些文件实际上已成功合并 (Well, at least Git thinks so. You might want to verify that what Git thinks is good, is really good, because Git is not very smart. :-) ) Note that git diff --cached will compare what's in the index, ready to be commited, vs what's in the current tip of the current branch. (好吧,至少Git这么认为。您可能想验证Git认为是好的,真的很好,因为Git不太聪明。:-))请注意git diff --cached将比较索引中的内容,准备好提交,而不是当前分支当前提示中的内容。

but now how am I supposed to finish the merge without committing those files? 但是现在我应该如何完成合并而不提交那些文件?

Normally you would want to include those staged, ready-to-commit changes in the files you're committing: they came from the B-vs-R changeset, and normally you should keep them. 通常,您要在提交的文件中包括那些已暂存的,准备提交的更改:它们来自B-vs-R更改集,通常应保留它们。

If you really shouldn't keep them, note that you can do anything you like to what's in the work-tree, and then git add the modified files to copy them into the index/staging-area so that whatever else you have done is also (or instead) included. 如果您真的不应该保留它们,请注意,您可以对工作树中的内容执行任何您喜欢的操作,然后git add修改后的文件以将它们复制到索引/临时区域中,这样您所做的其他任何事情都可以 (或代替)包括在内。 You can even extract the version of any file that appears in any other commit, including the tip of your current branch. 您甚至可以提取出现在任何其他提交中的任何文件的版本,包括当前分支的尖端。

Whatever you put into the index/staging-area will be in the next commit. 无论您输入索引/暂存区什么内容 ,都将在下一次提交中。 That's the definition of the index/staging-area. 那就是索引/临时区域的定义。 (Note that it doesn't hold changes , it holds files —copies of specific files. It's the act of comparing, with git diff , that shows you changes. Running git status is really running git diff .) (请注意,它不保存更改 ,它保存文件 -特定文件的副本。这是与git diff ,它向您显示更改。运行git status实际上是在运行git diff 。)

Use git commit to finish the merge. 使用git commit完成合并。

When you hit a merge conflict, git stages everything what it was able to merge. 当您遇到合并冲突时,git会暂存所有能够合并的内容。 Files with merge conflicts are NOT staged and must be handled manually. 具有合并冲突的文件不会暂存,必须手动处理。 git status will tell you what files are affected. git status会告诉您哪些文件受影响。

You can use git mergetool - this will usually stage your (hopefully) resolved changes on exit. 您可以使用git mergetool通常会在退出时分阶段(希望)解决您的更改。 You usually get a *.orig file for each resolved conflict file. 通常,每个已解决的冲突文件都会获得一个* .orig文件。 This one just tells you what the original file looked like. 这只是告诉您原始文件的外观。 Obviously, you don't want to commit these files... 显然,您不想提交这些文件...

Alternative, if you don't like the mergetool and want to fix it by hand, you can just edit them in your preferred editor. 或者,如果您不喜欢mergetool并想手动修复它,则可以在首选编辑器中对其进行编辑。 Conflicts, resolved via your editor, must be flagged as resolved via git add path/to/conflicting/file , to make git aware you have resolved the conflict. 通过编辑器解决的冲突必须通过git add path/to/conflicting/file标记为已解决,以使git知道您已解决冲突。

Finally, run git commit for finishing the merge. 最后,运行git commit以完成合并。

Git doesn't support committing your conflict resolutions in a separate commit, because those changes don't make sense except within a merge commit. Git不支持在单独的提交中提交冲突解决方案,因为这些更改只有在合并提交中才有意义。 It wouldn't make sense to commit it on top of one of the two branches that are being merged, because the resolutions change code that were from the other branch, and it wouldn't make sense to commit it on top of the merge commit, because the merge commit would contain conflicts, which would make it an invalid commit. 将其提交到正在合并的两个分支之一上是没有意义的,因为解析会更改来自另一个分支的代码,并且将其提交到合并提交之上是没有意义的。 ,因为合并提交将包含冲突,这将使其成为无效提交。

You should just run git merge --continue (or, equivalently, git commit ) to create a single merge commit containing the modifications to the files from both branches plus the modifications you did while resolving conflicts. 您应该只运行git merge --continue (或等效地, git commit )来创建一个合并提交,其中包含两个分支对文件的修改以及您在解决冲突时所做的修改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM