5

I have been working on some code, which I use git to manage. Earlier, I used commit to save a version of my code that worked. I later decided to redo the code, since it needed some new features and was designed poorly. After I got it working, I did another git commit. After doing git log, I saw I was not on any branch. So, I did "git checkout master". This brought be back to the version of my old code, with git log now showing my latest commit. This was A LOT of work, so I wanted to know if there was any way to undo what I did, and get back my latest code. Thanks in advance for your help.

futurevilla216
  • 982
  • 3
  • 13
  • 29
  • 1
    possible duplicate of [Not currently on any branch + git commit + checkout when not in any branch. Did i loose my changes?](http://stackoverflow.com/questions/6048425/not-currently-on-any-branch-git-commit-checkout-when-not-in-any-branch-did-i) – manojlds Aug 31 '11 at 22:07
  • Similar, however I have another problem - when merging I am getting a conflict on some of Xcode's files (see my comment below). – futurevilla216 Aug 31 '11 at 22:15

2 Answers2

14

Check the git reflog and find your commit.

Safest thing is to create a branch at the "lost" commit, then check if everything is dandy using gitk or git log -p

git branch recovery HEAD@{1} # use whatever commit you need
git log -p recovery

This new branch can then be merged, rebased on top of master, commits of it cherry-picked, etc. There are many possibilities in git to shoot yourself in the foot but also several more ways to re-attach that foot (possibly to your arms).

If you haven't done any new commits on master, you can simply merge the commit in question, which will be resolved as a fast-forward merge by git:

git merge HEAD@{1} # use whatever commit you need

If you don't care about any new commits on master and you simply want to reset master branch to that lost commit, use git reset. To not lose any changes, stash changes to your working copy first (git stash save)

git reset --keep HEAD@{1}

The next time you discover that you aren't on any branch, use git branch newbranch or git checkout -b newbranch

knittl
  • 246,190
  • 53
  • 318
  • 364
  • Thanks. When I am doing this however, I am getting two conflicts (both not on my files, on Xcode files). Is there anyway for me to override the current version with the new version? – futurevilla216 Aug 31 '11 at 22:11
  • Also, thanks for the advice for next time. However, what if I do not want to create a new branch? – futurevilla216 Aug 31 '11 at 22:12
  • @Lenny: to override, use `git reset --hard/--keep`. You can delete the new branch after you are done recovering your changes, if you really don't want to create one, move the master branch (`git reset` or `git branch -f`) – knittl Aug 31 '11 at 22:17
  • 1
    I am still getting the conflict. Running git reset (with --hard and --keep) works fine, however I am still getting conflicts when I merge. Also, thanks for the branch tip, I will keep that in mind next time. – futurevilla216 Aug 31 '11 at 22:20
  • @Lenny: when you've reset the branch to the "lost" commit you don't need to merge (and even if you do, git should tell you »everything up to date«). For git novices it's easier to create a new branch on the lost commit and check if everything is really there (`gitk` or `git log -p`). After you've verified this, you can always reset your master branch and delete the temporary ›recovery‹ branch – knittl Aug 31 '11 at 22:31
  • You lost me. In your answer, you said I should merge. Now, you are saying the opposite... – futurevilla216 Aug 31 '11 at 22:33
  • @Lenny: I said you should merge **if** you hadn'n tone any new commits on master, so git will do a fast-forward. I'll update my answer – knittl Sep 01 '11 at 07:05
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/3052/discussion-between-knittl-and-lenny-k) – knittl Sep 01 '11 at 07:05
1

Use git reflog, get the sha of your new commit ( or you can reference it in format like HEAD@{1}) and use git cherry-pick, git merge etc to bring in the commit to master.

manojlds
  • 290,304
  • 63
  • 469
  • 417