目前发生的情况是,您有一组文件,您曾尝试合并它们,但它们产生了合并冲突。
理想情况下,如果出现合并冲突,应该手动解决它们,并使用 git add file.name && git commit -m "removed merge conflicts"
提交更改。
现在,另一个用户已经更新了相关的文件,并将其更改推送到共同的上游仓库。
很遗憾,您的合并冲突(可能是最后一次提交)未被解决,因此您的文件没有正确合并,并且对应的标记为 U
(unmerged
)。
因此,当您执行 git pull
时,Git 会抛出错误,因为您有某个版本的文件未正确解决。
要解决此问题,您需要解决相关的合并冲突,并添加和提交更改,然后才能执行 git pull
。
问题的示例复制和解决:
Desktop $ cd test
首先,让我们创建仓库结构。
test $ mkdir repo && cd repo && git init && touch file && git add file && git commit -m "msg"
repo $ cd .. && git clone repo repo_clone && cd repo_clone
repo_clone $ echo "text2" >> file && git add file && git commit -m "msg" && cd ../repo
repo $ echo "text1" >> file && git add file && git commit -m "msg" && cd ../repo_clone
现在我们在repo_clone目录中,如果你执行git pull
命令,它会引发冲突。
repo_clone $ git pull origin master
remote: Counting objects: 5, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From /home/anshulgoyal/Desktop/test/test/repo
* branch master -> FETCH_HEAD
24d5b2e..1a1aa70 master -> origin/master
Auto-merging file
CONFLICT (content): Merge conflict in file
Automatic merge failed; fix conflicts and then commit the result.
如果我们忽略克隆中的冲突,并在原始存储库中进行更多的提交现在,
repo_clone $ cd ../repo
repo $ echo "text1" >> file && git add file && git commit -m "msg" && cd ../repo_clone
然后我们执行 git pull
命令,就能获取到
repo_clone $ git pull
U file
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'.
请注意,file
现在处于未合并状态,如果我们运行 git status
命令,我们可以清楚地看到同样的结果:
repo_clone $ git status
On branch master
Your branch and 'origin/master' have diverged,
and have 1 and 1 different commit each, respectively.
(use "git pull" to merge the remote branch into yours)
You have unmerged paths.
(fix conflicts and run "git commit")
Unmerged paths:
(use "git add <file>..." to mark resolution)
both modified: file
因此,要解决这个问题,我们首先需要解决之前忽略的合并冲突。
repo_clone $ vi file
并将其内容设置为
text2
text1
text1
然后将其添加并提交更改。
repo_clone $ git add file && git commit -m "resolved merge conflicts"
[master 39c3ba1] resolved merge conflicts