为什么 git 说“无法拉取,因为您有未合并的文件”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26376832/
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
Why does git say "Pull is not possible because you have unmerged files"?
提问by Harsukh Makwana
When I try to pull in my project directory in the terminal, I see the following error:
当我尝试在终端中拉入我的项目目录时,我看到以下错误:
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'.
Why does git say "Pull is not possible because you have unmerged files"
, and how can I resolve it?
为什么 git 说"Pull is not possible because you have unmerged files"
,我该如何解决?
回答by u1860929
What is currently happening is, that you have a certain set of files, which you have tried merging earlier, but they threw up merge conflicts.
Ideally, if one gets a merge conflict, he should resolve them manually, and commit the changes using git add file.name && git commit -m "removed merge conflicts"
.
Now, another user has updated the files in question on his repository, and has pushed his changes to the common upstream repo.
当前发生的情况是,您有一组特定的文件,您之前尝试过合并这些文件,但它们引发了合并冲突。理想情况下,如果遇到合并冲突,他应该手动解决它们,并使用git add file.name && git commit -m "removed merge conflicts"
. 现在,另一个用户更新了他存储库中的相关文件,并将他的更改推送到公共上游存储库。
It so happens, that your merge conflicts from (probably) the last commit were not not resolved, so your files are not merged all right, and hence the U
(unmerged
) flag for the files.
So now, when you do a git pull
, git is throwing up the error, because you have some version of the file, which is not correctly resolved.
碰巧的是,您的(可能)上次提交的合并冲突未得到解决,因此您的文件没有完全合并,因此文件的U
( unmerged
) 标志。所以现在,当您执行 a 时git pull
,git 会抛出错误,因为您有某个版本的文件,但未正确解析。
To resolve this, you will have to resolve the merge conflicts in question, and add and commit the changes, before you can do a git pull
.
要解决此问题,您必须先解决有问题的合并冲突,然后添加并提交更改,然后才能执行git pull
.
Sample reproduction and resolution of the issue:
问题的示例复制和解决方案:
# Note: commands below in format `CUURENT_WORKING_DIRECTORY $ command params`
Desktop $ cd test
First, let us create the repository structure
首先,让我们创建存储库结构
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
Now we are in repo_clone, and if you do a git pull
, it will throw up conflicts
现在我们在 repo_clone 中,如果你执行 a 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.
If we ignore the conflicts in the clone, and make more commits in the original repo now,
如果我们忽略克隆中的冲突,现在在原始存储库中进行更多提交,
repo_clone $ cd ../repo
repo $ echo "text1" >> file && git add file && git commit -m "msg" && cd ../repo_clone
And then we do a git pull
, we get
然后我们做一个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'.
Note that the file
now is in an unmerged state and if we do a git status
, we can clearly see the same:
请注意,file
now 处于未合并状态,如果我们执行 a 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
So, to resolve this, we first need to resolve the merge conflict we ignored earlier
所以,要解决这个问题,我们首先需要解决我们之前忽略的合并冲突
repo_clone $ vi file
and set its contents to
并将其内容设置为
text2
text1
text1
and then add it and commit the changes
然后添加它并提交更改
repo_clone $ git add file && git commit -m "resolved merge conflicts"
[master 39c3ba1] resolved merge conflicts
回答by Tim Biegeleisen
You are attempting to add one more new commits into your local branch while your working directory is not clean. As a result, Git is refusing to do the pull. Consider the following diagrams to better visualize the scenario:
当您的工作目录不干净时,您正在尝试向本地分支添加一个新的提交。因此,Git 拒绝执行拉取操作。考虑下图以更好地可视化场景:
remote: A <- B <- C <- D
local: A <- B*
(*indicates that you have several files which have been modified but not committed.)
远程:A <- B <- C <- D
本地:A <- B*
(*表示您有几个文件已被修改但未提交。)
There are two options for dealing with this situation. You can either discard the changes in your files, or retain them.
处理这种情况有两种选择。您可以放弃文件中的更改,也可以保留它们。
Option one: Throw away the changes
You can either use git checkout
for each unmerged file, or you can use git reset --hard HEAD
to reset all files in your branch to HEAD. By the way, HEAD in your local branch is B, without an asterisk. If you choose this option, the diagram becomes:
选项一:丢弃更改
您可以git checkout
用于每个未合并的文件,也可以用于git reset --hard HEAD
将分支中的所有文件重置为 HEAD。顺便说一句,您当地分支机构的 HEAD 是 B,没有星号。如果选择此选项,图表将变为:
remote: A <- B <- C <- D
local: A <- B
远程:A <- B <- C <- D
本地:A <- B
Now when you pull, you can fast-forward your branch with the changes from master. After pulling, you branch would look like master:
现在,当您拉取时,您可以使用 master 的更改快进您的分支。拉动后,您的分支看起来像 master:
local: A <- B <- C <- D
本地:A <- B <- C <- D
Option two: Retain the changes
If you want to keep the changes, you will first want to resolve any merge conflicts in each of the files. You can open each file in your IDE and look for the following symbols:
选项二:保留更改
如果要保留更改,首先要解决每个文件中的所有合并冲突。您可以在 IDE 中打开每个文件并查找以下符号:
<<<<<<< HEAD
// your version of the code
=======
// the remote's version of the code
>>>>>>>
<<<<<<< HEAD
// 您的代码版本
========
// 遥控器的代码版本
>>>>>>
Git is presenting you with two versions of code. The code contained within the HEAD markers is the version from your current local branch. The other version is what is coming from the remote. Once you have chosen a version of the code (and removed the other code along with the markers), you can add each file to your staging area by typing git add
. The final step is to commit your result by typing git commit -m
with an appropriate message. At this point, our diagram looks like this:
Git 向您展示了两个版本的代码。HEAD 标记中包含的代码是您当前本地分支的版本。另一个版本是来自遥控器的内容。一旦您选择了代码的一个版本(并删除了其他代码和标记),您可以通过键入将每个文件添加到您的暂存区git add
。最后一步是通过键入git commit -m
适当的消息来提交您的结果。此时,我们的图表如下所示:
remote: A <- B <- C <- D
local: A <- B <- C'
远程:A <- B <- C <- D
本地:A <- B <- C'
Here I have labelled the commit we just made as C' because it is different from the commit C on the remote. Now, if you try to pull you will get a non-fast forward error. Git cannot play the changes in remote on your branch, because both your branch and the remote have diverged from the common ancestor commit B. At this point, if you want to pull you can either do another git merge
, or git rebase
your branch on the remote.
在这里,我将我们刚刚进行的提交标记为 C',因为它与遥控器上的提交 C 不同。现在,如果您尝试拉动,则会出现非快进错误。Git 无法在您的分支上播放 remote 中的更改,因为您的分支和 remote 都与共同的祖先提交 B 发生了分歧。此时,如果您想拉取,您可以执行另一个git merge
,或者git rebase
您在远程上的分支。
Getting a mastery of Git requires being able to understand and manipulate uni-directional linked lists. I hope this explanation will get you thinking in the right direction about using Git.
掌握 Git 需要能够理解和操作单向链表。我希望这个解释能让你思考使用 Git 的正确方向。
回答by Pawan Seerwani
Theres a simple solution to it. But for that you will first need to learn the following
有一个简单的解决方案。但为此,您首先需要学习以下内容
vimdiff
To remove conficts, you can use
要删除冲突,您可以使用
git mergetool
The above command basically opens local file, mixed file, remote file (3 files in total), for each conflicted file. The local & remote files are just for your reference, and using them you can choose what to include (or not) in the mixed file. And just save and quit the file.
上述命令基本上为每个冲突文件打开本地文件、混合文件、远程文件(共3个文件)。本地和远程文件仅供您参考,使用它们您可以选择在混合文件中包含(或不包含)的内容。只需保存并退出文件。
回答by Santosh
If you dont want to merge the changes and still want to update your local then run:
如果您不想合并更改但仍想更新本地,请运行:
git reset --hard HEAD
This will reset your local with HEAD and then pull your remote using git pull.
这将使用 HEAD 重置您的本地,然后使用 git pull 拉出您的遥控器。
If you've already committed your merge locally (but haven't pushed to remote yet), and want to revert it as well:
如果您已经在本地提交了合并(但尚未推送到远程),并且还想将其还原:
git reset --hard HEAD~1
回答by user5245397
If you want to pull down a remote branch to run locally (say for reviewing or testing purposes), and when you $ git pull
you get local merge conflicts:
如果您想拉下远程分支以在本地运行(例如用于或测试目的),并且当$ 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)
回答by Nick
You have some files locally that need to be merged before you can pull. You could checkout the files and then pull to overwrite your local files.
您在本地有一些文件需要合并才能拉取。您可以签出文件,然后拉动以覆盖本地文件。
git checkout app/config/app.php app/config/database.php app/routes.php
git pull origin master
回答by Sarbasish Mishra
Steps to follow :
要遵循的步骤:
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)
Thanks, Sarbasish
谢谢,萨巴什
回答by S.Yadav
There was same issue with me
In my case, steps are as below-
我也有同样的问题,
就我而言,步骤如下-
- Removed all file which was being start with U(unmerged)symbol. as-
- 删除了所有以U(未合并)符号开头的文件。作为-
U project/app/pages/file1/file.ts
U project/www/assets/file1/file-name.html
- Pull code from master
- 从 master 拉取代码
$ git pull origin master
- Checked for status
- 检查状态
$ git status
Here is the message which It appeared-
and have 2 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")
这是它出现的消息 - 分别
有 2 个和 1 个不同的提交。
您有未合并的路径。(use "git pull" to merge the remote branch into yours)
(fix conflicts and run "git commit")
Unmerged paths:
(use "git add ..." to mark resolution)
未合并的路径:(
使用“git add ...”来标记分辨率)
both modified: project/app/pages/file1/file.ts
both modified: project/www/assets/file1/file-name.html
- Added all new changes -
- 添加了所有新更改 -
$ git add project/app/pages/file1/file.ts
project/www/assets/file1/file-name.html
- Commit changes on head-
- 在头上提交更改-
$ git commit -am "resolved conflict of the app."
- Pushed the code -
- 推送代码 -
$ git push origin master
回答by dfordevy
When a merge conflict occurs , you can open individual file. You will get "<<<<<<< or >>>>>>>" symbols. These refer to your changes and the changes present on remote. You can manually edit the part that is requires. after that save the file and then do : git add
当发生合并冲突时,您可以打开单个文件。您将获得“<<<<<<< 或 >>>>>>>”符号。这些是指您的更改和远程上存在的更改。您可以手动编辑需要的部分。之后保存文件然后执行: git add
The merge conflicts will be resolved.
合并冲突将得到解决。
回答by Grace Green
Just run this command:
只需运行此命令:
git reset --hard