为什么git说"因为你有未合并的文件而无法拉动"?

Har*_*ana 166 git git-pull git-fetch merge-conflict-resolution

当我尝试在终端中拉入我的项目目录时,我看到以下错误:

harsukh@harsukh-desktop:~/Sites/branch1$ git pull origin master
U app/config/app.php
U app/config/database.php
U app/routes.php
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'.
Run Code Online (Sandbox Code Playgroud)

为什么git说"Pull is not possible because you have unmerged files",我该如何解决?

mu *_*u 無 189

目前发生的是,您有一组特定的文件,您之前尝试过这些文件,但它们会引发合并冲突.理想情况下,如果发生合并冲突,他应该手动解决它们,并使用提交更改git add file.name && git commit -m "removed merge conflicts".现在,另一个用户已经在他的存储库中更新了有问题的文件,并将他的更改推送到了公共上游存储库.

碰巧,你的合并冲突(可能)最后一次提交没有得到解决,所以你的文件没有合并,因此文件的U(unmerged)标志.所以现在,当你这样做时git pull,git会抛出错误,因为你有一些版本的文件,但没有正确解析.

要解决此问题,您必须先解决有问题的合并冲突,然后添加并提交更改,然后才能执行此操作git pull.

样本复制和解决问题:

# Note: commands below in format `CUURENT_WORKING_DIRECTORY $ command params`
Desktop $ cd test
Run Code Online (Sandbox Code Playgroud)

首先,让我们创建存储库结构

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
Run Code Online (Sandbox Code Playgroud)

现在我们处于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.
Run Code Online (Sandbox Code Playgroud)

如果我们忽略克隆中的冲突,并在原始仓库中进行更多提交,

repo_clone $ cd ../repo
repo $ echo "text1" >> file && git add file && git commit -m "msg" && cd ../repo_clone
Run Code Online (Sandbox Code Playgroud)

然后我们做了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'.
Run Code Online (Sandbox Code Playgroud)

请注意,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
Run Code Online (Sandbox Code Playgroud)

因此,要解决此问题,我们首先需要解决之前忽略的合并冲突

repo_clone $ vi file
Run Code Online (Sandbox Code Playgroud)

并将其内容设置为

text2
text1
text1
Run Code Online (Sandbox Code Playgroud)

然后添加它并提交更改

repo_clone $ git add file && git commit -m "resolved merge conflicts"
[master 39c3ba1] resolved merge conflicts
Run Code Online (Sandbox Code Playgroud)

  • 当你有简单的冲突修复单个文件时,这个答案非常有效.例如,一行或两行不同的代码.如果您有多个文件已被大量修改,这是一个困难的修复.但是它让人想到,通过Git你应该经常承诺避免困难的冲突. (3认同)
  • 为了完整起见,我将添加 @Pawan 的答案以使用“git mergetool”来解决冲突。倒数第二步可能不太实用(“解决合并...[通过设置]...其内容为:[某些文本]”)。 (2认同)

Tim*_*sen 39

您正在尝试在工作目录不干净的同时向本地分支添加一个新提交.结果,Git拒绝做拉.请考虑以下图表以更好地可视化场景:

remote:A < - B < - C < - D
local:A < - B*
(*表示您有几个已修改但未提交的文件.)

处理这种情况有两种选择.您可以放弃文件中的更改,也可以保留它们.

选项一:丢弃更改
您可以git checkout为每个未合并的文件使用,也可以使用git reset --hard HEAD将分支中的所有文件重置为HEAD.顺便说一句,你当地分公司的HEAD是B,没有星号.如果选择此选项,则图表变为:

remote:A < - B < - C < - D
local:A < - B.

现在,当您拉动时,您可以使用master中的更改快进您的分支.拉后,你的分支看起来像主人:

local:A < - B < - C < - D.

选项二:保留更改
如果要保留更改,首先需要解决每个文件中的任何合并冲突.您可以在IDE中打开每个文件并查找以下符号:

<<<<<<< HEAD
//您的代码版本
=======
//遥控器的代码版本
>>>>>>>

Git向您展示了两个版本的代码.HEAD标记中包含的代码是当前本地分支的版本.另一个版本来自遥控器.一旦选择了代码版本(并删除了其他代码以及标记),您可以通过键入将每个文件添加到临时区域git add.最后一步是通过键入git commit -m 适当的消息来提交结果.此时,我们的图表如下所示:

remote:A < - B < - C < - D
local:A < - B < - C'

在这里,我将我们刚刚提交的提交标记为C',因为它与远程提交C不同.现在,如果你试图拉,你会得到一个非快进错误.Git无法在您的分支上播放远程更改,因为您的分支和远程分支都与公共祖先提交B分开.此时,如果您想要提取,则可以执行另一个操作git merge,也可以执行git rebase远程分支.

掌握Git需要能够理解和操纵单向链表.我希望这个解释可以帮助你思考使用Git的正确方向.


Paw*_*ani 27

这是一个简单的解决方案.但为此,您首先需要学习以下内容

vimdiff
Run Code Online (Sandbox Code Playgroud)

要删除conficts,您可以使用

git mergetool
Run Code Online (Sandbox Code Playgroud)

上面的命令基本上为每个冲突的文件打开本地文件,混合文件,远程文件(总共3个文件).本地和远程文件仅供您参考,使用它们可以选择在混合文件中包含(或不包含)的内容.然后保存并退出文件.

  • 我要说的是,方面Vimdiff*不*必需的(你可以用你的选择,比如的WinMerge,或OSX的任何合并/比较工具,内置FileMerge,请等).话虽如此,这是对@mu的答案的一个很好的补充. (2认同)

San*_*osh 15

如果您不想合并更改并且仍然想更新本地,请运行:

git reset --hard HEAD  
Run Code Online (Sandbox Code Playgroud)

这将使用HEAD重置您的本地,然后使用git pull拉动您的遥控器。

如果您已经在本地提交了合并(但尚未推送到远程),并且还想恢复它:

git reset --hard HEAD~1 
Run Code Online (Sandbox Code Playgroud)


use*_*397 6

如果要下拉远程分支以在本地运行(比如用于审查或测试目的),以及当$ git pull您遇到本地合并冲突时:

$ git checkout REMOTE-BRANCH
$ git pull  (you get local merge conflicts)
$ git reset --hard HEAD (discards local conflicts, and resets to remote branch HEAD)
$ git pull (now get remote branch updates without local conflicts)
Run Code Online (Sandbox Code Playgroud)


Dhr*_*hah 6

如果您不希望本地分支机构发生任何变化,我认为这是最好的方法

git clean -df
git reset --hard
git checkout REMOTE_BRANCH_NAME
git pull origin REMOTE_BRANCH_NAME
Run Code Online (Sandbox Code Playgroud)


Nic*_*ick 5

你有一些本地的文件需要合并才能拉.您可以签出文件,然后拉动以覆盖您的本地文件.

git checkout app/config/app.php app/config/database.php app/routes.php
git pull origin master
Run Code Online (Sandbox Code Playgroud)


S.Y*_*dav 5

我也有同样的问题,
就我而言,步骤如下-

  1. 删除了所有以U(未合并)符号开头的文件。作为-

U   project/app/pages/file1/file.ts
U   project/www/assets/file1/file-name.html
Run Code Online (Sandbox Code Playgroud)
  1. 从 master 拉取代码

$ git pull origin master
Run Code Online (Sandbox Code Playgroud)
  1. 检查状态

 $ git status
Run Code Online (Sandbox Code Playgroud)

这是它出现的消息 - 分别
有 2 个和 1 个不同的提交。 您有未合并的路径。
(use "git pull" to merge the remote branch into yours)

(fix conflicts and run "git commit")

未合并的路径:(
使用“git add ...”来标记分辨率)

both modified:   project/app/pages/file1/file.ts
both modified:   project/www/assets/file1/file-name.html
Run Code Online (Sandbox Code Playgroud)
  1. 添加了所有新更改 -

    $ git add project/app/pages/file1/file.ts
project/www/assets/file1/file-name.html
Run Code Online (Sandbox Code Playgroud)
  1. 在头上提交更改-

$ git commit -am "resolved conflict of the app."
Run Code Online (Sandbox Code Playgroud)
  1. 推送代码 -

$ git push origin master
Run Code Online (Sandbox Code Playgroud)

此图像可能解决了哪一回合的问题 - 在此处输入图片说明


小智 5

遵循的步骤:

step-1 : git reset --hard HEAD  (if you want to reset it to head)
step-2 : git checkout Master 
step-3 : git branch -D <branch Name>
(Remote Branch name where you want to get pull) 
step-4 : git checkout <branch name>
step-5 : git pull. (now you will not get any
error)
Run Code Online (Sandbox Code Playgroud)

谢谢,萨巴什