One of my favorite git tricks, this allows you to amend an old commit when you realize there's small changes needed, like fixing a typo or a small bug.
Note that this should only be done on a branch, or for commits which you haven't yet pushed, as this is re-writing history. Thus, if you've already pushed the commit you're editing, you'll need to force-push them the second time.
Steps are:
Make the changes
Stage the changes
Find the sha of the commit you want to add the staged changes to (referred to as [sha])
git commit --fixup [sha] – this creates a 'fixup' commit.
Optional: if you have other unstaged changes, you'll need to stash or commit them before the next step.
git rebase -i --autosquash ^[sha] – the upwards-caret indicates you want to rebase up to the commit before the original commit you're fixing up.
Note that if you're fixing up multiple commits simultaneously, the final 'autosquash' rebase will need to be of the oldest commit being fixed up.
Also note that this can potentially cause merge conflicts if the changes aren't small and conflict with other intervening changes. So use sparingly.
One of my favorite git tricks, this allows you to amend an old commit when you realize there's small changes needed, like fixing a typo or a small bug.
Note that this should only be done on a branch, or for commits which you haven't yet pushed, as this is re-writing history. Thus, if you've already pushed the commit you're editing, you'll need to force-push them the second time.
Steps are:
[sha]
)git commit --fixup [sha]
– this creates a 'fixup' commit.git rebase -i --autosquash ^[sha]
– the upwards-caret indicates you want to rebase up to the commit before the original commit you're fixing up.Note that if you're fixing up multiple commits simultaneously, the final 'autosquash' rebase will need to be of the oldest commit being fixed up.
Also note that this can potentially cause merge conflicts if the changes aren't small and conflict with other intervening changes. So use sparingly.
Good article here: https://thoughtbot.com/blog/autosquashing-git-commits