How to split last commit into two in Git

前端 未结 10 535
囚心锁ツ
囚心锁ツ 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:07

    Goals:

    • I want to split a past commit (splitme) into two.
    • I want to maintain the commit message.

    Plan:

    1. rebase interactive from one before splitme.
    2. edit splitme.
    3. Reset the files to split into a second commit.
    4. Amend commit, maintaining message, modify as necessary.
    5. Add back the files split out from the first commit.
    6. Commit with a new message.
    7. Continue rebase.

    The rebase steps (1 & 7) can be skipped if the splitme is the most recent commit.

    git rebase -i splitme^
    # mark splitme commit with 'e'
    git reset HEAD^ -- $files
    git commit --amend
    git add $files
    git commit -m "commit with just some files"
    git rebase --continue
    

    If I wanted the split files to be committed first, I'd then rebase -i again and switch the order

    git rebase -i splitme^
    # swap order of splitme and 'just some files'
    

提交回复
热议问题