Sometimes we find ourselves in a situation where we’ve been working on a feature locally and have a lot of local commits that look slightly messy.
For example, commits may have been done for typing, multiple styling commits, and tests that failed and that were later refactored to work. There are many reasons why you might have a local commit history that you don’t want to have added to the remote branch you’re working on.
To keep the branch history clean and easy to read, you can rewrite your local commit history before pushing your changes to the remote branch.
Firstly, let’s get a list of our local commits. There are a few ways to do this, we’re just going to compare where we are with the local HEAD (note I’m working on a branch called develop
, you’ll need to update that with your branch name):
git log origin/develop..HEAD
So as we can see there are four local commits, shown above in order from the latest to the oldest commit. In our scenario, we are using the Conventional Commits specification (more on that in a separate post soon). The first commit aligns with that specification, but the last three don’t. So we have three commits to remove and then we can add a new commit message.
It’s worth noting that there are multiple ways of doing this and people have opinions on the “best” or “correct” way of achieving the same result. I’ve always found this way the easiest when you have successive local commits to change.
You can see here that the command is a simple one-liner
git reset --soft HEAD~3
You can see that the three latest commits have now been removed, leaving the earlier one we were happy with. The changes in those removed commits are still staged, so you won’t need to run git add
again. You can now create a new commit and push your changes, for example:
git commit -m "docs: Update CONTRIBUTING.md with guidelines as well as a worked example"
And that’s it!