First check with git branch
, what's your current working branch. Next, if it isn't featureX
already, switch to it, using git checkout featureX
.
We'll now figure out how many commits there where that you want to squash into one. For this we'll use git log
. To get a bit nicer view of it, use git log --graph --decorate --pretty=oneline --abbrev-commit
.
Next to do the actual squashing: git rebase -i HEAD~<NUMBER OF COMMITS TO SQUASH>
or alternatively use the SHA hash git rebase -i <SHA>
. If you need help with this step, here are two pretty nice articles: https://thoughtbot.com/blog/git-interactive-rebase-squash-amend-rewriting-history and https://medium.com/@dirk.avery/the-definitive-git-rebase-guide-dbd7717f9437
Now we want to make sure, we've got the latest version of the master
branch. git checkout master
followed by git pull origin master
(assuming you use origin as upstream) and git checkout featureX
.
Finally we can rebase featureX
onto master
using git rebase master
and can push featureX
to GitHub, GitLab or where ever we please. git push origin featureX
.
Note: if you've previously pushed featureX
, you'll need to force push git push origin featureX --force
. Make sure to only do this on branches you're working on, that aren't shared, otherwise people will get into troubles.
An article explaining the whole rebase and squash stuff to keep the commit history clean pretty good can be found here: https://blog.carbonfive.com/2017/08/28/always-squash-and-rebase-your-git-commits/