HOW TO Amend files in older commit

I have a local branch, not merged or pushed to github. I made successive changes to the branch with non-overlapping commit items.

Branch->HEAD: File 3
Branch->HEAD~1: File 2
Branch->HEAD~2: File 1

I found an issue in File 1 and edited it. Can I somehow use git commit --amend to change HEAD~2 without upsetting the two later commits? I don’t want to have a new small HEAD commit. Some version of git checkout or git rebase perhaps?

Since this is only a local branch, not pushed, I don’t care about the commit history being changed.


git rebase -i HEAD~3 is probably what you’re looking for. The base commit for the rebase must be the parent of the first one you want to edit. The option -i (short for --interactive) lets you choose what to do to each commit since, with edit being one option.

See Git - Rewriting History for a lot more details. :slightly_smiling_face:

1 Like

EDIT: Just read your link. Looks like that will do the trick. Thank you. Will post update.

After that, how do I return to the current HEAD? Are the following changes preserved? Rebase had always made me nervous :slight_smile:


1 Like

I committed the change to be merged with HEAD~2. Did the git rebase -i HEAD~3 command and reordered and squashed the two commits. It looks like it worked. Did a backup of the local repo anyway just in case. Thanks.

1 Like