Squash my last X commits together using Git

前端 未结 30 3460
醉酒成梦
醉酒成梦 2020-11-21 05:17

How can I squash my last X commits together into one commit using Git?

30条回答
  •  遇见更好的自我
    2020-11-21 05:50

    In the branch you would like to combine the commits on, run:

    git rebase -i HEAD~(n number of commits back to review)
    

    example:

    git rebase -i HEAD~1
    

    This will open the text editor and you must switch the 'pick' in front of each commit with 'squash' if you would like these commits to be merged together. From documentation:

    p, pick = use commit

    s, squash = use commit, but meld into previous commit

    For example, if you are looking to merge all the commits into one, the 'pick' is the first commit you made and all future ones (placed below the first) should be set to 'squash'. If using vim, use :x in insert mode to save and exit the editor.

    Then to continue the rebase:

    git rebase --continue
    

    For more on this and other ways to rewrite your commit history see this helpful post

提交回复
热议问题