I make new branch from master
with:
git checkout -b testbranch
I make 20 commits into it.
Now I want to squash those
Another solution would be to save all commit logs to a file
git log > branch.log
Now branch.log will have all commit ids since beginning.. scroll down and take the first commit (this will be difficult in terminal) using the first commit
git reset --soft
all commits will be squashed
Another simple way to do this: go on the origin branch and do a merge --squash
. This command doesn't do the "squashed" commit. when you do it, all commit messages of yourBranch will be gathered.
$ git checkout master
$ git merge --squash yourBranch
$ git commit # all commit messages of yourBranch in one, really useful
> [status 5007e77] Squashed commit of the following: ...
Since I had some trouble with the solutions proposed here, I want to share a really simple solution (which really works regardless):
git merge origin/master && git reset --soft origin/master
The preceding merge cmd ensures, that no recent changes from master will go on your head (inverted) when committing! After that, just commit the changes and do git push -f
Checkout the branch for which you would like to squash all the commits into one commit. Lets say its called feature_branch
.
git checkout feature_branch
Do a soft reset of your origin/feature_branch
with your local master
branch (depending on your needs, you can reset with origin/master as well). This will reset all the extra commits in your feature_branch
, but without changing any of your file changes locally.
git reset --soft master
Add all of the changes in your git repo directory, to the new commit that is going to be created. And commit the same with a message.
git add -A && git commit -m "commit message goes here"
Git reset, as mentioned in many answers before, is by far the best and simplest way to achieve what you want. I use it in the following workflow:
(on development branch)
git fetch
git merge origin/master #so development branch has all current changes from master
git reset origin/master #will show all changes from development branch to master as unstaged
git gui # do a final review, stage all changes you really want
git commit # all changes in a single commit
git branch -f master #update local master branch
git push origin master #push it
Based on reading several Stackoverflow questions and answers on squashing, I think this is a good one liner to squash all commits on a branch:
git reset --soft $(git merge-base master YOUR_BRANCH) && git commit -am "YOUR COMMIT MESSAGE" && git rebase -i master
This is assuming master is the base branch.