This gives a good explanation of squashing multiple commits:
http://git-scm.com/book/en/Git-Branching-Rebasing
but it does not work for commits that have already been pushed. How do I squash the most recent few commits both in my local and remote repos?
When I do git rebase -i origin/master~4 master
, keep the first one as pick
, set the other three as squash
, and then exit (via c-x c-c in emacs), I get:
$ git rebase -i origin/master~4 master # Not currently on any branch. nothing to commit (working directory clean) Could not apply 2f40e2c... Revert "issue 4427: bpf device permission change option added" $ git rebase -i origin/master~4 master Interactive rebase already started
where 2f40 is the pick
commit. And now none of the 4 commits appear in git log
. I expected my editor to be restarted so that I could enter a commit message. What am I doing wrong?
The first one is to use the git merge command with the squash flag (two dashes there). And the second one is through an interactive rebase. The first option (merge) is very simple to perform. It's clean and fast, but it gives you almost no control on what you want to do.
Squashing commitsYou have 4 local commits (to your devel branch) that you want to "squash" into one (or more) nice, clean commit(s) before pushing to the remote repository. For example: You might have checkpointed your work making a trivial comment. You want to revise one or more commit messages.
Squash commits locally with
git rebase -i origin/master~4 master
and then force push with
git push origin +master
--force
and +
From the documentation of git push
:
Note that
--force
applies to all the refs that are pushed, hence using it withpush.default
set tomatching
or with multiple push destinations configured withremote.*.push
may overwrite refs other than the current branch (including local refs that are strictly behind their remote counterpart). To force a push to only one branch, use a+
in front of the refspec to push (e.ggit push origin +master
to force a push to themaster
branch).
On a branch I was able to do it like this (for the last 4 commits)
git checkout my_branch git reset --soft HEAD~4 git commit git push --force origin my_branch
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With