如何动态指定提交作者?

huangapple go评论61阅读模式
英文:

How to specify commit author on the fly?

问题

你想要只为这个提交签名,可以使用以下命令:

git commit -m "Message" --author="My Name <my_email@email.com>" --signoff

这将允许你仅为此提交签名,而不设置全局默认身份。

英文:

I want to do a commit in a repo. I don't want to set me as the default person signing, i.e. want to avoid git config --global user.email &quot;you@example.com&quot; and git config --global user.name &quot;Your Name&quot;. So I am doing git commit -m &quot;Message&quot; --author=&quot;My Name &lt;my_email@email.com&gt;&quot;, but Git says

Committer identity unknown

*** Please tell me who you are.

Run

  git config --global user.email &quot;you@example.com&quot;
  git config --global user.name &quot;Your Name&quot;

to set your account&#39;s default identity.
Omit --global to set the identity only in this repository.

How can I sign only this commit?

答案1

得分: 3

尝试在提交前设置GIT_AUTHOR_NAME和GIT_AUTHOR_EMAIL环境变量。有关此内容的解释

#!/bin/bash
GIT_AUTHOR_NAME="Your Name" GIT_AUTHOR_EMAIL=you@example.com git commit -m "Message"
英文:

Try to set GIT_AUTHOR_NAME and GIT_AUTHOR_EMAIL environment variables before your commit. See the explanation about this.

#!/bin/bash
GIT_AUTHOR_NAME=&quot;Your Name&quot; GIT_AUTHOR_EMAIL=you@example.com git commit -m &quot;Message&quot;

答案2

得分: 0

根据我所知,我认为你无法避免编辑你的配置。但是,你可以移除 --global 标志,这样它会保存名称/电子邮件到本地仓库,而不会覆盖你的全局设置。

所以你可以设置本地配置,签署提交,然后取消配置:

git config --global user.email "you@example.com"
git config --global user.name "Your Name"
git commit -m "Message"
git config --unset user.email
git config --unset user.name

我承认这有点繁琐,特别是如果你经常以这种方式提交。

你还可以使用 git config --edit 编辑本地配置文件,在那里你可以随时注释掉你的名称/电子邮件,以便 "签出"。

英文:

As far as I know, I don't think you can avoid editing your configuration. However, you can remove the --global flag and it will save the name/email for the local repository instead of overwriting your global settings.

So you could set the local configuration, sign the commit, then unset the configuration:

git config --global user.email &quot;you@example.com&quot;
git config --global user.name &quot;Your Name&quot;
git commit -m &quot;Message&quot;
git config --unset user.email
git config --unset user.name

Which I admit is a bit tedious, especially if you are committing this way frequently.

You can also edit this local configuration file with git config --edit, where you can comment out your name/email whenever you want to "sign out".

huangapple
  • 本文由 发表于 2023年6月15日 00:50:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76475879.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定