I make new branch from master
with:
git checkout -b testbranch
I make 20 commits into it.
Now I want to squash those 20 commits. I do that with:
git rebase -i HEAD~20
What about if I don't know how many commits? Is there any way to do something like:
git rebase -i all on this branch
This question is related to
git
version-control
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
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
What you're doing is pretty error-prone. Just do:
git rebase -i master
which will automatically rebase only your branch's commits onto the current latest master.
All this git reset, hard, soft, and everything else mentioned here is probably working (it didn't for me) if you do the steps correctly and some sort of a genie.
If you are the average Joe smo, try this:
How to use git merge --squash?
Saved my life, and will be my go to squash, been using this 4 times since I found out about it. Simple, clean and basically 1 comamnd.
In short:
If you are on a branch lets call it "my_new_feature" off develop and your pull request has 35 commits (or however many) and you want it to be 1.
A. Make sure your branch is up to date, Go on develop, get latest and merge and resolve any conflicts with "my_new_feature"
(this step really you should take as soon as you can all the time anyway)
B. Get latest of develop and branch out to a new branch call it
"my_new_feature_squashed"
C. magic is here.
You want to take your work from "my_new_feature" to "my_new_feature_squashed"
So just do (while on your new branch we created off develop):
git merge --squash my_new_feature
All your changes will now be on your new branch, feel free to test it, then just do your 1 single commit, push, new PR of that branch - and wait for repeat the next day.
Don't you love coding? :)
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.
You can use tool I've created specifically for this task:
https://github.com/sheerun/git-squash
Basically you need to call git squash master
and you're done
Solution for people who prefer clicking:
Install sourcetree (it is free)
Check how your commits look like. Most likely you have something similar to this
Right click on parent commit. In our case it is master branch.
You can squash commit with previous one by clicking a button. In our case we have to click 2 times. You can also change commit message
Side note: If you were pushing your partial commits to remote you have to use force push after squash
Checkout the branch for which you would like to squash all the commits into one commit. Let's say it's called feature_branch
.
git checkout feature_branch
Do a soft reset of your origin/feature_branch
with your local main
branch (depending on your needs, you can reset with origin/main 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 main
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"
In case you are okay with an answer involving another branch, try git checkout --orphan <new_branch>
It allowed me to simply commit ALL files from previous branch as one commit.
This is something like a git merge squash but not quite the same.
Assuming you were branching from the master, you don't need to enter yourBranch
into the reset step all the time:
git checkout yourBranch
git reset --soft HEAD~$(git rev-list --count HEAD ^master)
git add -A
git commit -m "one commit on yourBranch"
Explanation:
git rev-list --count HEAD ^master
counts the commits since you made your feature branch from the master, f.ex. 20.git reset --soft HEAD~20
will make a soft reset of the last 20 commits. This leaves your changes in the files, but removes the commits.Usage:
In my .bash_profile I have added an alias for gisquash
to do this with one command:
# squash all commits into one
alias gisquash='git reset --soft HEAD~$(git rev-list --count HEAD ^master)'
After reseting and committing you need to do a git push --force
.
Hint:
If you're using Gitlab >= 11.0 you don't need to do this anymore as it has a squashing option when merging branches.
If you use JetBrains based IDE like IntelliJ Idea and prefare using GUI over command line:
That's it. You uncommited all your changes. Now if you'll make a new commit it will be squashed
Although this answer is a bit verbose it does allow you to create a single commit branch by doing a single merge with another branch. (No rebasing/squashing/merging individual commits).
Since we don't care about the in-between commits we can use a simple solution:
# Merge and resolve conflicts
git merge origin/master
# Soft reset and create a HEAD with the version you want
git reset --soft origin/master
git commit -m "Commit message"
git push origin your-branch -f
... then to remove the history...
# Get the most up-to-date master
git checkout master
git reset --hard
# Create a temporary branch
git checkout -b temp-branch
# Retrieve the diff between master and your-branch and commit with a single commit
git checkout origin/your-branch
git commit -m "Feature commit"
# Force push to the feature branch
git push origin temp-branch:your-branch -f
# Clean up
git checkout your-branch
git branch -D temp-branch
This can all be put into a bash function with 3 params like so squashall --their master --mine your-branch --msg "Feature commit"
and it will work as long as you have the correct version of files locally.
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: ...
Source: Stackoverflow.com