如何正确执行Git合并/拉取:您尚未完成合并(存在MERGE_HEAD)

13

我是Git的新手,对如何使用Git感到困惑。我之前一直在使用CVS,所以需要学习Git。这是我尝试过的所有方法。现在,我只更改了PromoServiceImpl.java和build.sql两个文件。然而,在从“product”分支合并更改时遇到了问题。

您看到的其他文件(例如ApiServiceImpl.java等)都是其他用户的更改,但我不确定这里的表示是否意味着我已经更改了它们。也许在之前的拉取命令中已经合并了它们,现在它希望我提交它们?

无论我遵循什么命令流程(即使在进行了一些Google搜索之后),我都会看到以下错误:

您尚未完成合并(MERGE_HEAD存在) 请先提交您的更改,然后再合并。

这是我按照顺序执行的命令和输出(我尝试通过为每个人插入换行符使其美观易读)。即使从存储库中重新获取更改,此错误也会返回,因此我确信我正在做错事情,而不是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$ 

2
一个快速的建议:忘掉'git pull'吧。你不需要它,而且这是在git中迷失的最好方式。因为它只是一个git fetch后跟着一个merge,所以使用这两个命令!在这两个命令之间,你可能(肯定!)想看一下你实际要合并的内容。你也可能想要rebase而不是merge。在我看来,git pull是一个非常容易混淆的快捷方式。 - François
2
@François 我会非常小心使用rebase - 它会改变历史记录,这在版本控制系统方面是不应该做的,因为它的作用就是保留历史记录。 - valentinas
1个回答

20

这里有几件事情需要注意。了解不同的Git命令执行时会发生什么是很有帮助的。

pull命令(例如git pull会导致一次fetch,然后再进行merge。因此当你执行pull命令时,

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

Git试图从默认远程仓库拉取更改

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.

Git维护这3个版本的文件以等待合并--你的更改、被拉取的更改和共同的祖先。被拉取的版本暂时存储在MERGE_HEAD中。

您可以使用git diff查看差异。此时,您需要解决Git无法自动解决的冲突。解决后,只需执行通常的git add,然后是git commit。该过程也在commit命令的手册示例部分中解释。

哦!看起来你有一个名为Adding文件。请注意,git status命令以此结束。

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

当你让Git跟踪一个目录时(例如当你执行git init .时),就会发生这种情况。从那时起,Git会监视指定位置的变化。上面的话是在说:“我看到你添加了一个名为Adding的文件。我对它不太了解(它是未跟踪的)。”一旦你添加它,Git就开始跟踪变化(可以告诉你随着时间推移它发生了什么变化)。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接