Go back to an older commit and re-work from there

Assuming I didn’t create another branch, and I want to go back to an older commit on the current branch…

git checkout oldcommitID

Now I’m in a “detached HEAD” state and git will show me helpful information here. Basically, now I’m not on any branch.

Now, if I make any changes, and then:

  • …(not recommended) Try to push (to github) then it will prompt me on how to push to the HEAD of the remote branch.

  • …(not recommended) Try to checkout the master (or some other) branch, then git will warn me that if I don’t want to lose my changes, then I should create a new branch to save them.

If I want to save my recent changes that I made while in the detatched HEAD state, then it’s better to save them by creating a new branch:

git branch new-branch-name

and then I can merge them back in with master (see above).

Last updated