Let's say you're working on the whizbang feature. You branch off of develop
to the new feature/whizbang
branch and start coding. You commit with various things along the way. You commit one time and realize you broke it, so you fix it and commit again. Several commits later it's all set, tested, and ready to merge into develop. How do you squish all those intermediate commits into one, finished commit?
From your feature/whizbang
branch, type git rebase -i develop
Then you'll get a rebase editor with some helpful comments as a cheatsheet:
The top one is the first commit after branching off of develop. We're going to modify the commit message to have the issue number and all that. change the word pick
to reword
or just r
.
The rest of them are commits you made after you branched off of develop. change the word pick
in front of each of them to fixup
or just f
. This means we are discarding their commit messages. It should now look like this:
Save and quit with :wq
. It will bring up another editor and prompt you to change the commit message for the r
commit:
Change the first line to something like my reworded commit message
, save, and quit with :wq
. Now checkout develop
in git and merge in feature/whizbang
. Then check the git log. You'll see that only the reworded commit is there and the extraneous ones are gone. Sweet!
Another way to do this is to check out develop, do git merge --squash feature-whizbang
, and commit with your final message.
- This doesn't fit into a pull request workflow because you're committing directly on develop.
- You'll have to use
git branch -D
to delete the feature-whizbang branch because git thinks it's not fully merged. - It squashes the commits in develop, but not in the original branch, which you may not want.
- Git doesn't know you merged in a feature branch this way, it just thinks you committed directly on develop. You might not like how this looks in your git graph - It looks like a branch was made and committed to but never merged back in.