【问题标题】:ask committer name and email every git commit在每次 git 提交时询问提交者姓名和电子邮件
【发布时间】:2021-03-25 15:08:39
【问题描述】:

如果你 git commit 没有告诉 git 你是谁,你会收到这样的消息

$ git commit -m "From Qworp"

*** 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 commit 提示 user.name 和 user.email?像

$ git commit -am "From Qworp"
user.name: Qworp
user.email: qworp@qworp.com
[master be42c9e] From Qworp
 1 file changed, 5 insertions(+), 5 deletions(-)
...
$ git commit -am "Now Ace"
user.name: Ace
user.email: ace@qworp.com
[master 56ec176] Now Ace
 1 file changed, 2 insertions(+), 1 deletions(-)

并据此设置作者和提交者的详细信息?

【问题讨论】:

  • 开箱即用不支持。您只是想在每次提交时假装成其他人,还是您正在尝试解决一些潜在的问题?如果是第一个,我会编写一个 shell 脚本来包装 git commit 并在每次提交之前更改配置。注意,可以在命令行使用--author指定作者。
  • @JoachimSauer 是的,正是第一个。谢谢你这么清楚的评论。我想我会看看包装它
  • 你想完成什么?不同回购的不同默认值?我为此使用conditional includes...
  • @kapsiR:这可以通过简单地为每个 repo 配置这些值来解决,无需进一步复杂化。
  • @JoachimSauer 是的,但不是每个新的回购。如果我会依靠它,例如全局默认值,那么它对我不起作用 - 这就是我有基于文件夹的默认值的原因。

标签: git


【解决方案1】:

通常一个 git hook 是要走的路。但即使在第一个运行的 git hook pre-commit 中,用户也已经设置好了。因此,在git 周围使用以下包装器。在 .bashrc 和/或 .bash_profile 中定义此函数。

git() {
  local opts=()
  if [ "$1" = commit ]; then
    read -p "user.name: " name
    read -p "user.email: " email
    opts+=(-c user.name="${name}" -c user.email="${email}")
  fi
  command git "${opts[@]}" "$@"
}

【讨论】:

  • 你为什么改成 bash 答案而不是 git pre-commit ?
  • 感谢您的来信,请原谅我糟糕的测试。 Git 钩子默认没有标准输入。您可以使用exec < /dev/tty 读取标准输入。但是,我发现git config user.name 不会影响pre-commit 中的当前提交,而只会影响未来的提交。我的错。似乎 git hooks 在这里不是正确的方法,所以我切换到了一个包装函数。
  • 预提交不能改变当前提交的作者/提交者吗?
  • 我不能肯定地说。但是到目前为止我还没有找到方法。也许git commit --amend 中的git commit --amend 钩子可以工作,但如果用户使用 ctrl+c 中途中止提交,那将很容易出错。
  • 感谢您回到这里
猜你喜欢
  • 2018-05-27
  • 2018-11-21
  • 2014-11-14
  • 1970-01-01
  • 1970-01-01
  • 2021-04-11
相关资源
最近更新 更多