6h8j5 6h8j5 - 4 months ago 13
Git Question

Git branch diverged after removing a file that has moved

A file in the master repository had been moved into a subfolder, on my local server I deleted the file and was going to add it in on a future check in to the new location.

Upon checking in the commit and doing a

git pull --rebase origin master
I had a conflict stating that the file had been deleted in my commit and renamed/moved in a different commit so the merge failed.

I then did a
git rebase --abort
.

Now when I do git status I get the message:


Your branch and origin/master have diverged and have 1 and 27 commits each.


How can I fix this?

Answer

Start the git rebase operation again from the start.

When you hit the step where there's a conflict (you deleted, other moved), you need to decide how to resolve the conflict, preferably after discussing with the other developer who did the moving.

If the file should be removed, then delete it:

git rm path/to/renamed/file

If the file should remain, then add it:

git add path/to/renamed/file

The path/to/renamed/file is the conflicted path given by git status.

After you removed or added, continue the rebase with:

git rebase --continue

Note that if you decided to git add the conflicted file, then when you do git rebase --continue, it will probably tell you this:

No changes - did you forget to use 'git add'?
If there is nothing left to stage, chances are that something else
already introduced the same changes; you might want to skip this patch.

Indeed, the file was already added by the other branch, with your git add you just confirmed to keep that action, without really adding anything new. In this situation continue the rebase with git rebase --skip.