How to split last commit into two in Git

前端 未结 10 519
囚心锁ツ
囚心锁ツ 2020-12-07 06:23

I have two working branches, master and forum and I\'ve just made some modifications in forum branch, that I\'d like to ch

10条回答
  •  死守一世寂寞
    2020-12-07 07:04

    You can use git rebase -i , where is the latest commit you want to keep as-is. Add a break at each point where you would like to insert a new split-out commit. Then at each break, use git checkout -p to pull in the parts you want to split out, and commit them. Then git rebase --continue to remove those parts from later commits.

    For the simple case of splitting the most recent commit, this looks like:

    $ git rebase -i HEAD^
    # add 'break' at the start
    
    $ git checkout -p master # or whatever your branch is called
    # choose the parts you want to split out
    
    $ git commit
    # commit the newly-split-out parts
    
    $ git rebase --continue
    # rebase the remaining parts of the change onto the split-out parts
    

    This assumes you want the later commit to retain the original commit message; that's the situation I usually find myself in (factoring out some preparatory change).

提交回复
热议问题