Skip to content

Instantly share code, notes, and snippets.

@barbietunnie
Created May 18, 2022 18:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barbietunnie/3553d0b278378904572288350b9a18a7 to your computer and use it in GitHub Desktop.
Save barbietunnie/3553d0b278378904572288350b9a18a7 to your computer and use it in GitHub Desktop.
Rollback git commit to an old state

Rollback git commit to an old state

To roll back to a previous state, you can inspect the history to see which commit you are interested in

# view commit history
git log

# checkout the desired commit
git checkout <commit-id>

This switches you to a 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you create, you may do so (now or later) by using -c with the switch command. Example:

git switch -c <new-branch-name>

Or undo this operation with:

git switch -

You can turn off this advice by setting config variable advice.detachedHead to false

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment