How do I keep the commit message when editing commits via git rebase --interactive?

前端 未结 4 2077
北海茫月
北海茫月 2021-02-05 13:11

I\'m currently in the midst of a git rebase --interactive session, where I\'m editing a commit. I\'m proceeding as suggested by How can I split up a Git commit bur

相关标签:
4条回答
  • 2021-02-05 13:14

    Why follow instructions on commit splitting if you don't want it?

    You don't reset to HEAD^, this applies only when splitting commits. Just mark the commit for editing in rebase -i, make your changes, commit --amend and rebase --continue.

    0 讨论(0)
  • 2021-02-05 13:15

    While CharlesB's solution is correct and probably easier, the reason that the original poster is seeing the commit message before the commit he wants to edit is because he's using the --amend flag of git commit, which modifies the previous commit.

    Instead of using --amend to commit your changes, just use git commit without the flag, which won't touch the previous commit. You can also pass in an option to reuse the commit message from the commit that you reset using git reset head^:

    git commit --reuse-message=HEAD@{1}
    
    # Or use -C, which is the same thing, but shorter:
    git commit -C HEAD@{1}
    

    HEAD@{1} points to the commit you were at before you did git reset head^. You can also just pass in the sha id for that commit directly.

    From the git commit docs:

    -C <commit>
    --reuse-message=<commit>
    

    Take an existing commit object, and reuse the log message and the authorship information (including the timestamp) when creating the commit.

    Of course, like I said, CharlesB's solution is simpler, since if you don't do the first git reset head^, you can just make changes and amend the commit you're trying to modify directly, and you automatically get the previous commit message when you do git commit --amend, you don't have to pass in the commit sha for it.

    0 讨论(0)
  • 2021-02-05 13:27

    I have another answer that seems to be more fool-proof, though undocumented.

    $ git reset HEAD^
    $ git add ...               # stage the first part of your split
    $ git commit -m 'part one'
    $ git commit -a -F $(git rev-parse --git-dir)/rebase-merge/message
    
    0 讨论(0)
  • 2021-02-05 13:30

    I adapted some examples in git-commit(1) and git-reset(1):

    $ git reset HEAD^
    $ git add ...                 # stage the first part of your split
    $ git commit -m 'part one'
    $ git commit -a -c ORIG_HEAD  # start with the earlier commit message
    

    If you're worried about doing something that might change ORIG_HEAD, you could instead do:

    $ SAVED=$(git rev-parse HEAD)
    $ git reset HEAD^
    ...
    $ git commit -a -c $SAVED   # start with the earlier commit message
    
    0 讨论(0)
提交回复
热议问题