Search code examples
gitsquash

want to automatically squash 20 commits


My topic branch is about 20 commits ahead of where I branched from.

I would like to automatically squash all these commits into the first commit after the branch.

I know I can do this interactively with rebase, but I don't want to interact, just fire off the command.


Solution

  • You could do it this way:

    git reset --soft HEAD~20
    git commit -m "massive commit"
    
    • The first command moves your topic branch 20 commits back, but preserves the changes in the index (and the working tree)
    • Then you create a single commit with all the changes.

    A quick way to find the exact commit you want to reset to is to do

    git merge-base <topicbranch> <otherbranch>
    

    If the output of that is <commit-ish>, you can then do

    git reset --soft <commit-ish>