git init无法读取全局配置的init.templateDir模板目录问题
问题
配置Git全局模板目录后,直接执行git init时提示模板目录不存在,但通过--template参数或环境变量GIT_TEMPLATE_DIR指定时能正常识别。Git版本为2.40.1,命令行操作记录如下:
[nix-shell:~/tmp/ai-webui/foo]$ git config --global init.templateDir '/home/abrahms/.git_template/' [nix-shell:~/tmp/ai-webui/foo]$ git init warning: templates not found in '/home/abrahms/.git_template/' Initialized empty Git repository in /home/abrahms/tmp/ai-webui/foo/.git/ [nix-shell:~/tmp/ai-webui/foo]$ git init --template=/home/abrahms/.git_template/ Reinitialized existing Git repository in /home/abrahms/tmp/ai-webui/foo/.git/ [nix-shell:~/tmp/ai-webui/foo]$ export GIT_TEMPLATE_DIR="$HOME/.git_template/" [nix-shell:~/tmp/ai-webui/foo]$ git init Reinitialized existing Git repository in /home/abrahms/tmp/ai-webui/foo/.git/ [nix-shell:~/tmp/ai-webui/foo]$ unset GIT_TEMPLATE_DIR [nix-shell:~/tmp/ai-webui/foo]$ git init warning: templates not found in '/home/abrahms/.git_template/' Reinitialized existing Git repository in /home/abrahms/tmp/ai-webui/foo/.git/ [nix-shell:~/tmp/ai-webui/foo]$ git --version git version 2.40.1
原因
从git config --global init.templateDir的输出可以看到,配置值包含了单引号('/home/abrahms/.git_template/'),这会导致Git尝试查找的路径实际是带引号的'/home/abrahms/.git_template/',而非正确的/home/abrahms/.git_template/,自然找不到目标目录。
这种情况通常是配置时错误地给路径添加了引号导致的,比如执行了类似git config --global init.templateDir "'/home/abrahms/.git_template/'"的命令。
解决步骤
- 重新设置全局模板目录配置,确保路径不带引号:
git config --global init.templateDir /home/abrahms/.git_template/
或者使用波浪线简化路径:
git config --global init.templateDir ~/.git_template/
- 验证配置是否正确:
执行git config --global init.templateDir,此时输出应该是不带引号的路径:
/home/abrahms/.git_template/
- 测试
git init:
再次执行git init,应该不会再出现模板目录不存在的警告,全局模板会正常生效。
内容的提问来源于stack exchange,提问作者Justin Abrahms
相关产品推荐
相关产品推荐

