Is there a way to reuse the previous comment on a git commit?

前端 未结 4 504
滥情空心
滥情空心 2020-12-30 02:34

Occasionally I will drop into troubleshooting mode and commit/push a number of small but separate commits with a comment like, \"Troubleshooting the during

相关标签:
4条回答
  • 2020-12-30 02:50

    From the git-commit(1) command documentation,

    -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.
    
    -c <commit>
    --reedit-message=<commit>
    Like -C, but with -c the editor is invoked, so that the user can further edit 
    the commit message.
    

    It's then possible using,

      git commit --reuse-message=HEAD
    

    Update:

    You may also need to use the --reset-author option,

    --reset-author
    When used with -C/-c/--amend options, declare that the authorship of the 
    resulting commit now belongs of the committer. This also renews the author 
    timestamp.
    
    0 讨论(0)
  • 2020-12-30 02:52

    I'm not sure of how you can have a certain set of git commits use the last git comment you had entered, but you can set a default commit message. That could do the trick as long as you unset the default commit message once you're done with all the commits that needed to use that message.

    Here's how you go about setting a default commit message. First, enter the desired commit message in a file, lets call it ~/LastCommitMessage.txt. Then, specify this as your default (global) commit message like so:

    $ git config --global commit.template ~/LastCommitMessage.txt
    

    You could narrow the scope by not using --global and using something else instead.

    You can easily access all git settings by opening up the .gitconfig file located in your home directory. Open up that file and delete the setting above to unset it once you're done with all your commits.

    0 讨论(0)
  • 2020-12-30 02:53

    At first, I answered:

    I guess git commit --reuse-message=HEAD does it

    Then I thought that's not what you wanted and deleted it. Then life caught up and got AFK for a couple of hours. Anyways, despite an answer having already been accepted, I would have suggested:

    $ git config alias.troubleshoot '!troubleshoot() { git add -u && git commit -m "Troubleshooting the $1 during deployment to Heroku."; }; troubleshoot'
    

    And you use it the following way:

    1. modify existing files
    2. (eventually add untracked files)
    3. git troubleshoot foo

    Would commit changes (and eventually new files) with "Troubleshooting the foo during deployment to Heroku." as commit message.

    0 讨论(0)
  • 2020-12-30 02:54

    .git/COMMIT_EDITMSG contains the last commit message. https://git-scm.com/docs/git-commit#_files

    git commit --file .git/COMMIT_EDITMSG
    

    will use that file as the commit message.

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