git 如何在不更改提交消息的情况下修改提交(重用前一个)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10237071/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 13:31:24  来源:igfitidea点击:

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

gitcommitgit-commitamend

提问by Sridhar Sarnobat

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 previous message?

有没有办法修改提交而不vi(或您的$EDITOR)弹出修改提交消息的选项,而只是重用以前的消息?

回答by Shaggie

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

从 git 1.7.9 版本开始,您还可以使用git commit --amend --no-edit来获取结果。

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

请注意,这不会包括来自其他提交的元数据,例如对您来说可能重要也可能不重要的时间戳。

回答by Andy Ross

git commit -C HEAD --amendwill do what you want. The -Coption takes the metadata from another commit.

git commit -C HEAD --amend会做你想做的。该-C选项从另一个提交中获取元数据。

回答by galva

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

另一种(愚蠢的)可能性是git commit --amend <<< :wq如果你有 vi(m) as $EDITOR

回答by Rambatino

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

要扩展已接受的答案,您还可以执行以下操作:

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

to add the currently changed files.

添加当前更改的文件。

回答by aljgom

Using the accepted answer to create an alias

使用接受的答案创建别名

 oops = "!f(){ \
    git add -A; \
    if [ \"\" == '' ]; 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

修改替换消息