解决合并冲突后如何完成合并?
我已阅读 基本分支和合并Git 社区书籍的 部分。
因此,我按照它创建了一个分支:experimental
。
然后我:
- 切换到实验分支(git checkoutexperimental)
- 进行一堆更改
- 提交它(gitcommit-a)
- 切换到master分支(gitcheckoutmaster)
- 进行一些更改并在那里提交
- 切换回实验性(gitcheckoutexperimental)
- 合并master 更改为实验性 (git merge master)
有一些冲突,但在我解决它们之后,我做了“git add myfile”
现在我被卡住了,我无法移回主控
现在我被卡住了,当我这样做时
$ git checkout master
error: Entry 'res/layout/my_item.xml' would be overwritten by merge. Cannot merge.
:
$ git rebase --abort
没有进行中的变基?
我做了:
$ 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.
我该怎么做才能回到我的主分支?
I've read the Basic Branching and Merging section of the Git Community Book.
So I follow it and create one branch: experimental
.
Then I:
- switch to experimental branch (git checkout experimental)
- make a bunch of changes
- commit it (git commit -a)
- switch to master branch (git checkout master)
- make some changes and commit there
- switch back to experimental (git checkout experimental)
- merge master change to experimental (git merge master)
there are some conflicts but after I resolve them, I did 'git add myfile'
And now i am stuck, I can't move back to master
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
当合并过程中出现冲突时,您必须手动完成合并提交。听起来您已经完成了前两个步骤,编辑冲突的文件,然后对它们运行 git add 将它们标记为已解决。最后,您需要使用 git commit 实际提交合并。届时您将能够再次切换分支。
快速提示:您可以使用
git commit -am "your commit message"
同时对跟踪的文件执行添加和提交操作。 (来源:@vaheeds)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 add
on them to mark them as resolved. Finally, you need to actually commit the merge withgit commit
. At that point you will be able to switch branches again.Quick Tip: You can use
git commit -am "your commit message"
to perform add and commit operations on tracked files simultaneously. (Credit: @vaheeds)使用 Git 2.12(2017 年第一季度),您将拥有更自然的命令:
如果您不想在继续/恢复合并时编辑消息:
如果
--no-edit
不起作用,如 akseli 中报道的 注释,您可以执行以下操作:您可以为这些命令定义别名。
含义,正如评论中 mgnobody 所解释的那样:
请参阅 commit c7d227d(2016 年 12 月 15 日),作者:杰夫·金 (
peff
)。请参阅提交042e290,提交 c261a87, 提交 367ff69(2016 年 12 月 14 日)作者:Chris Packham (
cpackham
)。(由 Junio C Hamano --
gitster
-- 合并于 提交 05f6e1b,2016 年 12 月 27 日)请参阅2.12 发行说明。
With Git 2.12 (Q1 2017), you will have the more natural command:
And if you don't want to edit the message when continuing/resuming the merge:
If
--no-edit
does not work, as akseli reported in the comments, you can do:You can define an alias for those commands.
Meaning, as explained by mgnobody in the comments:
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)See 2.12 release notes.
如果您在合并/变基过程中遇到困难,您可以随时
将工作恢复到上次提交的状态。这将丢失工作树中的更改,因此如果您在合并之前进行了本地修改,那么这些修改将在合并之后消失 - 这就是为什么建议在进行本地修改时不要开始合并。 :)
In case you ever get stuck during a merge/rebase you can always
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. :)
手动解决冲突后的下一步是:-
git merge --Continue
、git Cherry-pick --Continue
,git rebase --继续
]The next steps after resolving the conflicts manually are:-
git merge --continue
,git cherry-pick --continue
,git rebase --continue
]只需
git commit
即可。可选地
git abort
它:我遇到了合并冲突。我怎样才能中止合并?
为了让合并变得更轻松,请安装 kdiff3 并将其配置为合并工具。说明: http://doodkin.com/2016/05/29/git-merge-easy-github-this-branch-has-conflicts-that-must-be-resolved-use- the-command-line/
该页面包含以下视频:https://www.youtube .com/watch?v=Cc4xPp7Iuzo
Just
git commit
it.Optionally
git abort
it:I ran into a merge conflict. How can I abort the merge?
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/
That page contains this video: https://www.youtube.com/watch?v=Cc4xPp7Iuzo
每当您使用命令 git mergebranchabranchb 合并两个分支时,有两种可能性:
一个分支(假设brancha)可以通过遵循其提交来由另一个分支(假设branchb)到达在这种情况下,git 只是快进头部以指向最近的分支(在本例中为branchb)。
2.但是如果两个分支在某个较旧的点出现分歧,那么 git 会创建一个新快照并添加一个指向它的新提交。所以万一
您要合并的分支之间不存在冲突,git 会顺利地创建一个新的提交。
现在回到合并分支之间存在合并冲突的有趣情况。我从页面 https:// git-scm.com/book/en/v2/Git-Branching-Basic-Branching-and-Merging
因此,如果存在合并冲突,您需要解决冲突,然后添加您所做的更改使用 git add filename 到暂存区域,然后使用命令 git commit 提交更改,该命令由于冲突而被 git 暂停。我希望这可以解释您的查询。另请访问上面的链接以进行详细了解。如有任何疑问,请在下面发表评论,我很乐意提供帮助。
Whenever You merge two branches using command
git merge brancha branchb
, There are two possibilities: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.
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
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 filename
and then commit the changes by using the commandgit commit
which 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 status
生成冲突列表。当遇到冲突行时,Git 将使用视觉指示器来编辑受影响文件的内容,该指示器标记冲突内容的两侧。
当您修复冲突的文件并准备好合并时,您所要做的就是运行 git add 和 git commit 来生成合并提交。提交完成后,
git Push
将更改推送到分支。参考文章:Git 合并。
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
.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.
When you fix your conflicted files and you are ready to merge, all you have to do is run
git add
andgit commit
to generate the merge commit. Once the commit was made ,git push
the changes to the branch.Reference article: Git merge.
解决冲突的步骤:
分支(BRANCH_NAME_TO_BE_MERGED)
“git merge origin/BRANCH_NAME_TO_BE_MERGED”
现在您可以看到它显示“CONFLICT(内容)”,对于那些具有“ CONFLICT”,查看您的代码并解决它们
(当你要提交时添加一些消息,如果没有,它将打开 vi 或 vim 编辑器,你需要按“esc:q!”然后按“enter”)
7.“git 推送”
Steps to resolve conflict:
branch(BRANCH_NAME_TO_BE_MERGED)
"git merge origin/BRANCH_NAME_TO_BE_MERGED"
Now you can see it is showing "CONFLICT (content)", to those file which is having "CONFLICT", see your code and resolve them
(add some message when you are going to commit, if not then it will open vi or vim editor where you need to press "esc:q!" then press "enter")
7."git push"
我要澄清的第一件事是分支名称只是特定提交的别名。当您进行拉取、推送合并等操作时,提交就是 git 的工作原理。每个提交都有一个唯一的 id。
当您执行 $ git merge 时,实际发生的情况是 git 尝试将当前分支快速转发到引用分支所在的提交(换句话说,两个分支名称都指向同一个提交。)这种情况对于 git 来说是最简单的处理,因为没有新的提交。想象一下主人跳到你的树枝正在冷却的紫莲花上。可以设置 --no-ff 标志,在这种情况下,无论是否存在任何代码冲突,git 都会创建一个新的提交。
如果您尝试合并的两个分支之间存在代码冲突(通常是提交历史记录共享过去共同提交的两个分支),则快进将不起作用。只要冲突文件中的两个分支没有更改同一行,git 仍然可以自动合并文件。在这种情况下,git 会为你合并冲突的文件并自动提交它们。你可以通过执行 $ git diff --cached 来预览 git 的表现。或者您可以将 --no-commit 标志传递给合并命令,这会将修改后的文件保留在您需要添加和提交的索引中。但是您可以 $ git diff 这些文件来查看合并将更改的内容。
第三种情况是当存在 git 无法自动解决的冲突时。在这种情况下,您需要手动合并它们。在我看来,这是最容易通过合并来完成的,例如 araxis merge 或 p4merge (免费)。无论哪种方式,您都必须逐一完成每个文件。如果合并似乎被卡住,请使用 $ git merge --continue 来推动它。 Git 应该告诉您它是否无法继续,如果可以的话为什么不继续。如果你觉得你在某个时候搞砸了合并,你可以执行 $ git merge --abort,任何合并都将撤消,你可以重新开始。完成后,您合并的每个文件都将是需要添加和提交的修改文件。您可以使用 $ git status 验证文件的位置。如果您尚未提交合并的文件。您需要这样做才能完成合并。您必须先完成合并或中止合并,然后才能切换分支。
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.
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.
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.
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 commit”。
“git status”将建议做什么:尚未添加的文件列在底部,一旦全部完成,它会建议在顶部进行提交,其中解释了当前分支的合并状态。
After all files have been added, the next step is a "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 HEAD 没有更新。
这个建议将解决 git reset HEAD 的问题。
It may be late. It is Happen because your git HEAD is not updated.
this commend would solve that
git reset HEAD
.还有另一种选择,我尝试了隐藏并且在没有任何提交的情况下对我来说工作得很好在
解决所有冲突之后,
-> git 存储
-> git stash apply stash@{0}
工作正常,在此之后您也可以切换到另一个分支。
Another option also, I tried stash and worked fine for me without any commit
After you resolve all conflicts,
-> git stash
-> git stash apply stash@{0}
worked fine, you can switch to another branch also after this.