Skip to main content

Rewriting a git commit

·160 words·1 min
Kostiantyn Lysenko
Author
Kostiantyn Lysenko

Sometimes you commit something nasty and need to rewrite it. Or split a commit into parts. Or whatever.

Use case: you want to remove some files from a commit you already made.

Find the commit
#

git log --pretty=format:'%h %ad | %s%d [%an]' --graph --date=short

Say it’s 4ca80f0.

Remove files from the commit
#

git rebase -i 4ca80f0~1

Replace pick with edit and save the file.

Unstage everything in that commit:

git reset HEAD^

Now all files are back to pre-commit state. Add, remove, or stage as needed.

To undo changes to a tracked file:

git checkout -- windows

For a new file, just delete it:

rm -rf windows

You can also split this into several commits.

Change the commit message
#

git commit --amend

Apply the rewrite
#

git rebase --continue

References
#


comments powered by Disqus