How can I split up a Git commit buried in history?

前端 未结 6 857
刺人心
刺人心 2020-11-28 17:15

I flubbed up my history and want to do some changes to it. Problem is, I have a commit with two unrelated changes, and this commit is surrounded by some other changes in my

6条回答
  •  一整个雨季
    2020-11-28 17:35

    There's a faster version if you only want to extract content from just one file. It's faster because the interactive rebase is not actually interactive anymore (and it's of course even faster if you want to extract from the last commit, then no need to rebase at all)

    1. Use your editor and delete the lines you want to extract from the_file. Close the_file. That's the only edition you need, all the rest is just git commands.
    2. Stage that deletion in the index:

      git  add  the_file
      
    3. Restore the lines you just deleted back into the file without affecting the index!

      git show HEAD:./the_file > the_file
      
    4. "SHA1" is the commit you want to extract the lines from:

      git commit -m 'fixup! SHA1' 
      
    5. Create the second, brand new commit with the content to extract restored by step 3:

      git commit -m 'second and new commit' the_file 
      
    6. Don't edit, don't stop/continue - just accept everything:

      git rebase --autosquash -i SHA1~1
      

    Of course even faster when the commit to extract from is the last commit:

    4. git commit -C HEAD --amend
    5. git commit -m 'second and new commit' thefile
    6. no rebase, nothing
    

    If you use magit then step 4, 5 and 6 are a single action: Commit, instant Fixup

提交回复
热议问题