Break a previous commit into multiple commits

前端 未结 14 2059
渐次进展
渐次进展 2020-12-20 13:09

Without creating a branch and doing a bunch of funky work on a new branch, is it possible to break a single commit into a few different commits after it\'s been committed to

相关标签:
14条回答
  • 2020-12-20 13:46

    I think that the best way i use git rebase -i. I created a video to show the steps to split a commit: https://www.youtube.com/watch?v=3EzOz7e1ADI

    0 讨论(0)
  • 2020-12-20 13:48

    git rebase -i will do it.

    First, start with a clean working directory: git status should show no pending modifications, deletions, or additions.

    Now, you have to decide which commit(s) you want to split.

    A) Splitting the most recent commit

    To split apart your most recent commit, first:

    $ git reset HEAD~
    

    Now commit the pieces individually in the usual way, producing as many commits as you need.

    B) Splitting a commit farther back

    This requires rebasing, that is, rewriting history. To find the correct commit, you have several choices:

    • If it was three commits back, then

      $ git rebase -i HEAD~3
      

      where 3 is how many commits back it is.

    • If it was farther back in the tree than you want to count, then

      $ git rebase -i 123abcd~
      

      where 123abcd is the SHA1 of the commit you want to split up.

    • If you are on a different branch (e.g., a feature branch) that you plan to merge into master:

      $ git rebase -i master
      

    When you get the rebase edit screen, find the commit you want to break apart. At the beginning of that line, replace pick with edit (e for short). Save the buffer and exit. Rebase will now stop just after the commit you want to edit. Then:

    $ git reset HEAD~
    

    Commit the pieces individually in the usual way, producing as many commits as you need, then

    $ git rebase --continue
    
    0 讨论(0)
提交回复
热议问题