0

I was going through a rebase, and accidentally deleted one of my files.

When I try to get it back via git checkout it tells me

error: path 'foo' is unmerged

When I try to get it back via git reset foo, I get the following error:

fatal: ambiguous argument 'foo': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions

How can I get my file back?

daxelrod
  • 2,499
  • 1
  • 22
  • 30

1 Answers1

1

The "path is unmerged" error suggests that you haven't just deleted a file, but have some kind of conflict with it. If, however, you can manage to get to a clean working tree and index (git status shows no output except maybe some untracked files), and if you still don't have your file named "foo", then pick a commit, say HEAD~5, that has "foo" in the state you want, and git checkout HEAD~5 -- foo.

Ryan Stewart
  • 126,015
  • 21
  • 180
  • 199
  • 1
    Helpful tip, Catherine can use `git show HEAD~5:foo` to look at that state of the file. – basicxman Sep 15 '11 at 02:58
  • 1
    @basicxman: Helpful indeed, but it needs a colon: `git show HEAD~5:foo`. – Ryan Stewart Sep 15 '11 at 03:01
  • Thanks for the answer. I think that the conflict thing is correct--there were merge conflicts within it that I had to resolve, except then instead of checking it in I removed it (that's what I get for holding a conversation while rebasing). The problem is that now I can't rebase because I can't `git add` add a nonexistent file. Should I just `git rm` it and then retrieve it later? Will that affect my rebase adversely? – Catherine Sep 15 '11 at 04:59
  • Maybe the simplest thing is just to remove it, reset it, and check it back out. I.e. `git rm foo` to stage removal and wipe out the conflict, `git reset foo` to unstage it, and then `git checkout HEAD~5 -- foo` to get your preferred version of it. Of course, this will lose the changes being introduced by the rebase at this point. I'm not sure that there's a way to recover those without restarting the rebase, since you've already removed the file with the conflict markers. – Ryan Stewart Sep 15 '11 at 11:57