Git: How to squash all commits on branch

前端 未结 13 1924
情深已故
情深已故 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 05:01

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


    EDIT: you will need to use git push --force


    Karlotcha Hoa adds in the comments:

    For the reset, you can do

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

    [That] automatically uses the branch you are currently on.
    And if you use that, you can also use an alias, as the command doesn't rely on the branch name.

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

    If you use JetBrains based IDE like IntelliJ Idea and prefare using GUI over command line:

    1. Go to Version control window (Alt + 9/Command + 9) - "Log" tab.
    2. Choose a point in the tree from which you created your branch
    3. Right click on it -> Reset current branch to here -> Pick Soft (!!!) (it's important for not to lose your changes)
    4. Push the Reset button in the bottom of the dialog window.

    That's it. You uncommited all your changes. Now if you'll make a new commit it will be squashed

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

    You can use tool I've created specifically for this task:

    https://github.com/sheerun/git-squash

    Basically you need to call git squash master and you're done

    0 讨论(0)
  • 2020-12-04 05:06

    Assuming you were branching from the master, you don't need to enter yourBranch into the reset step all the time:

    git checkout yourBranch
    git reset --soft HEAD~$(git rev-list --count HEAD ^master)
    git add -A
    git commit -m "one commit on yourBranch"
    

    Explanation:

    • git rev-list --count HEAD ^master counts the commits since you made your feature branch from the master, f.ex. 20.
    • git reset --soft HEAD~20 will make a soft reset of the last 20 commits. This leaves your changes in the files, but removes the commits.

    Usage:

    In my .bash_profile I have added an alias for gisquash to do this with one command:

    # squash all commits into one
    alias gisquash='git reset --soft HEAD~$(git rev-list --count HEAD ^master)'
    

    After reseting and committing you need to do a git push --force.

    Hint:

    If you're using Gitlab >= 11.0 you don't need to do this anymore as it has a squashing option when merging branches.

    0 讨论(0)
  • 2020-12-04 05:07

    All this git reset, hard, soft, and everything else mentioned here is probably working (it didn't for me) if you do the steps correctly and some sort of a genie.
    If you are the average Joe smo, try this:
    How to use git merge --squash?


    Saved my life, and will be my go to squash, been using this 4 times since I found out about it. Simple, clean and basically 1 comamnd. In short:


    If you are on a branch lets call it "my_new_feature" off develop and your pull request has 35 commits (or however many) and you want it to be 1.

    A. Make sure your branch is up to date, Go on develop, get latest and merge and resolve any conflicts with "my_new_feature"
    (this step really you should take as soon as you can all the time anyway)

    B. Get latest of develop and branch out to a new branch call it "my_new_feature_squashed"

    C. magic is here.
    You want to take your work from "my_new_feature" to "my_new_feature_squashed"
    So just do (while on your new branch we created off develop):
    git merge --squash my_new_feature

    All your changes will now be on your new branch, feel free to test it, then just do your 1 single commit, push, new PR of that branch - and wait for repeat the next day.
    Don't you love coding? :)

    0 讨论(0)
  • 2020-12-04 05:10

    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.

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