Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save christophermark/9f9706cd1620aa7d0f9cd990f3febecf to your computer and use it in GitHub Desktop.

Select an option

Save christophermark/9f9706cd1620aa7d0f9cd990f3febecf to your computer and use it in GitHub Desktop.
Git reset but keep commit message
1. Prevent losing the message in the first place
If you know ahead of time that you want to “undo” a commit without losing either your changes or your commit message, use soft reset:
# Move HEAD one commit back, but keep index & working-tree untouched
git reset --soft HEAD~1
• What it does
• HEAD moves to the previous commit
• Your index (staging area) still contains exactly what you had in that commit
• Your working tree is untouched
At this point you can simply re-commit, reusing the old message:
# Re-commit, copying the original commit message:
git commit -c ORIG_HEAD
Here, ORIG_HEAD is the ref Git automatically sets to the pre-reset HEAD. The -C <commit> flag tells Git “reuse that commit’s message exactly.”
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment