git commit --author 在未设置全局时不起作用

ric*_*hmb 14 git

在具有共享登录名的实验室机器上工作。不想设置全局 git 配置参数。

git commit --author="username <email>" 
Run Code Online (Sandbox Code Playgroud)

这曾经适用于较旧的 git 版本。现在它产生这个错误:

*** 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.

fatal: unable to auto-detect email address (got 'blah@blah.(none)')
Run Code Online (Sandbox Code Playgroud)

有什么想法吗?

whi*_*cko 11

另一种解决方案是,仅为一次运行设置标识git commit并使用 的-c选项git

git -c user.name="Your Name" -c user.email="you@example.com" commit -m "message"
Run Code Online (Sandbox Code Playgroud)

我的场景是运行一个 docker 容器,其中我是容器中的用户(不是 root),该容器对其主目录没有写入权限。图像是docker.io/alpine/git.

$ docker run -u 1004:1005 -it --rm --entrypoint=/bin/sh docker.io/alpine/git 
~ $ git config --global user.email "you@example.com"
error: could not lock config file /git/.gitconfig: Permission denied
Run Code Online (Sandbox Code Playgroud)


Agu*_*ico 9

如果您只为当前存储库设置 git 配置信息怎么办?

  git config user.email "you@example.com"
  git config user.name "Your Name"
Run Code Online (Sandbox Code Playgroud)

编辑 基于 OP 评论,一个可能的解决方案是:

GIT_AUTHOR_EMAIL="you@email.com" && GIT_AUTHOR_NAME="Your Name" && git commit
Run Code Online (Sandbox Code Playgroud)


Von*_*onC 5

首先,使用 Git 2.29(2020 年第 4 季度),错误消息更加清晰。

请参阅Junio C Hamano ( )的提交 9ed104e(2020 年 8 月 21 日)。(由Junio C Hamano 合并 -- --提交 e9bd00a中,2020 年 8 月 31 日)gitster
gitster

identuser.name:给出提示时说出谁的身份缺失

如果user.nameuser.email尚未配置并且用户调用:

git commit --author=...  
Run Code Online (Sandbox Code Playgroud)

如果不指定提交者身份,Git 就会出错并显示一条消息,要求用户进行配置user.nameuser.email但不会告诉用户缺少哪个属性。

对于不了解用户、作者和提交者之间区别的 Git 新手来说,这可能会令人困惑。

通过扩展错误消息以说明预期的归因,为此类用户提供更多帮助。

错误消息会明确指出:

Author identity unknown
# or
Committer identity unknown
Run Code Online (Sandbox Code Playgroud)

其次,来自OP的评论

其他人使用该机器和相同的存储库。我想我可以设置 git config,然后在完成后删除配置

您可以包装 git 命令并强制每个命令进行自我识别(每个 shell 会话一次)。
请参阅“针对单用户应用程序的多开发人员 Git 的最佳实践”和我的项目VonC/gitw