How to split last commit into two in Git

前端 未结 10 515
囚心锁ツ
囚心锁ツ 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条回答
  •  Happy的楠姐
    2020-12-07 07:03

    To change the current commit into two commits, you can do something like the following.

    Either:

    git reset --soft HEAD^
    

    This undoes the last commit but leaves everything staged. You can then unstage certain files:

    git reset -- file.file
    

    Optionally restage parts of those files:

    git add -p file.file
    

    Make a new first commit:

    git commit
    

    The stage and commit the rest of the changes in a second commit:

    git commit -a
    

    Or:

    Undo and unstage all of the changes from the last commit:

    git reset HEAD^
    

    Selectively stage the first round of changes:

    git add -p
    

    Commit:

    git commit
    

    Commit the rest of the changes:

    git commit -a
    

    (In either step, if you undid a commit that added a brand new file and want to add this to the second commit you'll have to manually add it as commit -a only stages changes to already tracked files.)

提交回复
热议问题