Squash my last X commits together using Git

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

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

30条回答
  •  说谎
    说谎 (楼主)
    2020-11-21 05:43

    Based on Chris Johnsen's answer,

    Add a global "squash" alias from bash: (or Git Bash on Windows)

    git config --global alias.squash '!f(){ git reset --soft HEAD~${1} && git commit --edit -m"$(git log --format=%B --reverse HEAD..HEAD@{1})"; };f'
    

    ... or using Windows' Command Prompt:

    git config --global alias.squash "!f(){ git reset --soft HEAD~${1} && git commit --edit -m\"$(git log --format=%B --reverse HEAD..HEAD@{1})\"; };f"
    


    Your ~/.gitconfig should now contain this alias:

    [alias]
        squash = "!f(){ git reset --soft HEAD~${1} && git commit --edit -m\"$(git log --format=%B --reverse HEAD..HEAD@{1})\"; };f"
    


    Usage:

    git squash N
    

    ... Which automatically squashes together the last N commits, inclusive.

    Note: The resultant commit message is a combination of all the squashed commits, in order. If you are unhappy with that, you can always git commit --amend to modify it manually. (Or, edit the alias to match your tastes.)

提交回复
热议问题