无法更改 git 配置的 user.name,立即重置

Can't change git config's user.name, gets reset immediately

如果我这样做:

 git config --global user.name "My New Name"

它能用一段时间。如果我这样做:

cat ~/.gitconfig

我可以在 user.name 属性 中看到正确的值。

但是,只要我打开一个新终端 window 或执行 git 提交,旧名称就会重置。

我正在使用 ssh。有没有什么缓存机制?

(注意这不是关于 GitHub 用户名,而是关于每次提交的作者姓名)

FILES section of the git config documentation 显示配置值的来源。

If not set explicitly with --file, there are four [or five] files where git config will search for configuration options:

  1. $(prefix)/etc/gitconfig
    System-wide configuration file.
  2. $XDG_CONFIG_HOME/git/config
    Second user-specific configuration file. If $XDG_CONFIG_HOME is not set or empty, $HOME/.config/git/config will be used. Any single-valued variable set in this file will be overwritten by whatever is in ~/.gitconfig. It is a good idea not to create this file if you sometimes use older versions of Git, as support for this file was added fairly recently.
  3. ~/.gitconfig
    User-specific configuration file. Also called "global" configuration file.
  4. $GIT_DIR/config
    Repository specific configuration file.
  5. $GIT_DIR/config.worktree
    This is optional and is only searched when extensions.worktreeConfig is present in $GIT_DIR/config.

至于它们的优先级

The files are read in the order given above, with last value found taking precedence over values read earlier. When multiple values are taken then all values of a key from all files will be used.

如果您使用 git configgit config --local 修改存储库的配置(这将修改 $GIT_DIR/config,因此 .git/config 用于带有工作树的存储库或 config 在裸仓库中),通过 git config --global 的更改(存储在 ~/.gitconfig 中)将在该仓库中不可见。

为了快速检查完整性,运行 两个命令。

git config --global user.name
git config --local  user.name

我有一个 shell 脚本覆盖全局 ~/.gitconfig。具体来说,来自 Mathias Bynens 的 .extra 文件 dotfiles.

See this GitHub issue for more details.