-
Notifications
You must be signed in to change notification settings - Fork 72
Git commit history rebasing and squashing
Why squash commits and take care of your commit history?
git checkout master
git fetch origin
git merge origin master
git checkout dev_branch_to_squash
git reset master
git add .
git commit -m "blabla"
git push -f origin dev_branch_to_squash
##Explained Make sure your repo is up to date:
git checkout master
git fetch origin
git merge origin master
Switch to the branch you want to squash:
git checkout dev_branch_to_squash
Let dev_branch_to_squash branch point to master:
git reset master
This leaves are your changes you did on dev_test as changes in your working directory. Now you can choose what to add and create the commits you want. If you want to create one commit with all your changes, to this:
git add .
git commit -m "blabla"
Now you can force push.
Make sure your repo is up to date:
git checkout master
git pull origin
git merge origin master
Switch to the branch you want to squash:
git checkout dev_test
Start a git rebase in interactive mode:
git rebase -i origin/master
This will open your editor. In this editor, you can pick the commits you want to squash (you can do many other things, as it's described in the file that is opened).
Mark the commits you want to squash with s
, save and leave the dialog. Then, you will be able to edit the commit messages. If you have conflicts, handle them as you would any conflicts during a rebase.
After that, you have to force push the branch to github with
git push -f origin dev_test
Alternativs are provide at this link: http://makandracards.com/makandra/527-squash-several-git-commits-into-a-single-commit
Some comments on the link above:
-
the 'easy mode' does squash your commits directly onto master. This is not what we want usually. But you can create a new branch after
git reset origin/master
and have all changes in one commit on this branch. This will create a new branch, which you can push and create a PR with. -
the 'hard mode' is basically what I was describing above. Please note that you shouldn't do the last command (
git merge squashed_feature
) because we don't want to merge into master locally, but only through PR on github.