Daniel Roy Greenfeld

Daniel Roy Greenfeld

About | Articles | Books | Jobs | News | Tags

TIL: Change older git commit

Typically I try to avoid changing older commits in a pull request. My reasoning has a tiny bit to do with preserving history and lots to do with being resistant to trying new things. Finally I got around to looking this up today, it is easy to do.

WARNING: Do this on branches of main, not on the main branch itself. More on that at the end.

Find the commit

In our example the change we want to amend is 3 commits back, do this to find it:

git rebase -i HEAD~3

That will bring up a screen that looks like this in your text editor:

pick ec0fb5e0333c Configure omnitron galactic retroverter
pick 6e7f323681f1 Activate the anvil generator
pick 0001757cea8f Wind up clockwork battery
pick 849f7c453458 Solidify the electricity

# Rebase dd341c1572..64c2cbbd76 onto dd34dc1572 (4 commands)

On the third line, change the word, pick to edit. It should now look like this:

pick ec0fb5e0333c Configure omnitron galactic retroverter
pick 6e7f323681f1 Activate the anvil generator
edit 0001757cea8f Wind up clockwork battery # <-- changed line
pick 849f7c453458 Solidify the electricity

# Rebase dd341c1572..64c2cbbd76 onto dd34dc1572 (4 commands)

Save and close the file.

Make changes

Now make changes to code files and save them.

Commit and rebase

Run these commands to have git recognize the changes:

git commit --all --amend --no-edit
git rebase --continue

If you have a remote repo (aka github or gitlab) you will need to run git push --force .

About the warning

Before you do this understand this rewrites the SHA-1s from the point of change forward. Any old SHA-1 representatives of those commits are gone. Hence this should happen on a branch away from master, preferably in a pull request.

References



Tags: howto git TIL
← Back to home