Git: How to squash all commits on branch

前端 未结 13 1922
情深已故
情深已故 2020-12-04 04:33

I make new branch from master with:

git checkout -b testbranch

I make 20 commits into it.

Now I want to squash those

相关标签:
13条回答
  • 2020-12-04 04:45

    Another solution would be to save all commit logs to a file

    git log > branch.log

    Now branch.log will have all commit ids since beginning.. scroll down and take the first commit (this will be difficult in terminal) using the first commit

    git reset --soft

    all commits will be squashed

    0 讨论(0)
  • 2020-12-04 04:53

    Another simple way to do this: go on the origin branch and do a merge --squash. This command doesn't do the "squashed" 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: ...
    
    0 讨论(0)
  • 2020-12-04 04:53

    Since I had some trouble with the solutions proposed here, I want to share a really simple solution (which really works regardless):

    git merge origin/master && git reset --soft origin/master
    

    The preceding merge cmd ensures, that no recent changes from master will go on your head (inverted) when committing! After that, just commit the changes and do git push -f

    0 讨论(0)
  • 2020-12-04 04:58

    Checkout the branch for which you would like to squash all the commits into one commit. Lets say its called feature_branch.

    git checkout feature_branch
    

    Step 1:

    Do a soft reset of your origin/feature_branch with your local master branch (depending on your needs, you can reset with origin/master 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 master
    

    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.

    git add -A && git commit -m "commit message goes here"
    
    0 讨论(0)
  • 2020-12-04 04:59

    Git reset, as mentioned in many answers before, is by far the best and simplest way to achieve what you want. I use it in the following workflow:

    (on development branch)

    git fetch
    git merge origin/master  #so development branch has all current changes from master
    git reset origin/master  #will show all changes from development branch to master as unstaged
    git gui # do a final review, stage all changes you really want
    git commit # all changes in a single commit
    git branch -f master #update local master branch
    git push origin master #push it
    
    0 讨论(0)
  • 2020-12-04 05:00

    Based on reading several Stackoverflow questions and answers on squashing, I think this is a good one liner to squash all commits on a branch:

    git reset --soft $(git merge-base master YOUR_BRANCH) && git commit -am "YOUR COMMIT MESSAGE" && git rebase -i master
    

    This is assuming master is the base branch.

    0 讨论(0)
提交回复
热议问题