How to split last commit into two in Git

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

    Run git gui, select the "Amend last commit" radio button, and unstage (Commit > Unstage From Commit, or Ctrl-U) changes that you do not want to go into first commit. I think that's the easiest way to go about it.

    Another thing you could do is cherry-pick the change without committing (git cherry-pick -n) and then either manually or with git gui select desired changes before committing.

    0 讨论(0)
  • 2020-12-07 07:18

    This might be another solution targeted for cases where there is a huge commit and a small amount of files needs to be moved into a new commit. This will work if a set of <path> files are to be extracted out of the last commit at HEAD and all moved to a new commit. If multiple commits are needed the other solutions can be used.

    First make patches into the staged and unstaged areas that would contain the changes to revert the code to before modification and after modification respectively:

    git reset HEAD^ <path>
    
    $ git status
    On branch <your-branch>
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
    
            modified:   <path>
    
    Changes not staged for commit:
      (use "git add <file>..." to update what will be committed)
      (use "git checkout -- <file>..." to discard changes in working directory)
    
            modified:   <path>
    

    To understand what's gonna happen (arrow and comments are not part of command):

    git diff --cached   -> show staged changes to revert <path> to before HEAD
    git diff            -> show unstaged changes to add current <path> changes
    

    Revert <path> changes in last commit:

    git commit --amend  -> reverts changes on HEAD by amending with staged changes
    

    Create new commit with <path> changes:

    git commit -a -m "New Commit" -> adds new commit with unstaged changes
    

    This has the effect of creating a new commit containing the changes extracted out of the last commit.

    0 讨论(0)
  • 2020-12-07 07:20

    Since you're cherry-picking, you can:

    1. cherry-pick it with --no-commit option added.
    2. reset and use add --patch, add --edit or just add to stage what you want to keep.
    3. commit the staged changes.
      • To re-use original commit message, you can add --reuse-message=<old-commit-ref> or --reedit-message=<old-commit-ref> options to the commit command.
    4. Blow away unstaged changes with reset --hard.

    Another way, preserving or editing the original commit message:

    1. cherry-pick the original commit as normal.
    2. Reverse the changes you don't want and use add to stage the reversal.
      • This step would be easy if you are removing what you added, but a bit tricky if you're adding what you removed or reversing a change.
    3. commit --amend to effect the reversal on the cherry-picked commit.
      • You'll get the same commit message again, which you can keep or revise as necessary.
    0 讨论(0)
  • 2020-12-07 07:28
    git reset HEAD^
    

    the --hard is what's killing your changes.

    0 讨论(0)
提交回复
热议问题