3

Suppose this:

mkdir test; cd test
echo "1" > file1; git init; git add .; git commit -m "initial - file 1"   # 1st commit on master
echo "2" > file2; git add .; git commit -m "file 2"                       # 2nd commit on master
git checkout -b newbranch                                                 # creates newbranch
echo "1" >> file1; git add .; git commit -m "changed 1"                   # 1st commit on newbranch
git checkout master                                                       # goes to master
echo "2" >> file2; git add .; git commit -m "changed 2"                   # 3rd commit on master
git merge newbranch -m "merge commit"                                     # merge newbranch on master
echo "3" > file3; git add .; git commit --amend -m "merge commit"         # amend merge commit and adds file3
git rebase HEAD~2                                                         # don't change anything, just leave
ls                                                                        # there isn't file3 anymore!

Is there a way to recover the merge commit so the changes that were amended on it aren't lost?

bitlogic
  • 33
  • 3

1 Answers1

0

The changes you made are still in your reflog.

After the accidental rebase (I'm assuming it's accidental, since you're talking about "lost" commits), run git reflog.

Find the entry just below the last "rebase:" line in the topmost set. The commit hash to the left of it is the amended merge commit before you started rebasing.

Borealid
  • 86,367
  • 8
  • 101
  • 120
  • Thx but any of the commits on `git reflog` shows `file3`. Did `git whatchanged` on all commits. – bitlogic Mar 30 '13 at 02:04
  • @bitlogic Check again. Running your above script, followed by `checkout HEAD@{4}`, restores `file3`. – Borealid Mar 30 '13 at 02:06
  • Yep, that's correct. It's there. Strange is the fact that `git whatchanged` doesn't show the file being added! – bitlogic Mar 30 '13 at 02:18