Git: How to squash all commits on branch

I make new branch from master with:

git checkout -b testbranch

  • Fix Git “object not found” for good
  • Changing the name of the committer in Xcode
  • Git PathSpec Issue on Git Stash
  • My diff contains trailing whitespace - how to get rid of it?
  • Can I use git to manage a website even if other people update it without using git?
  • How to untrack all deleted files in Git
  • 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

  • Find commit with the smallest diff
  • git rebase --onto results on single commit
  • Is it possible to show if git branches have the same commits if the one of the branches has been rebased so they don't have the same SHA1 IDs?
  • git- strange characters in output (msys-git)
  • svn: replace trunk with branch
  • Do web interfaces like GitHub garbage collect dangling remote git commits, and if not is it possible to fetch them?
  • 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.