How to amend older Git commit?

This question already has an answer here:

  • How to modify a specified commit in git? 10 answers

  • git rebase -i HEAD^^^
    

    Now mark the ones you want to amend with edit or e (replace pick ). Now save and exit.

    Now make your changes, then

    git add -A
    git commit --amend --no-edit
    git rebase --continue
    

    If you want to add an extra delete remove the options from the commit command. If you want to adjust the message, omit just the --no-edit option.


    I prepared my commit that I wanted to amend with an older one and was surprised to see that rebase -i complained that I have uncommitted changes. But I didn't want to make my changes again specifying edit option of the older commit. So the solution was pretty easy and straightforward:

  • prepare your update to older commit, add it and commit
  • git rebase -i <commit you want to amend>^ - notice the ^ so you see the said commit in the text editor
  • you will get sometihng like this:

    pick 8c83e24 use substitution instead of separate subsystems file to avoid jgroups.xml and jgroups-e2.xml going out of sync
    pick 799ce28 generate ec2 configuration out of subsystems-ha.xml and subsystems-full-ha.xml to avoid discrepancies
    pick e23d23a fix indentation of jgroups.xml
    
  • now to combine e23d23a with 8c83e24 you can change line order and use squash like this:

    pick 8c83e24 use substitution instead of separate subsystems file to avoid jgroups.xml and jgroups-e2.xml going out of sync    
    squash e23d23a fix indentation of jgroups.xml
    pick 799ce28 generate ec2 configuration out of subsystems-ha.xml and subsystems-full-ha.xml to avoid discrepancies
    
  • write and exit the file, you will be present with an editor to merge the commit messages. Do so and save/exit the text document

  • You are done, your commits are amended
  • credit goes to: http://git-scm.com/book/en/Git-Tools-Rewriting-History There's also other useful demonstrated git magic.


    You could can use git rebase to rewrite the commit history. This can be potentially destructive to your changes, so use with care.

    First commit your "amend" change as a normal commit. Then do an interactive rebase starting on the parent of your oldest commit

    git rebase -i 47175e84c2cb7e47520f7dde824718eae3624550^
    

    This will fire up your editor with all commits. Reorder them so your "amend" commit comes below the one you want to amend. Then replace the first word on the line with the "amend" commit with s which will combine ( s quash) it with the commit before. Save and exit your editor and follow the instructions.

    链接地址: http://www.djcxy.com/p/23420.html

    上一篇: 修改一个不是以前提交的提交

    下一篇: 如何修改旧的Git提交?