Search code examples
gitmsysgit

Undoing checkout in GIT


Using the MSYSGIT history browser, I checked-out a particular commit. This was a mistake - I was trying to view the files and thought I had to check them out to see them.

Since then, I have been unable to push any more commits to my remote repo (on GITHub). Which I somewhat understand, though not entirely. [I admit to not fully understand how checkout works, please be gentle on a noob.]

How do I undo that checkout and get things back to the state they were in before?

If I right click on the 'checked out' commit, I get the option to 'remove this branch'. Will that delete the files of that commit [bad] or just undo the check out? Am I barking up the wrong tree?


Solution

  • My understanding is that you accidentally checked out an earlier commit, and then made fresh commits on top of that which you'd like to keep. First, ensure you have a pointer to your current work:

    git checkout -b newbranch
    

    Then, checkout where you were before:

    git checkout master
    

    Hopefully this will put you back on your mainline branch and you can push to GitHub as normal. If you then want to pull in the changes you made, you can then merge and (once you're happy) delete the temporary branch:

    git merge newbranch
    git branch -d newbranch