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

发布于 2024-08-26 06:36:15 字数 979 浏览 6 评论 0原文

我已阅读 基本分支和合并Git 社区书籍的 部分。

因此,我按照它创建了一个分支:experimental

然后我:

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

  9. 现在我被卡住了,我无法移回主控

现在我被卡住了,当我这样做时

 $ 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:

  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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

天赋异禀 2024-09-02 06:36:15

当合并过程中出现冲突时,您必须手动完成合并提交。听起来您已经完成了前两个步骤,编辑冲突的文件,然后对它们运行 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 with git 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)

-柠檬树下少年和吉他 2024-09-02 06:36:15

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

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

git merge --continue

如果您不想在继续/恢复合并时编辑消息:

git merge --continue --no-edit

如果 --no-edit 不起作用,如 akseli 中报道的 注释,您可以执行以下操作:

# Linux
GIT_EDITOR=true git merge --continue

# Windows
cmd /V /C "set "GIT_EDITOR=true" && git merge --continue"

您可以为这些命令定义别名。

含义,正如评论中 mgnobody 所解释的那样:

如果运行合并并出现冲突,则只需在解决冲突后执行 git add 即可。
不要在 git add 之后执行 git commit。您的 git merge --continue 将为您完成此操作。

所以,简而言之:

git 合并开发 
# 地址冲突 
git add <冲突的文件> 
git 合并——继续;

请参阅 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 发行说明

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

教导“git merge--continue选项,该选项允许“继续”一个
通过完成来合并。
解决冲突后完成合并的传统方法是使用“git commit”。
现在,像“git rebase”和“gitcherry-pick”这样的命令有一个“--continue”选项,将这样的选项添加到“ git merge' 提供了一致的 UI。

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 merge --continue

And if you don't want to edit the message when continuing/resuming the merge:

git merge --continue --no-edit

If --no-edit does not work, as akseli reported in the comments, you can do:

# Linux
GIT_EDITOR=true git merge --continue

# Windows
cmd /V /C "set "GIT_EDITOR=true" && git merge --continue"

You can define an alias for those commands.

Meaning, as explained by mgnobody in the comments:

if you run merge and get conflicts, you only need to do git add after resolving conflicts.
DO NOT do git commit after git add. Your git merge --continue will do that for you.

So, in short:

git merge develop 
# address conflicts 
git add <conflicted files> 
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)

See 2.12 release notes.

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

Teach 'git merge' the --continue option 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.

我的痛♀有谁懂 2024-09-02 06:36:15

如果您在合并/变基过程中遇到困难,您可以随时

git reset --hard

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

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. :)

时光是把杀猪刀 2024-09-02 06:36:15

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

  1. git add 。
  2. git status(这将向您显示继续自动合并过程所需的命令)
  3. [git 建议的命令,例如 git merge --Continuegit Cherry-pick --Continuegit rebase --继续]

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]
貪欢 2024-09-02 06:36:15

只需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

狂之美人 2024-09-02 06:36:15

每当您使用命令 git mergebranchabranchb 合并两个分支时,有两种可能性:

  1. 一个分支(假设brancha)可以通过遵循其提交来由另一个分支(假设branchb)到达在这种情况下,git 只是快进头部以指向最近的分支(在本例中为branchb)。

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

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

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

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


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

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

  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.

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

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

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


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 command git 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.

还给你自由 2024-09-02 06:36:15

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

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

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

当您修复冲突的文件并准备好合并时,您所要做的就是运行 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.

<<<<<<< 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 add and git commit to generate the merge commit. Once the commit was made ,git push the changes to the branch.

Reference article: Git merge.

秋日私语 2024-09-02 06:36:15

解决冲突的步骤:

  1. 首先“签出”到您想要从其他分支合并的分支
    分支(BRANCH_NAME_TO_BE_MERGED)

"git checkout "MAIN_BRANCH"
  1. 然后使用命令将其与“MAIN_BRANCH”合并:

git merge origin/BRANCH_NAME_TO_BE_MERGED


Auto-merging src/file1.py
CONFLICT (content): Merge conflict in src/file1.py
Auto-merging src/services/docker/filexyz.py
Auto-merging src/cache.py
Auto-merging src/props.py
CONFLICT (content): Merge conflict in src/props.py
Auto-merging src/app.py
CONFLICT (content): Merge conflict in src/app.py
Auto-merging file3
CONFLICT (content): Merge conflict in file3
Automatic merge failed; fix conflicts and then commit the result.

现在您可以看到它显示“CONFLICT(内容)”,对于那些具有“ CONFLICT”,查看您的代码并解决它们

  1. 运行“git status” =>它将显示您需要添加哪些文件(已解决):

 Unmerged paths:
      (use "git add <file>..." to mark resolution)

        both modified:   file3
        both modified:   src/app.py
        both modified:   src/props.py
        both modified:   src/utils/file1.py
  1. 逐个添加每个文件

git add file3
git add src/app.py
git add src/props.py
git add src/utils/file1.py
  1. 解决所有冲突后,使用下面的 git 命令“git commit”
    (当你要提交时添加一些消息,如果没有,它将打开 vi 或 vim 编辑器,你需要按“esc:q!”然后按“enter”)
  2. 再次运行“git status”

 On branch MAIN_BRANCH
  Your branch is ahead of 'origin/MAIN_BRANCH' by 10 commits.
  (use "git push" to publish your local commits)

7.“git 推送”

Steps to resolve conflict:

  1. First 'checkout' to your branch in which you want to merge from the other
    branch(BRANCH_NAME_TO_BE_MERGED)

"git checkout "MAIN_BRANCH"
  1. Then merge it with "MAIN_BRANCH" by using command:

"git merge origin/BRANCH_NAME_TO_BE_MERGED"


Auto-merging src/file1.py
CONFLICT (content): Merge conflict in src/file1.py
Auto-merging src/services/docker/filexyz.py
Auto-merging src/cache.py
Auto-merging src/props.py
CONFLICT (content): Merge conflict in src/props.py
Auto-merging src/app.py
CONFLICT (content): Merge conflict in src/app.py
Auto-merging file3
CONFLICT (content): Merge conflict in file3
Automatic merge failed; fix conflicts and then commit the result.

Now you can see it is showing "CONFLICT (content)", to those file which is having "CONFLICT", see your code and resolve them

  1. run "git status" => It will show you what are the files that you need to add (which you have resolved):

 Unmerged paths:
      (use "git add <file>..." to mark resolution)

        both modified:   file3
        both modified:   src/app.py
        both modified:   src/props.py
        both modified:   src/utils/file1.py
  1. Once you have resolved all conflict, add each file one by one by using below git command

git add file3
git add src/app.py
git add src/props.py
git add src/utils/file1.py
  1. "git commit"
    (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")
  2. run again "git status"

 On branch MAIN_BRANCH
  Your branch is ahead of 'origin/MAIN_BRANCH' by 10 commits.
  (use "git push" to publish your local commits)

7."git push"

锦欢 2024-09-02 06:36:15

我要澄清的第一件事是分支名称只是特定提交的别名。当您进行拉取、推送合并等操作时,提交就是 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.

一刻暧昧 2024-09-02 06:36:15

添加所有文件后,下一步是“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.

噩梦成真你也成魔 2024-09-02 06:36:15

可能会晚了。这是因为你的 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.

幸福%小乖 2024-09-02 06:36:15

还有另一种选择,我尝试了隐藏并且在没有任何提交的情况下对我来说工作得很好在

解决所有冲突之后,

-> 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文