git 解决合并冲突后如何完成合并?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2474097/
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 08:05:08  来源:igfitidea点击:

How do I finish the merge after resolving my merge conflicts?

gitgit-merge

提问by michael

I've read the Basic Branching and Mergingsection of the Git Community Book.

我已经阅读了Git Community Book的Basic Branching and Merging部分。

So I follow it and create one branch: experimental.

所以我遵循它并创建一个分支:experimental.

Then I:

然后我:

  1. switch to experimental branch (git checkout experimental)
  2. make a bunch of changes
  3. commit it (git commit -a)
  4. switch to master branch (git checkout master)
  5. make some changes and commit there
  6. switch back to experimental (git checkout experimental)
  7. merge master change to experimental (git merge master)
  8. there are some conflicts but after I resolve them, I did 'git add myfile'

  9. And now i am stuck, I can't move back to master

  1. 切换到实验分支(git checkout 实验)
  2. 做一些改变
  3. 提交它(git commit -a)
  4. 切换到 master 分支(git checkout master)
  5. 进行一些更改并在那里提交
  6. 切换回实验(git checkout 实验)
  7. 将 master 更改为实验性 (git merge master)
  8. 有一些冲突,但在我解决它们之后,我做了 'git add myfile'

  9. 现在我卡住了,我无法回到主人身边

when I do

当我做

 $ git checkout master
error: Entry 'res/layout/my_item.xml' would be overwritten by merge. Cannot merge.

and I did:

我做到了:

$ git rebase --abort

No rebase in progress?

没有进行变基?

and I did :

我做了:

$  git add res/layout/socialhub_list_item.xml
$ git checkout master
error: Entry 'res/layout/my_item.xml' would be overwritten by merge. Cannot merge.

What can I do so that I can go back to my master branch?

我该怎么做才能回到我的主分支?

采纳答案by Jimmy Cuadra

When there is a conflict during a merge, you have to finish the merge commit manually. It sounds like you've done the first two steps, to edit the files that conflicted and then run git addon them to mark them as resolved. Finally, you need to actually commit the merge with git commit. At that point you will be able to switch branches again.

当合并过程中出现冲突时,您必须手动完成合并提交。听起来您已经完成了前两个步骤,编辑冲突的文件,然后运行git add它们以将它们标记为已解决。最后,您需要实际提交与git commit. 届时,您将能够再次切换分支。

回答by VonC

How do I finish the merge after resolving my merge conflicts?

解决合并冲突后如何完成合并?

With Git 2.12 (Q1 2017), you will have the more natural command:

使用 Git 2.12(2017 年第一季度),您将拥有更自然的命令:

git merge --continue

See commit c7d227d(15 Dec 2016) by Jeff King (peff).
See commit 042e290, commit c261a87, commit 367ff69(14 Dec 2016) by Chris Packham (cpackham).
(Merged by Junio C Hamano -- gitster--in commit 05f6e1b, 27 Dec 2016)

请参阅Jeff King ( )提交的 c7d227d(2016 年 12 月 15 日。 请参阅Chris Packham ( ) 的commit 042e290commit c261a87commit 36​​7ff69(2016 年 12 月 14 日)(由Junio C Hamano合并-- --提交 05f6e1b,2016 年 12 月 27 日)peff
cpackham
gitster

See 2.12 release notes.

请参阅2.12 发行说明

merge: add '--continue' option as a synonym for 'git commit'

Teach 'git merge' the --continueoption which allows 'continuing' a merge by completing it.
The traditional way of completing a merge after resolving conflicts is to use 'git commit'.
Now with commands like 'git rebase' and 'git cherry-pick' having a '--continue' option adding such an option to 'git merge' presents a consistent UI.

merge:添加“ --continue”选项作为的代名词“ git commit

教“ git merge--continue通过完成合并来“继续”合并的选项。
解决冲突后完成合并的传统方法是使用“ git commit”。
现在,像“ git rebase”和“ git cherry-pick”这样的命令有一个“ --continue”选项,将这样的选项添加到“ git merge”会呈现一致的用户界面。

回答by Bombe

In case you ever get stuck during a merge/rebase you can always

如果您在合并/变基期间卡住,您可以随时

git reset --hard

to restore your working to the state of the last commit. This will lose your changes from the working tree so if you had local modifications before the merge they will be gone after this—which is why it's advisable to not start a merge when you have local modifications. :)

将您的工作恢复到上次提交的状态。这将从工作树中丢失您的更改,因此如果您在合并之前进行了本地修改,它们将在此之后消失 - 这就是为什么当您进行本地修改时建议不要开始合并。:)

回答by Sourav Prem

Whenever You merge two branches using command git merge brancha branchb, There are two possibilities:

每当您使用 command 合并两个分支时git merge brancha branchb,有两种可能性:

  1. One branch (lets say brancha) can be reached by the other branch (lets say branchb) by following its commits history.In this case git simply fast-forward the head to point to the recent branch (in this case branchb).

    2.But if the two branches have diverged at some older point then git creates a new snapshot and add a new commit that points to it. So in case there is no conflict between the branches you are merging, git smoothly creates a new commit.

  1. 一个分支(比如 brancha)可以通过跟踪其提交历史被另一个分支(比如 branchb)访问。在这种情况下,git 简单地将 head 快进指向最近的分支(在这种情况下是 branchb)。

    2.但是如果两个分支在某个较旧的点出现分歧,那么 git 会创建一个新快照并添加一个指向它的新提交。因此,如果您正在合并的分支之间没有冲突,git 会顺利地创建一个新的提交。

Run git logto see the commit after you have merged two non-conflicting branches.

git log合并两个不冲突的分支后运行以查看提交。

Now coming back to the interesting case when there are merge conflicts between the merging branches. I quote this from the page https://git-scm.com/book/en/v2/Git-Branching-Basic-Branching-and-Merging

现在回到合并分支之间存在合并冲突的有趣案例。我从页面https://git-scm.com/book/en/v2/Git-Branching-Basic-Branching-and-Merging引用了这个

Git hasn't automatically created a new merge commit. It has paused the process while you resolve the conflict. If you want to see which files are unmerged at any point after a merge conflict, you can run git status

Git 没有自动创建一个新的合并提交。在您解决冲突时,它已暂停该过程。如果您想在合并冲突后的任何时候查看哪些文件未合并,您可以运行git status



So in case there are merge conflicts, you need to resolve the conflict then add the changes you have made to the staging area using git add filenameand then commit the changes by using the command git commitwhich was paused by git because of the conflict.I hope this explains your query. Also do visit the link above for a detailed understanding. In case of any query please comment below , I'll be happy to help.

因此,如果存在合并冲突,您需要解决冲突,然后将您所做的更改添加到临时区域git add filename,然后使用git commit由于冲突而被 git 暂停的命令提交更改。我希望这能解释您的询问。也请访问上面的链接以进行详细了解。如有任何疑问,请在下方评论,我将很乐意为您提供帮助。

回答by Shimon Doodkin

Just git commitit.

git commit这样。

Optionally git abortit:
I ran into a merge conflict. How can I abort the merge?

可选git abort
我遇到了合并冲突。如何中止合并?

To make life easier with on merges install kdiff3 and configure it as a mergetool. Instructions: http://doodkin.com/2016/05/29/git-merge-easy-github-this-branch-has-conflicts-that-must-be-resolved-use-the-command-line/

为了让合并的生活更轻松,请安装 kdiff3 并将其配置为合并工具。说明:http: //doodkin.com/2016/05/29/git-merge-easy-github-this-branch-has-conflicts-that-must-be-resolved-use-the-command-line/

That page contains this video: https://www.youtube.com/watch?v=Cc4xPp7Iuzo

该页面包含此视频:https: //www.youtube.com/watch?v=Cc4xPp7Iuzo

回答by Timidfriendly

The next steps after resolving the conflicts manually are:-

手动解决冲突后的下一步是:-

  1. git add .
  2. git status (this will show you which commands are necessary to continue automatic merge procedure)
  3. [command git suggests, e.g. git merge --continue, git cherry-pick --continue, git rebase --continue]
  1. git 添加。
  2. git status (这将显示哪些命令是继续自动合并过程所必需的)
  3. [命令 git 建议,例如git merge --continue, git cherry-pick --continue, git rebase --continue]

回答by Nesha Zoric

A merge conflict occurs when two branches you're trying to merge both changed the same part of the same file. You can generate a list of conflicts with git status.

当您尝试合并的两个分支都更改了同一文件的同一部分时,就会发生合并冲突。您可以使用 生成冲突列表git status

When the conflicted line is encountered, Git will edit the content of the affected files with visual indicators that mark both sides of the conflicting content.

当遇到冲突行时,Git 将编辑受影响文件的内容,并使用标记冲突内容两侧的视觉指示器。

<<<<<<< HEAD
conflicted text from HEAD
=======
conflicted text from merging_branch
>>>>>>> merging_branch

When you fix your conflicted files and you are ready to merge, all you have to do is run git addand git committo generate the merge commit. Once the commit was made ,git pushthe changes to the branch.

当您修复冲突文件并准备好合并时,您所要做的就是运行git addgit commit生成合并提交。提交后,git push对分支的更改。

Reference article: Git merge.

参考文章:Git 合并

回答by hobgoblin

After all files have been added, the next step is a "git commit".

添加完所有文件后,下一步是“ git commit”。

"git status" will suggest what to do: files yet to add are listed at the bottom, and once they are all done, it will suggest a commit at the top, where it explains the merge status of the current branch.

“git status”会建议做什么:尚未添加的文件列在底部,一旦它们全部完成,它会在顶部建议提交,在那里解释当前分支的合并状态。

回答by Harry Robbins

The first thing I want to make clear is that branch names are just an alias to a specific commit. a commit is what git works off, when you pull, push merge and so forth. Each commit has a unique id.

我要澄清的第一件事是分支名称只是特定提交的别名。提交是 git 的作用,当你拉、推合并等等。每个提交都有一个唯一的 ID。

When you do $ git merge , what is actually happening is git tries to fast forward your current branch to to the commit the referenced branch is on (in other words both branch names point to the same commit.) This scenario is the easiest for git to deal, since there's no new commit. Think of master jumping onto the lilipad your branch is chilling on. It's possible to set the --no-ff flag, in which case git will create a new commit regardless of whether there were any code conflicts.

当您执行 $ git merge 时,实际发生的是 git 尝试将您当前的分支快进到引用分支所在的提交(换句话说,两个分支名称都指向同一个提交。)这种情况对 git 来说是最简单的处理,因为没有新的提交。想想主人跳上你的树枝令人不寒而栗的lilipad。可以设置 --no-ff 标志,在这种情况下,无论是否存在任何代码冲突,git 都会创建一个新的提交。

In a situation where there are code conflicts between the two branches you are trying to merge (usually two branches whose commit history share a common commit in the past), the fast forward won't work. git may still be able to automatically merge the files, so long as the same line wasn't changed by both branches in a conflicting file. in this case, git will merge the conflicting files for you AND automatically commit them. You can preview how git did by doing $ git diff --cached. Or you can pass the --no-commit flag to the merge command, which will leave modified files in your index you'll need to add and commit. But you can $ git diff these files to review what the merge will change.

在您尝试合并的两个分支之间存在代码冲突的情况下(通常两个分支的提交历史在过去共享一个共同的提交),快进将不起作用。git 可能仍然能够自动合并文件,只要冲突文件中的两个分支都没有更改同一行。在这种情况下,git 将为您合并冲突文件并自动提交它们。您可以通过执行 $ git diff --cached 来预览 git 的表现。或者您可以将 --no-commit 标志传递给合并命令,这会将修改后的文件保留在您需要添加和提交的索引中。但是您可以 $ git diff 这些文件来查看合并将更改的内容。

The third scenario is when there are conflicts git can't automatically resolve. In this case you'll need to manually merge them. In my opinion this is easiest to do with a merge took, like araxis merge or p4merge (free). Either way, you have to do each file one by one. If the merge ever seems to be stuck, use $ git merge --continue, to nudge it along. Git should tell you if it can't continue, and if so why not. If you feel you loused up the merge at some point, you can do $ git merge --abort, and any merging will undo and you can start over. When you're done, each file you merged will be a modified file that needs to be added and committed. You can verify where the files are with $ git status. If you haven't committed the merged files yet. You need to do that to complete the merge. You have to complete the merge or abort the merge before you can switch branches.

第三种情况是当存在 git 无法自动解决的冲突时。在这种情况下,您需要手动合并它们。在我看来,这是最简单的合并方式,例如 araxis merge 或 p4merge(免费)。无论哪种方式,您都必须一个一个地执行每个文件。如果合并似乎卡住了,请使用 $ git merge --continue 来推动它。Git 应该告诉你它是否不能继续,如果是,为什么不。如果你觉得你在某个时候搞砸了合并,你可以执行 $ git merge --abort,任何合并都会撤消,你可以重新开始。完成后,您合并的每个文件都将是一个需要添加和提交的修改文件。您可以使用 $ git status 验证文件的位置。如果您还没有提交合并的文件。您需要这样做才能完成合并。

回答by s.morteza malekabadi

It may be late. It is Happen because your git HEAD is not updated. this commend would solve that git reset HEAD.

可能会迟到。这是因为您的 git HEAD 未更新。这个推荐可以解决这个问题git reset HEAD