如何正确执行 git merge/pull:您尚未完成合并(MERGE_HEAD 存在)

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

How to do git merge/pull correctly: You have not concluded your merge (MERGE_HEAD exists)

gitmergegit-pull

提问by Smiles in a Jar

I am a newbie to Git and having trouble understanding how to use Git. I have been using CVS so going through some learning curve for Git. Here is what all I tried

我是 Git 的新手,无法理解如何使用 Git。我一直在使用 CVS,因此对 Git 进行了一些学习曲线。这是我尝试过的所有内容

Now I had changed only two files PromoServiceImpl.java and build.sql. However having trouble merging changed from "product" branch.

现在我只更改了两个文件 PromoServiceImpl.java 和 build.sql。但是,从“产品”分支合并时遇到麻烦。

Remaining files that you see (like ApiServiceImpl.java etc) are changes from other users but I am not sure if the representation here suggests that I have changed them. Prob it got merged during earlier pull commands and now it expects me to commit them ??

您看到的剩余文件(如 ApiServiceImpl.java 等)是来自其他用户的更改,但我不确定此处的表示是否表明我已更改它们。可能它在早期的 pull 命令中被合并了,现在它希望我提交它们??

No matter whatever is the flow of commands I follow (even after some Google search) I see the following error:

无论我遵循的命令流是什么(即使经过一些谷歌搜索),我都会看到以下错误:

You have not concluded your merge (MERGE_HEAD exists) Please, commit your changes before you can merge.

您尚未完成合并(MERGE_HEAD 存在)请在合并之前提交您的更改。

Here is the sequence of commands I followed and output (tried to make it pretty by inserting new lines for everyone to read). Also this error comes back even after taking changes from repository afresh so I am sure I am doing something wrong rather than some issue with git.

这是我遵循和输出的命令序列(试图通过插入新行让每个人阅读来让它变得漂亮)。即使重新从存储库进行更改后,此错误也会再次出现,因此我确定我做错了什么,而不是 git 出现了一些问题。

kamal@kamal-Lenovo-G470:~/git/TryGit$ gs
# On branch product
# Your branch is ahead of 'origin/product' by 1 commit.
#
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   TryGitServices/src/main/java/com/TryGit/services/promo/impl/PromoServiceImpl.java
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   Adding
no changes added to commit (use "git add" and/or "git commit -a")


kamal@kamal-Lenovo-G470:~/git/TryGit$ git pull origin product
From ssh://192.168.2.251/TryGit
 * branch            product    -> FETCH_HEAD
Auto-merging TryGitResources/build/build.sql
CONFLICT (content): Merge conflict in TryGitResources/build/build.sql
Automatic merge failed; fix conflicts and then commit the result.


kamal@kamal-Lenovo-G470:~/git/TryGit$ gs
# On branch product
# Your branch is ahead of 'origin/product' by 1 commit.
#
# Changes to be committed:
#
#   modified:   TryGitCore/src/main/java/com/TryGit/core/api/binding.xml
#   modified:   TryGitServices/src/main/java/com/TryGit/services/api/impl/ApiServiceImpl.java
#   modified:   TryGitServices/src/main/java/com/TryGit/services/common/impl/StartupServiceImpl.java
#   modified:   TryGitWeb/src/main/webapp/WEB-INF/wsdl/TryGit.wsdl
#   modified:   TryGitWeb/src/main/webapp/static/css/TryGit/homeDeal.css
#   modified:   TryGitWeb/src/main/webapp/static/css/TryGit/TryGit.css
#   modified:   TryGitWeb/src/main/webapp/static/img/TryGit/sprite/TryGit.png
#   modified:   TryGitWeb/src/main/webapp/views/admin/createPromoCode.jsp
#   modified:   TryGitWeb/src/main/webapp/views/admin/product/productAdmin.jsp
#   modified:   TryGitWeb/src/main/webapp/views/deal/homePage.jsp

#
# Unmerged paths:
#   (use "git add/rm <file>..." as appropriate to mark resolution)
#
#   both modified:      TryGitResources/build/build.sql
#
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   TryGitServices/src/main/java/com/TryGit/services/promo/impl/PromoServiceImpl.java
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   Adding


kamal@kamal-Lenovo-G470:~/git/TryGit$ git reset TryGitResources/build/build.sql
Unstaged changes after reset:
M   TryGitResources/build/build.sql
M   TryGitServices/src/main/java/com/TryGit/services/promo/impl/PromoServiceImpl.java
kamal@kamal-Lenovo-G470:~/git/TryGit$ gs
# On branch product
# Your branch is ahead of 'origin/product' by 1 commit.
#
# Changes to be committed:
#
#   modified:   TryGitCore/src/main/java/com/TryGit/core/api/binding.xml
#   modified:   TryGitResources/build/build.sql
#   modified:   TryGitServices/src/main/java/com/TryGit/services/api/impl/ApiServiceImpl.java
#   modified:   TryGitServices/src/main/java/com/TryGit/services/common/impl/StartupServiceImpl.java
#   modified:   TryGitWeb/src/main/webapp/WEB-INF/wsdl/TryGit.wsdl
#   modified:   TryGitWeb/src/main/webapp/static/css/TryGit/homeDeal.css
#   modified:   TryGitWeb/src/main/webapp/static/css/TryGit/TryGit.css
#   modified:   TryGitWeb/src/main/webapp/static/img/TryGit/sprite/TryGit.png
#   modified:   TryGitWeb/src/main/webapp/views/admin/createPromoCode.jsp
#   modified:   TryGitWeb/src/main/webapp/views/admin/product/productAdmin.jsp
#   modified:   TryGitWeb/src/main/webapp/views/deal/homePage.jsp
#
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   TryGitResources/build/build.sql
#   modified:   TryGitServices/src/main/java/com/TryGit/services/promo/impl/PromoServiceImpl.java
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   Adding


kamal@kamal-Lenovo-G470:~/git/TryGit$ git stash
Saved working directory and index state WIP on product: b8bb080 changing build.sql for RBT task
HEAD is now at b8bb080 changing build.sql for RBT task


kamal@kamal-Lenovo-G470:~/git/TryGit$ gs
# On branch product
# Your branch is ahead of 'origin/product' by 1 commit.
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   Adding
nothing added to commit but untracked files present (use "git add" to track)


kamal@kamal-Lenovo-G470:~/git/TryGit$ git reset Adding


kamal@kamal-Lenovo-G470:~/git/TryGit$ git reset Adding


kamal@kamal-Lenovo-G470:~/git/TryGit$ gs
# On branch product
# Your branch is ahead of 'origin/product' by 1 commit.
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   Adding
nothing added to commit but untracked files present (use "git add" to track)

kamal@kamal-Lenovo-G470:~/git/TryGit$ git rm Adding
fatal: pathspec 'Adding' did not match any files

kamal@kamal-Lenovo-G470:~/git/TryGit$ man git rm

kamal@kamal-Lenovo-G470:~/git/TryGit$ git rm Adding
fatal: pathspec 'Adding' did not match any files

kamal@kamal-Lenovo-G470:~/git/TryGit$ gs
# On branch product
# Your branch is ahead of 'origin/product' by 1 commit.
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   Adding
nothing added to commit but untracked files present (use "git add" to track)

kamal@kamal-Lenovo-G470:~/git/TryGit$ git pull origin product
remote: Counting objects: 566, done.
remote: Compressing objects: 100% (316/316), done.
remote: Total 386 (delta 207), reused 0 (delta 0)
Receiving objects: 100% (386/386), 203.81 KiB, done.
Resolving deltas: 100% (207/207), completed with 56 local objects.
From ssh://192.168.2.251/TryGit
 * branch            product    -> FETCH_HEAD
Auto-merging TryGitResources/build/build.sql
CONFLICT (content): Merge conflict in TryGitResources/build/build.sql
Automatic merge failed; fix conflicts and then commit the result.

kamal@kamal-Lenovo-G470:~/git/TryGit$ git pull origin product
M   TryGitCore/src/main/java/com/TryGit/core/api/binding.xml
U   TryGitResources/build/build.sql
M   TryGitServices/src/main/java/com/TryGit/services/api/impl/ApiServiceImpl.java
M   TryGitServices/src/main/java/com/TryGit/services/common/impl/StartupServiceImpl.java
M   TryGitWeb/src/main/webapp/WEB-INF/wsdl/TryGit.wsdl
M   TryGitWeb/src/main/webapp/static/css/TryGit/homeDeal.css
M   TryGitWeb/src/main/webapp/static/css/TryGit/TryGit.css
M   TryGitWeb/src/main/webapp/static/img/TryGit/sprite/TryGit.png
M   TryGitWeb/src/main/webapp/views/admin/createPromoCode.jsp
M   TryGitWeb/src/main/webapp/views/admin/product/productAdmin.jsp
M   TryGitWeb/src/main/webapp/views/deal/homePage.jsp
Pull is not possible because you have unmerged files.
Please, fix them up in the work tree, and then use 'git add/rm <file>'
as appropriate to mark resolution, or use 'git commit -a'.

kamal@kamal-Lenovo-G470:~/git/TryGit$ git add TryGitResources/build/build.sql
kamal@kamal-Lenovo-G470:~/git/TryGit$ git pull origin product
You have not concluded your merge (MERGE_HEAD exists).
Please, commit your changes before you can merge.
kamal@kamal-Lenovo-G470:~/git/TryGit$ gs
# On branch product
# Your branch is ahead of 'origin/product' by 1 commit.
#
# Changes to be committed:
#
#   modified:   TryGitCore/src/main/java/com/TryGit/core/api/binding.xml
#   modified:   TryGitResources/build/build.sql
#   modified:   TryGitServices/src/main/java/com/TryGit/services/api/impl/ApiServiceImpl.java
#   modified:   TryGitServices/src/main/java/com/TryGit/services/common/impl/StartupServiceImpl.java
#   modified:   TryGitWeb/src/main/webapp/WEB-INF/wsdl/TryGit.wsdl
#   modified:   TryGitWeb/src/main/webapp/static/css/TryGit/homeDeal.css
#   modified:   TryGitWeb/src/main/webapp/static/css/TryGit/TryGit.css
#   modified:   TryGitWeb/src/main/webapp/static/img/TryGit/sprite/TryGit.png
#   modified:   TryGitWeb/src/main/webapp/views/admin/createPromoCode.jsp
#   modified:   TryGitWeb/src/main/webapp/views/admin/product/productAdmin.jsp
#   modified:   TryGitWeb/src/main/webapp/views/deal/homePage.jsp

#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   Adding

kamal@kamal-Lenovo-G470:~/git/TryGit$ git pull origin product
You have not concluded your merge (MERGE_HEAD exists).
Please, commit your changes before you can merge.

kamal@kamal-Lenovo-G470:~/git/TryGit$ 

回答by Sri Sankaran

There are a couple of things going on here. It helps to understand what happens when you issue various Git commands.

这里有几件事情正在发生。它有助于理解当您发出各种 Git 命令时会发生什么。

The pullcommand (as in git pull) results ina fetchfollowed by a merge. So when you did a pull

pull命令(如git pull)的结果是afetch后跟 a merge。所以当你做了一个pull

kamal@kamal-Lenovo-G470:~/git/TryGit$ git pull origin product

Git tried to pull in changes from the default remote repository

Git 尝试从默认远程存储库中提取更改

From ssh://192.168.2.251/TryGit
 * branch            product    -> FETCH_HEAD

and merge them in.

并将它们合并。

Auto-merging TryGitResources/build/build.sql

In so doing it encountered a conflict

在这样做时遇到了冲突

CONFLICT (content): Merge conflict in TryGitResources/build/build.sql
Automatic merge failed; fix conflicts and then commit the result.

Git maintains this 3 versionsof the file pending a merge -- yourchanges, changes pulled in and the common ancestor. The version pulled in is temporarily stored in MERGE_HEAD.

Git 维护文件的这 3 个版本,等待合并——您的更改、引入的更改和共同祖先。拉入的版本暂时存放在 MERGE_HEAD 中。

You can see the differences with a git diffinvocation. At this point you need to fix the conflicts that Git was unable to automatically resolve. Once you have fixed it simply perform the usual git addfollowed by a git commit. This process is also explained in the examples section of the man page for the commitcommand.

您可以通过git diff调用查看差异。此时需要修复 Git 无法自动解决的冲突。一旦你修复了它,只需执行通常的git add后跟git commit. 该commit命令的手册页的示例部分也解释了此过程。

Oh! And it appears you have a filecalled Adding. Note that the git statuscommand ends with

哦!它似乎你有一个文件Adding。请注意,该git status命令以

# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   Adding

This happens when you ask Git to track a directory (such as when you did git init .). From then on Git monitors changes in the specified location. The above is saying that "I see that you have added a file called Adding. I don't know much more about it (it is untracked)." Once you add it, Git starts tracking changes (can tell you whathas changed with it over time).

当您要求 Git 跟踪目录时(例如您执行时git init .),就会发生这种情况。从此 Git 监视指定位置的更改。上面是说“我看到你添加了一个名为Adding的文件。我对它了解不多(它没有被跟踪)。” 添加它后,Git 开始跟踪更改(可以告诉您随着时间的推移发生了什么变化)。