简体   繁体   English

Git 没有提交消息的提交

[英]Git commit with no commit message

How can I commit changes without specifying commit message?如何在不指定提交消息的情况下提交更改? Why is it required by default?为什么默认需要它?

git generally requires a non-empty message because providing a meaningful commit message is part of good development practice and good repository stewardship. git 通常需要非空消息,因为提供有意义的提交消息是良好开发实践和良好存储库管理的一部分。 The first line of the commit message is used all over the place within git;提交消息的第一行用于 git 中的所有地方; for more, read "A Note About Git Commit Messages" .有关更多信息,请阅读“关于 Git 提交消息的说明”

If you open Terminal.app, cd to your project directory, and git commit -am '' , you will see that it fails because an empty commit message is not allowed.如果你打开 Terminal.app, cd到你的项目目录,然后git commit -am '' ,你会看到它失败了,因为不允许空的提交消息。 Newer versions of git have the git 的较新版本具有
--allow-empty-message commandline argument, including the version of git included with the latest version of Xcode. This will let you use this command to make a commit with an empty message: --allow-empty-message命令行参数,包括最新版本 Xcode 中包含的 git 版本。这将允许您使用此命令进行空消息提交:

git commit -a --allow-empty-message -m ''

Prior to the --allow-empty-message flag, you had to use the commit-tree plumbing command.--allow-empty-message标志之前,您必须使用commit-tree管道命令。 You can see an example of using this command in the "Raw Git" chapter of the Git book .您可以在 Git 一书的“Raw Git”一章中看到使用此命令的示例。

And if you add an alias for it then it's even better right?如果你为它添加一个别名,那就更好了,对吧?

git config --global alias.nccommit 'commit -a --allow-empty-message -m ""'

Now you just do an nccommit, nc because of no comment, and everything should be commited.现在你只是做一个 nccommit,nc 因为没有评论,一切都应该被提交。

When working on an important code update, if you really need an intermediate checkpoint you might just do:在进行重要的代码更新时,如果您确实需要一个中间检查点,您可以这样做:

git commit -am'.'

or shorter:或更短:

git commit -am.

This adds a commit with the message .这会添加带有消息的提交.

Note: starting git1.8.3.2 (July 2013), the following command ( mentioned above by Jeremy W Sherman ) won't open an editor anymore:注意: 从 git1.8.3.2 (2013 年 7 月)开始,以下命令( Jeremy W Sherman上面提到的)将不再打开编辑器:

git commit --allow-empty-message -m ''

See commit 25206778aac776fc6cc4887653fdae476c7a9b5a :请参阅提交 25206778aac776fc6cc4887653fdae476c7a9b5a

If an empty message is specified with the option -m of git commit then the editor is started.如果使用 git commit 的选项-m指定了一条空消息,则将启动编辑器。
That's unexpected and unnecessary.这是出乎意料和不必要的。
Instead of using the length of the message string for checking if the user specified one, directly remember if the option -m was given.不是使用消息字符串的长度来检查用户是否指定了一个,而是直接记住是否给出了选项-m


git 2.9 (June 2016) improves the empty message behavior: git 2.9(2016 年 6 月)改进了空消息行为:

See commit 178e814 (06 Apr 2016) by Adam Dinwoodie ( me-and ) .请参阅Adam Dinwoodie( me-and提交 178e814 (2016 年 4 月 6 日)。
See commit 27014cb (07 Apr 2016) by Jeff King ( peff ) .请参阅Jeff King ( peff )提交 27014cb (2016 年 4 月 7 日)。
(Merged by Junio C Hamano -- gitster -- in commit 0709261 , 22 Apr 2016) (由Junio C Hamano 合并gitster 提交 0709261,2016年 4 月 22 日)

commit : do not ignore an empty message given by -m '' commit :不要忽略-m ''给出的空消息

  • " git commit --amend -m '' --allow-empty-message ", even though it looks strange, is a valid request to amend the commit to have no message at all. git commit --amend -m '' --allow-empty-message ”,尽管它看起来很奇怪,但它是一个有效的请求,将提交修改为完全没有消息。
    Due to the misdetection of the presence of -m on the command line, we ended up keeping the log messsage from the original commit.由于在命令行上错误检测到-m的存在,我们最终保留了原始提交的日志消息。
  • " git commit -m "$msg" -F file " should be rejected whether $msg is an empty string or not, but due to the same bug, was not rejected when $msg is empty. git commit -m "$msg" -F file " 无论$msg是否为空字符串都应该被拒绝,但由于同样的错误,当$msg为空时不会被拒绝。
  • " git -c template=file -m "$msg" " should ignore the template even when $msg is empty, but it didn't and instead used the contents from the template file. git -c template=file -m "$msg" ”应该忽略模板,即使$msg为空,但它没有,而是使用模板文件中的内容。

--allow-empty-message -m '' (and -m "" ) fail in Git 2.29.2 on PowerShell: --allow-empty-message -m '' (和-m "" )在 PowerShell 上的 Git 2.29.2 中失败:

error: switch `m' requires a value错误:开关“m”需要一个值

(oddly enough, with a backtick on one side and a single quote on the other) (奇怪的是,一侧是反引号,另一侧是单引号)


The following works consistently in Linux, PowerShell, and Command Prompt:以下内容在 Linux、PowerShell 和命令提示符中始终有效:

git commit --allow-empty-message --no-edit

The --no-edit bit does the trick, as it prevents the editor from launching. --no-edit位可以解决问题,因为它会阻止编辑器启动。

I find this form more explicit and a bit less hacky than forcing an empty message with -m '' .我发现这种形式比使用-m ''强制发送空消息更明确,也更轻松。

You don't need git to accomplish this.您不需要 git 来完成此操作。 Creative use of a bash function will do the trick just fine.创造性地使用 bash function 就可以了。 If you don't care about messages, just set a default one and forget it.如果您不关心消息,只需设置一个默认消息即可。

function gitcom() {
  git commit -m "my default commit message"
}

If you were feeling really adventurous you could add, commit and push with one command如果您真的很喜欢冒险,您可以使用一个命令添加、提交和推送

function gitzap() {
  git add . && git commit -m "whatevs" && git push $1 $2
}

Which you would then run as然后你会运行

gitzap origin master

You could even get deeper and use parse_git_branch to save yourself some keystrokes there, or set a common default of "origin" and "master".你甚至可以更深入地使用 parse_git_branch 来为自己节省一些击键,或者设置一个通用的默认值“origin”和“master”。

Git requires a commit to have a comment, otherwise it wont accept the commit. Git 需要提交才能有评论,否则不会接受提交。

You can configure a default template with git as your default commit message or can look up the --allow-empty-message flag in git. I think (not 100% sure) you can reconfigure git to accept empty commit messages (which isn´t such a good idea).您可以使用 git 配置默认模板作为默认提交消息,或者可以在 git 中查找 --allow-empty-message 标志。我认为(不是 100% 确定)您可以重新配置 git 以接受空提交消息(这不是这不是个好主意)。 Normally each commit should be a bit of work which is described by your message.通常每次提交都应该是您的消息中描述的一些工作。

I found the simplest solution:我找到了最简单的解决方案:

git commit -am'save'

That's all,you will work around git commit message stuff.就是这样,您将解决 git 提交消息的问题。

you can even save that commend to a bash or other stuff to make it more simple.您甚至可以将该推荐保存到 bash 或其他内容以使其更简单。

Our team members always write those messages,but almost no one will see those message again.我们的团队成员总是写这些消息,但几乎没有人会再次看到这些消息。

Commit message is a time-kill stuff at least in our team,so we ignore it.至少在我们的团队中,提交消息是一种消磨时间的东西,所以我们忽略它。

I have the following config in my private project:我的私人项目中有以下配置:

git config alias.auto 'commit -a -m "changes made from [device name]"'

That way, when I'm in a hurry I do这样,当我赶时间时,我会

git auto
git push

And at least I know what device the commit was made from.至少我知道提交是用什么设备进行的。

Building upon some other answers, I came up with the following:基于其他一些答案,我得出以下结论:

git config --global alias.gromit '!git add --all && git commit --allow-empty-message -m ""'

This differs from the answer by dalvarezmartinez1 in that git add --all also adds new files, while the -a flag in git commit leaves them be.这与 dalvarezmartinez1 的答案不同,因为git add --all还添加了新文件,而git commit中的-a标志保留了它们。 Also, I think that this alias fits the command much better.另外,我认为这个别名更适合命令。 :-) :-)

Future self, forgive me all the silent, quick, great commits to come.未来的自己,请原谅我所有无声的、快速的、伟大的承诺。

How can I commit changes without specifying commit message?如何在不指定提交消息的情况下提交更改? Why is it required by default?为什么默认需要它?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM