git remove merge commit from history

后端 未结 4 1739
再見小時候
再見小時候 2020-11-29 15:55

My Git history looks like that :

\"Git

I would like to squash the purple commits into a singl

4条回答
  •  执笔经年
    2020-11-29 16:37

    Starting with the repo in the original state

    To remove the merge commit and squash the branch into a single commit in the mainline

    Use these commands (replacing 5 and 1 with the SHAs of the corresponding commits):

    git checkout 5
    git reset --soft 1
    git commit --amend -m '1 2 3 4 5'
    git rebase HEAD master
    

    To retain a merge commit but squash the branch commits into one:

    Use these commands (replacing 5, 1 and C with the SHAs of the corresponding commits):

    git checkout -b tempbranch 5
    git reset --soft 1
    git commit --amend -m '1 2 3 4 5'
    git checkout C
    git merge --no-ff tempbranch
    git rebase HEAD master
    

    To remove the merge commit and replace it with individual commits from the branch

    Just do (replacing 5 with the SHA of the corresponding commit):

    git rebase 5 master
    

    And finally, to remove the branch entirely

    Use this command (replacing C and D with the SHAs of the corresponding commits):

    git rebase --onto C D~ master
    

提交回复
热议问题