【问题标题】:Commit Without Setting User Name and Email在不设置用户名和电子邮件的情况下提交
【发布时间】:2022-02-11 21:16:57
【问题描述】:

我尝试像这样commit

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

commit 的行数太多了!

有没有更短的方法?

【问题讨论】:

  • 实际上并没有更短,但是如果您设置环境变量GIT_COMMITTER_NAMEGIT_COMMITTER_EMAIL(以及相同的AUTHOR 版本,或者像您一样使用--author 参数),这应该足够了,你不需要--unset
  • git config user.name "你的用户名在这里" git config user.email user@email.com
  • 或者,这更短,可能更容易:git -c user.name=... -c user.email=... commit ...

标签: git


【解决方案1】:

(在建议使用带有环境变量的长版本后,我想到了这一点——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:Git 在版本 1.7.2 (missed it by that much :--:) 中学习了 git -c
  • 现代 git 版本似乎不再支持它们:git commit -c user.name='test bot' -c user.email='&lt;noreply@danilopianini.org&gt;' -m "foo: something" fatal: Option -m cannot be combined with -c/-C/-F. -c 现在从提供的提交中选择提交信息。
  • @DaniloPianini:Git 维护者的邮件列表上现在有一些关于与此相关的事情和/或与git merge 一起使用的错误讨论。似乎最近的一些变化打破了一些东西。他们多久能修好,我不确定。但是请注意,您应该使用git -c ... commit -m ...,而不是git commit -c ... -m ...。这里的-c 选项是git,而不是git commit,换句话说。
  • 感谢您的澄清!确实,我一直在滥用它。
【解决方案2】:

您可以在您的 repo 中编辑 .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

【讨论】:

  • 不再在 git 2.34.1 中工作
【解决方案3】:

值得注意的是,Linux 上的 git 将默认使用 /etc/passwd 中“gecos”字段中给出的 linux 用户名。您可以使用grep $(whoami) /etc/passwd | cut -d ':' -f 5 输出此名称。如果此字段为空,则您无法在不设置用户名的情况下提交,但如果它不为空,git 将使用它。因此,您将自己作为作者,系统用户作为提交者。

https://github.com/git/git/blob/master/ident.c

【讨论】:

    【解决方案4】:

    这是我在共享盒子上使用的解决方案。在~/.bashrc 中,添加以下行:

    alias gitaspaul='alias git="git -c user.name=\"Paul Draper\" -c user.email=\"my@email.org\""'
    alias gitasnone='unalias git'
    

    它的工作原理如下:当您在 shell 会话中时,运行 gitaspaul。从那一刻起,直到会话结束的每个 git 命令都将使用您的临时凭证。当您离开 shell 时,凭据将被遗忘。如果您想在会话结束前手动忘记凭据,请运行 gitasnone

    这是一个很好的解决方案,因为它不特定于任何一个 repo,并且它会一直存在到你的计算会话自然结束(比如关闭你的 ssh 会话)。

    它是这样的:

    $ git commit -m "new stuff"
    
    *** 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.
    
    $ gitaspaul
    $ git commit -m "new stuff"
    [master f6a17cd] new stuff
    2 files changed, 28 insertions(+), 6 deletions(-)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-01
      • 2015-01-03
      • 1970-01-01
      • 2016-07-04
      • 2011-02-08
      • 2011-12-29
      • 1970-01-01
      相关资源
      最近更新 更多