Git: How to squash all commits on branch

I make new branch from master with:

git checkout -b testbranch

  • Fix history after forced push
  • Merge 2 Different git Repos
  • Moving a git repo to a second computer?
  • git fsck --full only checking directories
  • Want to download a Git repository, what do I need (windows machine)?
  • Best workflow for developing npm packages that depend on each other
  • 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

  • Docker run command from git repo?
  • DVCS, Databases, and User Generated Content?
  • git turn off “LF will be replaced by CRLF” warning
  • Showing diff's in the commit message
  • git: cumulative diff with commit-limiting
  • github: No supported authentication methods available
  • 3 Solutions collect form web for “Git: How to squash all commits on branch”

    Another way to squash all your commits is 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"
    

    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).

    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.

    Another simple way to do this: go on the origin branch and do a merge --squash. This command don’t do the “squased” 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: ...
    
    Git Baby is a git and github fan, let's start git clone.