You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows下VS Code中Git邮箱与用户名设置报错解决方法求助

Windows下VS Code中Git身份配置失败的解决办法

问题现象

在Windows系统的VS Code中提交代码时,出现身份未识别错误:

2022-12-19 07:17:00.902 [info] Author identity unknown

*** 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: no email was given and auto-detection is disabled

执行全局配置命令时,无论是PowerShell还是Git Bash都报错:

PS E:\path\to\repo> git config --global user.email "john.doh@domainname.com"
error: could not lock config file %HOMEDRIVE%%HOMEPATH%/.gitconfig: No such file or directory
johndoh@myhost MINGW64 /
$ git config --global user.email "john.doh@domainname.com"
error: could not lock config file C:/Program Files/Git/%HOMEDRIVE%%HOMEPATH%/.gitconfig: No such file or directory

当前Git版本为2.38.1.windows.1,通过Visual Studio Installer安装。

解决方法

1. 手动创建全局配置文件

Git无法自动定位用户目录生成配置文件时,可手动创建:

  • 打开文件资源管理器,进入个人用户目录(如C:\Users\johndoh)
  • 新建文本文件,重命名为.gitconfig(需开启「显示文件扩展名」,删除默认的.txt后缀)
  • 打开文件并添加以下内容,替换为自己的信息:
[user]
    name = 你的用户名
    email = john.doh@domainname.com

2. 指定配置文件路径执行命令

直接指定用户目录下的配置文件路径,绕过环境变量解析问题:
在PowerShell或Git Bash中执行:

git config --file "C:/Users/johndoh/.gitconfig" --global user.name "你的用户名"
git config --file "C:/Users/johndoh/.gitconfig" --global user.email "john.doh@domainname.com"

注意替换为自己的实际用户目录路径。

3. 修复系统环境变量

环境变量HOMEDRIVE或HOMEPATH配置错误会导致Git无法定位用户目录:

  • 右键「此电脑」→「属性」→「高级系统设置」→「环境变量」
  • 在「用户变量」区域检查:
    • HOMEDRIVE应设置为系统盘(如C:)
    • HOMEPATH应设置为用户目录路径(如\Users\johndoh)
  • 若变量缺失或错误,手动添加/修改后保存,重启终端和VS Code再尝试配置。

4. 单独配置本地仓库

如果全局配置始终有问题,可直接在当前仓库下配置(仅对该仓库生效):
进入仓库目录后执行:

git config user.name "你的用户名"
git config user.email "john.doh@domainname.com"

内容的提问来源于stack exchange,提问作者watkipet

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 14:20:23