Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: How to squash all commits on branch

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 
like image 637
user3803850 Avatar asked Aug 18 '14 05:08

user3803850


People also ask

How do you squash multiple commits in the same branch?

Git's squash commits command There is no explicit Git squash command. Instead, to squash git commits, the interactive git rebase is used. To squash all commits on a single branch, the interactive git rebase command must be passed one of two arguments: the id of the commit from which the branch split from its parent.

How do you squash all commits in a branch?

Squashing by Merging With the –squash Option This can effectively clean the commit-graph in a branch. However, we sometimes make many commits in our feature branch while working on it. After we've developed the feature, we usually want to merge the feature branch to the main branch, say “master”.


2 Answers

Another way to squash all your commits is to reset the index to master:

git checkout yourBranch git reset $(git merge-base master $(git branch --show-current)) git add -A git commit -m "one commit on yourBranch" 

This isn't perfect as it implies you know from which branch "yourBranch" is coming from.
Note: finding that origin branch isn't easy/possible with Git (the visual way is often the easiest, as seen here).

Note: git branch --show-current has been introduced with Git 2.22 (Q1 20219).


EDIT: you will need to use git push --force


Karlotcha Hoa adds in the comments:

For the reset, you can do

git reset $(git merge-base master $(git rev-parse --abbrev-ref HEAD))  

[That] automatically uses the branch you are currently on.
And if you use that, you can also use an alias, as the command doesn't rely on the branch name.


sschoof adds in the comments:

Since my default branch is called main and my search had multi times brought me here:
To copy it for my next time

git reset $(git merge-base main $(git rev-parse --abbrev-ref HEAD)) 
like image 180
VonC Avatar answered Sep 19 '22 20:09

VonC


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 

Step 1:

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 

Step 2:

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.

# Add files for the commit. git add ... git commit -m "commit message goes here" 
like image 36
shanky Avatar answered Sep 18 '22 20:09

shanky