Skip to content

Instantly share code, notes, and snippets.

View grafoo's full-sized avatar
✌️

Manuel Graf grafoo

✌️
View GitHub Profile
@scmx
scmx / split-git-commit-with-orig-head.md
Last active January 9, 2024 09:37
How to split a Git commit and preserve message using ORIG_HEAD #git #commit #ORIG_HEAD https://twitter.com/albertarv/status/954389020758495233

How to split a Git commit and preserve message using ORIG_HEAD

A colleague told me he'd accidentally committed something. Now he wanted to remove it from the previous commit and then put in a new commit. This is called splitting a commit, and here's the best way I know of how to do it.

Solution

  1. git reset --soft HEAD~ Undo the last commit, but preserve "Changes to be committed"
  2. git reset <some/path> Exclude what you want
  3. git commit -C ORIG_HEAD Commit with the same message as before, which was stored in ORIG_HEAD

Example