提交时不设置user.email和user.name


76

我尝试这样提交

git commit --author='Paul Draper <my@email.org>' -m 'My commit message'

但我明白了

*** Please tell me who you are.

Run

git config --global user.email "you@example.com"
git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.

我可以设置它们,但是我在一个共享的盒子上,之后我将不得不(想要)取消它们。

git config user.name 'Paul Draper'
git config user.email 'my@email.org'
git commit -m 'My commit message'
git config --unset user.name
git config --unset user.email

一次提交有很多行!

有更短的方法吗?


7
确实没有那么短,但是如果您设置环境变量GIT_COMMITTER_NAMEGIT_COMMITTER_EMAIL(以及AUTHOR相同的版本,或者--author像您一样使用参数),就足够了,并且不需要--unset
torek

git config user.name“您的用户名在这里” git config user.email user@email.com
felipsmartins 2014-2-27

1
或者,它更短并且可能更容易:git -c user.name=... -c user.email=... commit ...
torek

Answers:


126

(这是我在建议使用环境变量的长版本后发生的:git commit希望同时设置作者和提交者,并且--author仅覆盖前者。)

所有git命令都在操作动词之前使用-c参数来设置临时配置数据,因此这是理想的选择:

git -c user.name='Paul Draper' -c user.email='my@email.org' commit -m '...'

因此,在这种情况下,它-cgit命令的一部分,而不是commit子命令。


这听起来很棒,但是哪个版本支持呢?我得到一个“未知选项:-c”,我的版本是1.7.1 ....:'-(
msb

3
@msb:Git从git -c1.7.2版中学到了(错过了那么多-: :)。
torek

19

您可以在.git/config存储库中编辑文件以添加以下别名:

[alias]
  paulcommit = -c user.name='Paul Draper' -c user.email='my@email.org' commit

或者您可以通过命令行执行此操作:

git config alias.paulcommit "-c user.name='Paul Draper' -c user.email='my@email.org' commit"

然后您可以执行以下操作:

git paulcommit -m "..."

备注:

  • 我们的想法是那么还添加别名一样jeancommitgeorgecommit...这个共享盒的其他用户。
  • 您可以通过编辑您的个人信息.gitconfig--global在添加别名时在命令行中添加选项来将此别名添加到全局配置中。
  • 别名paulcommit不是简短的别名,但是它很冗长,通常只能输入git pau+ tab

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.