Git: How to squash all commits on branch

1.6K    Asked by RogerLum in Devops , Asked on Jul 1, 2021

I make a new branch from the 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
Answered by Kirsty Deller

For git squash all commits in branch and to reset the index to master:

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

Note: finding that origin branch isn't easy/possible with Git.

but the above method is not a perfect one because it has a list, where the branch is coming.

so there is a way for that you will need to use:

     git push --force

For the reset, you can do

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

it automatically uses the branch you are currently on.



Your Answer

Interviews

Parent Categories