覆盖单个 git commit 的配置用户

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19840921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 17:15:43  来源:igfitidea点击:

Override configured user for a single git commit

gitgithub

提问by FilBot3

I am trying to commit to a project on github.comfrom my work laptop, which is already configured for the company git server. Is there a way to commit specifying different author credentials, possible using a different configuration file or orther command line switches?

我正在尝试从我的工作笔记本电脑提交到github.com上的一个项目,该项目已经为公司 git 服务器配置。有没有办法提交指定不同的作者凭据,可以使用不同的配置文件或其他命令行开关?

I've tried using

我试过使用

--author="My Name <[email protected]>" 

and I received the message:

我收到了消息:

 Committer: unknown <[email protected]>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:

    git config --global user.name "Your Name"
    git config --global user.email [email protected]

After doing this, you may fix the identity used for this commit with:

    git commit --amend --reset-author

but it still didn't update my username for committing to the github.com project. Is there anything else I can try and is it even possible?

但它仍然没有更新我提交 github.com 项目的用户名。还有什么我可以尝试的吗?它甚至可能吗?

回答by John Szakmeister

First, the author is not necessarily the same as the committer. Git tracks both.

首先,作者不一定与提交者相同。Git 跟踪两者。

To set what name to use for just this repository, you could do:

要设置仅用于此存储库的名称,您可以执行以下操作:

git config user.name "Your Name"
git config user.email "Your email"

Notice the absence of the --globaloption. This will set the configuration in this repository only.

请注意该--global选项的缺失。这将仅在此存储库中设置配置。

Alternatively, you can do this for only a single command, using the -coption:

或者,您可以使用以下-c选项仅对单个命令执行此操作:

git -c "user.name=Your Name" -c "user.email=Your email" commit ...

But I think it's better to just use the config options above.

但我认为最好只使用上面的配置选项。

回答by Asenar

Try to prepend this to your git command :

尝试将其添加到您的 git 命令中:

git -c [email protected] -c user.name='Your Name'

This is ok for a one-shot, but you could prefer save this in the .git/config file of the local repo you cloned from git. This can be done by running git config(no --global or --system to make it "local to the repo") or by editing the .git/config(it's the same syntax as ~/.gitconfig

这是一次性的,但您可能更喜欢将其保存在从 git 克隆的本地存储库的 .git/config 文件中。这可以通过运行git config(没有 --global 或 --system 使其成为“本地存储库”)或通过编辑.git/config(它的语法与~/.gitconfig

回答by Jesse Glick

You could also set environment variables GIT_COMMITTER_NAME, GIT_COMMITTER_EMAIL, GIT_AUTHOR_NAME, and GIT_AUTHOR_EMAILon a per-shell or even per-command basis.

您还可以在每个 shell 甚至每个命令的基础上设置环境变量GIT_COMMITTER_NAMEGIT_COMMITTER_EMAILGIT_AUTHOR_NAMEGIT_AUTHOR_EMAIL

回答by Joe

I believe you could run:

我相信你可以运行:

git config user.name "Your Name"
git config user.email [email protected]

for different parts of git. Try opening the folder where the work is being done for the repo to the github server.

对于 git 的不同部分。尝试将正在为 repo 完成工作的文件夹打开到 github 服务器。

Make sure that you see a .git folder in that work folder. Now open that folder in Terminal/Console/CommandPrompt and try changing the username and email for that folder only by running the commands I specified above.

确保您在该工作文件夹中看到一个 .git 文件夹。现在在终端/控制台/CommandPrompt 中打开该文件夹,并尝试仅通过运行我上面指定的命令来更改该文件夹的用户名和电子邮件。

By changing that, you may now be able to access the GitHub server.

通过更改它,您现在可以访问 GitHub 服务器。

Take a look here: https://help.github.com/articles/setting-your-username-in-git?

看看这里:https: //help.github.com/articles/setting-your-username-in-git

Also, I believe there is a way to push to a remote repo by using your github username and password in the command itself, but I'm not quoting it because it didn't work for me.

另外,我相信有一种方法可以通过在命令本身中使用您的 github 用户名和密码来推送到远程存储库,但我没有引用它,因为它对我不起作用。

回答by slumtrimpet

Following up on Jesse Glick's answer here with an expanded implementation because IMO his suggestion is the best way to go for this.

在这里通过扩展实现跟进 Jesse Glick 的回答,因为 IMO 他的建议是实现这一目标的最佳方式。

Add the below to your .bash_profile. It will prompt you for a user when first running any git command in the active shell. It'll then remember the user for all subsequent runs.

将以下内容添加到您的 .bash_profile。首次在活动 shell 中运行任何 git 命令时,它会提示您输入用户。然后它会记住所有后续运行的用户。

(obviously cross reference an input user value to exact desired names and email addresses for the various git auther/name values in the case block and update the assigns as needed)

(显然,将输入用户值交叉引用到 case 块中各种 git auther/name 值的确切所需名称和电子邮件地址,并根据需要更新分配)

git() {
  echo "Running BETTER git..."
  if [ -z "$GIT_COMMITTER_NAME" ]; then
    while true; do
      read -p "Git User: " UNAME
      case $UNAME in
          user1 ) break;;
          user2 ) break;;
          * ) echo "Invalid User";;
      esac
    done
    GIT_COMMITTER_NAME=$UNAME
    export GIT_COMMITTER_NAME
    [email protected]
    export GIT_COMMITTER_EMAIL
    GIT_AUTHOR_NAME=$GIT_COMMITTER_NAME
    export GIT_AUTHOR_NAME
    GIT_AUTHOR_EMAIL=$GIT_COMMITTER_EMAIL
    export GIT_AUTHOR_EMAIL
  fi
  echo "  using git user: $GIT_AUTHOR_NAME / $GIT_AUTHOR_EMAIL"
  /usr/bin/git "$@"
}

回答by VonC

You could also set environment variables GIT_COMMITTER_NAME, GIT_COMMITTER_EMAIL, GIT_AUTHOR_NAME, and GIT_AUTHOR_EMAILon a per-shell or even per-command basis.

您还可以在每个 shell 甚至每个命令的基础上设置环境变量GIT_COMMITTER_NAMEGIT_COMMITTER_EMAILGIT_AUTHOR_NAMEGIT_AUTHOR_EMAIL

Actually, since Git 2.22 (Q2 2019) you have a new option: Four new configuration variables {author,committer}.{name,email}have been introduced to override user.{name,email}in more specific cases.

实际上,自 Git 2.22(2019 年第二季度)以来,您有了一个新选项:{author,committer}.{name,email}引入了四个新的配置变量以user.{name,email}在更具体的情况下覆盖。

See commit 39ab4d0(04 Feb 2019) by William Hubbs (``).
(Merged by Junio C Hamano -- gitster--in commit 4e021dc, 07 Mar 2019)

请参阅William Hubbs (``)提交的 39ab4d0(2019 年 2 月 4 日
(由Junio C gitsterHamano合并-- --commit 4e021dc,2019 年 3 月 7 日)

config: allow giving separate author and committer idents

The author.email, author.name, committer.emailand committer.namesettings are analogous to the GIT_AUTHOR_*and GIT_COMMITTER_*environment variables, but for the git configsystem.
This allows them to be set separately for each repository.

Git supports setting different authorship and committer information with environment variables. However, environment variables are set in the shell, so if different authorship and committer information is needed for different repositories an external tool is required.

This adds support to git config for author.email, author.name, committer.emailand committer.namesettings so this information can be set per repository.

config: 允许提供单独的作者和提交者身份

author.emailauthor.namecommitter.emailcommitter.name设置类似于GIT_AUTHOR_*GIT_COMMITTER_*环境变量,但对于git config系统。
这允许为每个存储库单独设置它们。

Git 支持使用环境变量设置不同的作者和提交者信息。但是,环境变量是在 shell 中设置的,因此如果不同的存储库需要不同的作者和提交者信息,则需要外部工具。

这增加了支持的混帐配置author.emailauthor.namecommitter.emailcommitter.name设置,因此,这些信息可以为每个存储库设置。