Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

After git rm file; commit -- how to get file back from the remote branch?

I was pulling in my .emacs directory, and hit the following conflict:

CONFLICT (add/add): Merge conflict in elisp/dired-details+.el

Git status showed the following:

 Unmerged paths:
 #   (use "git add/rm <file>..." as appropriate to mark resolution)
 #  both added:         elisp/dired-details+.el

Okay, so git suggested using git rm. I want to completely replace the local file with the file I'm pulling in, so it seems kind of sort of sensible... So I do git rm elisp/dired-details+.el and git merge. I get:

git merge: fatal: You have not concluded your merge (MERGE_HEAD exists). Please, commit your changes before you can merge.

Okay, fine: git commit -a -m "ugh merge conflicts"; git pull origin master.

Now everything merges fine, except for I am missing dired-details+.el, I am kind of confused, and I would like to know the answers to the following:

  1. How do I undo git-rm and get that file from the remote branch?..
  2. Why was there a conflict in the first place?.. What's going on here with add/add?..
  3. What should I have done instead of git-rm'ing the file I wanted to replace?..
like image 278
Leo Alekseyev Avatar asked Jan 06 '11 09:01

Leo Alekseyev


1 Answers

First, undo your merge commit using git reset --hard HEAD^. Note that this will wipe out the last commit and reset your working copy to the state of the previous commit, so be careful to understand what this will do before doing it. gitk can help with visualizing this.

Second, git rm doesn't do what you expected here. When you have a merge conflict, you're looking at the file in a semi-merged state, with the file containing conflict markers for you to resolve the conflict. At this point, you are being asked to fix the working copy to the state that you want the final merge commit to look like. By removing the file, you told git that you don't want the file to be present at all any more, which is not what you wanted.

What you need to do at this stage is update the conflicted file in your working copy to the version you want to keep. Usually this is done by examining the conflict markers and adjusting the file accordingly. In your case, if you know for certain that you want the copy from the branch you're merging from, you can use git show :3:elisp/dired-details+.el > elisp/dired-details+.el to do this. Then git add elisp/dired-details+.el to tell git that you've resolved the conflict in that file and then git commit to finish.

Here, git show :3:... is requesting the version of the file from MERGE_HEAD. If you need a different version, you can also use 1 for the common ancestor or 2 for "your" side of the merge.

like image 165
Robie Basak Avatar answered Nov 14 '22 07:11

Robie Basak