DevOp's Blog

jakshi's devop blog.

Rewriting Git Commit

Sometimes when you commit something nasty, you need to rewrite it. Or you may need to split commit into parts. Or whatever.

Use case: You want to remove some files that you added with your commit.

Find the commit

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

Let it be 4ca80f0.

Remove files that you don’t want in the commit.

1
git rebase -i 4ca80f0~1

Replace pick with edit and save file.

Then you need to unstage files in the commit:

1
git reset HEAD^

now all files are in state before that commit, and you can add/remove/stage them.

to undo windows file changes:

1
git checkout -- windows

or if this is a new file you can just delete it

1
rm -rf windows

also you can split this commit into several commits.

Change commit message

Now you probably want to change commit message:

1
git commit --amend

Apply changes

1
git rebase --continue

Profit!

Enjoy you splitted/amended commit.

References

Comments