Git Pull 是不可能的,未合并的文件

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

Git Pull is Not Possible, Unmerged Files

git

提问by Christian Stewart

I've read all of the similar questions on this; it seems that none of the following have worked:

我已经阅读了所有关于此的类似问题;似乎以下方法均无效:

Delete offending files
git reset --hard HEAD
git stash
git pull

Nearly every combination, stashing changes and pulling from repository, results in unmergable files. I'd like to discard all local changes and just use the remote, but I cannot clone again (bandwidth and internet usage limitations with the developer trying to do this). How do I do this?

几乎所有组合,存储更改和从存储库中提取,都会导致无法合并的文件。我想放弃所有本地更改并只使用遥控器,但我无法再次克隆(开发人员尝试这样做的带宽和互联网使用限制)。我该怎么做呢?

Just tried:

刚试过:

git stash
git pull

Also did not work.

也没有工作。

More Info

更多信息

There is one local commit, and the upstream has a commit as well. I've thus tried git pull --rebasebut it's still not working properly... That gives me errors - "exiting because of an unresolved conflict". If I do git stash, git reset --hard HEAD, git pull --rebase, I get the error "pull is not possible, unmerged changes..."

有一个本地提交,上游也有一个提交。我已经尝试过,git pull --rebase但它仍然无法正常工作......这给了我错误 - “由于未解决的冲突而退出”。如果我这样做git stash, git reset --hard HEAD, git pull --rebase,我会收到错误消息“无法拉动,未合并的更改......”

回答by Trevor Norris

Say the remote is originand the branch is master, and say you already have masterchecked out, might try the following:

假设遥控器 isorigin和分支 is master,并说您已经master签出,可以尝试以下操作:

git fetch origin
git reset --hard origin/master

This basically just takes the current branch and points it to the HEADof the remote branch.

这基本上只是获取当前分支并将其指向HEAD远程分支的 。

WARNING: As stated in the comments, this will throw away your local changesand overwrite with whatever is on the origin.

警告:如评论中所述,这将丢弃您的本地更改覆盖 origin 上的任何内容

Or you can use the plumbing commands to do essentially the same:

或者您可以使用管道命令执行基本相同的操作:

git fetch <remote>
git update-ref refs/heads/<branch> $(git rev-parse <remote>/<branch>)
git reset --hard

EDIT: I'd like to briefly explain why this works.

编辑:我想简要解释一下为什么会这样。

The .gitfolder can hold the commits for any number of repositories. Since the commit hash is actually a verification method for the contents of the commit, and not just a randomly generated value, it is used to match commit sets between repositories.

.git文件夹可以保存任意数量的存储库的提交。由于提交哈希实际上是对提交内容的验证方法,而不仅仅是随机生成的值,因此它用于匹配存储库之间的提交集。

A branch is just a named pointer to a given hash. Here's an example set:

分支只是指向给定散列的命名指针。这是一个示例集:

$ find .git/refs -type f
.git/refs/tags/v3.8
.git/refs/heads/master
.git/refs/remotes/origin/HEAD
.git/refs/remotes/origin/master

Each of these files contains a hash pointing to a commit:

这些文件中的每一个都包含一个指向提交的哈希:

$ cat .git/refs/remotes/origin/master
d895cb1af15c04c522a25c79cc429076987c089b

These are all for the internal git storage mechanism, and work independently of the working directory. By doing the following:

这些都是内部git存储机制,独立于工作目录工作。通过执行以下操作:

git reset --hard origin/master

git will point the current branch at the same hash value that origin/master points to. Then it forcefully changes the working directory to match the file structure/contents at that hash.

git 会将当前分支指向与 origin/master 指向的相同哈希值。然后它强制更改工作目录以匹配该哈希的文件结构/内容。

To see this at work go ahead and try out the following:

要在工作中看到这一点,请继续尝试以下操作:

git checkout -b test-branch
# see current commit and diff by the following
git show HEAD
# now point to another location
git reset --hard <remote>/<branch>
# see the changes again
git show HEAD

回答by Tayler

回答by Christian Stewart

Solved, using the following command set:

已解决,使用以下命令集:

git reset --hard
git pull --rebase
git rebase --skip
git pull

The trick is to rebase the changes... We had some trouble rebasing one trivial commit, and so we simply skipped it using git rebase --skip (after having copied the files).

诀窍是重新设置更改...我们在重新设置一个微不足道的提交时遇到了一些麻烦,因此我们只是使用 git rebase --skip (在复制了文件之后)跳过了它。

回答by Nimeshka Srimal

If you ever happen to get this issue after running a git fetchand then git is not allowing you to run git pullbecause of a merge conflict (both modified / unmerged files, and to make you more frustrated, it won't show you any conflict markers in the file since it's not yet merged). If you do not wish to lose your work, you can do the following.

如果您在运行 a 后碰巧遇到此问题git fetch,然后 gitgit pull由于合并冲突而不允许您运行(修改/未合并的文件,并且让您更加沮丧,它不会在文件,因为它尚未合并)。如果您不想丢失您的工作,您可以执行以下操作。

stage the file.

暂存文件。

$ git add filename

then stash the local changes.

然后隐藏本地更改。

$ git stash

pull and update your working directory

拉取并更新您的工作目录

$ git pull

restore your local modified file (git will automatically merge if it can, otherwise resolve it)

恢复你本地修改过的文件(如果可以,git会自动合并,否则解决它)

$ git stash pop

Hope it will help.

希望它会有所帮助。

回答by user2945950

There is a solution even if you don't want to remove your local changes. Just fix the unmerged files (by git addor git remove). Then do git pull.

即使您不想删除本地更改,也有一个解决方案。只需修复未合并的文件(通过git addgit remove)。然后做git pull

回答by Ryan Stewart

Assuming you want to throw away any changes you have, first check the output of git status. For any file that says "unmerged" next to it, run git add <unmerged file>. Then follow up with git reset --hard. That will git rid of any local changes except for untracked files.

假设您想丢弃您所做的任何更改,请首先检查git status. 对于旁边显示“未合并”的任何文件,运行git add <unmerged file>. 然后跟进git reset --hard。除了未跟踪的文件外,这将消除任何本地更改。

回答by Chetabahana

I got solved with git remove the unmerged file locally.

我用 git remove the unmerged file local 解决了。

$ git rm <the unmerged file name>
$ git reset --hard
$ git pull --rebase
$ git rebase --skip
$ git pull
Already up-to-date.

When I send git commit afterward:

当我之后发送 git commit 时:

$ git commit . -m "my send commit"
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean

回答by nightblade9

Ryan Stewart's answer was almost there. In the case where you actually don'twant to delete your local changes, there's a workflow you can use to merge:

瑞安·斯图尔特的答案几乎就在那里。在你实际的情况并不想删除你的本地修改,还有就是你可以用它来合并工作流程:

  • Run git status. It will give you a list of unmerged files.
  • Merge them (by hand, etc.)
  • Run git commit
  • 运行git status。它会给你一个未合并文件的列表。
  • 合并它们(手动等)
  • git commit

Git will commit just the mergesinto a new commit. (In my case, I had additional added files on disk, which weren't lumped into that commit.)

Git 只会将合并提交到新的提交中。(就我而言,我在磁盘上添加了其他文件,这些文件并未归入该提交。)

Git then considers the merge successful and allows you to move forward.

然后 Git 认为合并成功并允许您继续前进。