This post is a note for myself when dealing with git. It shows you the way to go back and change your previous commit without losing any of the new work you’ve done since you commited. Source of this post can be found at this link
- Save and stash your work so far.
- Look at
git log
and copy the first 5 or so characters from the ID of the commit you want to edit onto your clipboard. - Start the interactive rebase process, pasting in the characters from the ID:
git rebase --interactive ID
- Your editor will come up with several lines like*
pick d3adb33 Commit message
*, one line for each commit since the older one. - Change the word “pick” to “edit” in front of the commit you want to change.
- Save and quit.
- Edit your project files to make the correction, then run
git<em><strong> commit --all --amend</strong></em>
. - After you’ve committed the fixed version, do
git rebase --continue
. - Git will do its magic, recreating all the commits since then.
- You might need to resolve some conflicts, if the change you made affected later commits. Follow Git’s instructions to resolve those.
- Once the rebase is done, re-apply the stash and continue happily with your life.