Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git could not unlink file during pull - how do I get out of inconsistent state of repository?

Tags:

git

While doing "git pull", git aborted the operation since it could not unlink a file it wanted to overwrite, because I forgot to close an application locked these files.

When I close the application and try to reexecute "git pull" and get the following error message:

"Error: Your local changes to the following files would be overwritten by merge: ..."

and

"Error: The following untracked working tree files would be overwritten by merge: ..."

Clearly, git just aborted the pull without doing a roll-back and now thinks the changes that were just pulled are my local changes.

How do I get out of this state? We are talking about a lot of files, and before doing a "git revert" I would have to check manually for each file if there were some local changes from my side.

Note: we are using git in a client-server-like setup, where the distributed developers "pull" and "push&commit" frequently to a central bitbucket repository.

like image 646
Stiefel Avatar asked Jan 25 '16 14:01

Stiefel


People also ask

How do I fix a git pull error?

How to Fix error: failed to push some refs to Error in Git Using git pull --rebase. The git pull --rebase command is helpful in situations where your local branch is a commit behind the remote branch.

What is unlink of file in git?

What does it mean? Answered by anu rhea. Getting message of git unlink of file failed means that another program is using the same file, which is preventing git from "moving" the file into or out of the working directory when you are attempting to change branches.

How do I discard changes in git?

There are two Git commands a developer must use in order to discard all local changes in Git, remove all uncommited changes and revert their Git working tree back to the state it was in when the last commit took place. The commands to discard all local changes in Git are: git reset –hard. git clean -fxd.

Does git pull affect untracked files?

@AayushNeupane git is not going to do anything with untracked files, they are untracked means git is not tracking them, so no git command can do anything to the untracked files.


1 Answers

Since none of the above methods worked for me, I used a procedure that does is not fully automatic, but reduces the effort to a few manually selected files (in my case 2 instead of 50).

First stash all files (including untracked and ignored) using

git stash save --all             

Then redo the pull, that previously failed, using

git pull                         

Now checkout from the stash to simply overwrite all files, that were changed during the first pull (no merge attempt):

git checkout stash -- .         

Now one problem is left: changes that were made on the server between the failed pull and the second pull are now overwritten. But those should be only a few files (if the time between the two 'pull' was short), and much more easy to identify in the list of working changes you made yourself. Simply "revert" them (I used the Tortoise GIT commit dialog) before doing the final commit.

git commit
git stash drop
like image 99
Stiefel Avatar answered Nov 04 '22 01:11

Stiefel