How to amend a commit without changing commit message (reusing the previous one)?

前端 未结 6 1653
醉梦人生
醉梦人生 2020-11-27 08:52

Is there a way to amend a commit without vi (or your $EDITOR) popping up with the option to modify your commit message, but simply reusing the prev

相关标签:
6条回答
  • 2020-11-27 09:21

    Using the accepted answer to create an alias

     oops = "!f(){ \
        git add -A; \
        if [ \"$1\" == '' ]; then \
            git commit --amend --no-edit; \
        else \
            git commit --amend \"$@\"; \
        fi;\
    }; f"
    

    then you can do

    git oops
    

    and it will add everything, and amend using the same message

    or

    git oops -m "new message"
    

    to amend replacing the message

    0 讨论(0)
  • 2020-11-27 09:24

    To extend on the accepted answer, you can also do:

    git commit --amend --no-edit -a
    

    to add the currently changed files.

    0 讨论(0)
  • 2020-11-27 09:36

    Since git 1.7.9 version you can also use git commit --amend --no-edit to get your result.

    Note that this will not include metadata from the other commit such as the timestamp which may or may not be important to you.

    0 讨论(0)
  • 2020-11-27 09:43

    git commit -C HEAD --amend will do what you want. The -C option takes the metadata from another commit.

    0 讨论(0)
  • 2020-11-27 09:43

    just to add some clarity, you need to stage changes with git add, then amend last commit:

    git add /path/to/modified/files
    git commit --amend --no-edit
    

    This is especially useful for when you forgot to some changes in last commit or when you want to add more changes without creating new commits by reusing the last commit

    0 讨论(0)
  • 2020-11-27 09:44

    Another (silly) possibility is to git commit --amend <<< :wq if you've got vi(m) as $EDITOR.

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