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

关于这个问题,我读过所有类似的问题; 似乎以下所有问题都没有奏效:

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

几乎所有的组合,包括存储更改和从存储库中提取,都会导致不可合并的文件。我想放弃所有本地更改,只使用远程,但我不能再次克隆(带宽和互联网使用限制与开发人员试图这样做)。我该怎么做?

刚试过了:

git stash
git pull

也没有工作。

更多信息

有一个本地提交,上游也有一个提交。因此,我尝试了 git pull --rebase,但它仍然不能正常工作... 这给我带来错误-“退出,因为一个未解决的冲突”。如果我做 git stash, git reset --hard HEAD, git pull --rebase,我得到错误“拉不可能,未合并的更改...”

256874 次浏览

I've had luck with

git checkout -f <branch>

in a similar situation.

http://www.kernel.org/pub//software/scm/git/docs/git-checkout.html

Undo delete in GIT

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.

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).

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

git fetch origin
git reset --hard origin/master

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

WARNING: As stated in the comments, this will throw away your local changes and overwrite with whatever is on the 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 .git folder 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.

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 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.

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

I got solved with git remove the unmerged file locally.

$ 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 . -m "my send commit"
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean

Ryan Stewart's answer was almost there. In the case where you actually don't want 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 will commit just the merges into a new commit. (In my case, I had additional added files on disk, which weren't lumped into that commit.)

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

If you ever happen to get this issue after running a git fetch and then git is not allowing you to run git pull because 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.

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 stash pop

Hope it will help.

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

Try find files that has merge conflicts:

git diff --name-status --diff-filter=U

Resolve conflict for files, than add this file to commit (even if file is deleted)

git add path/to/file.name

After this, if there is no merge conflict files, try to commit.