Git报错Warning: safe.directory非绝对路径及命令执行失败求助
解决Git safe.directory警告及配置命令错误问题
问题根源
你之前执行的命令语法有误,--add和safe.directory之间多了个点,导致Git无法识别该选项;另外safe.directory要求配置本地绝对路径,填仓库URL完全无效,这也是警告里“not absolute”的核心原因。
正确解决步骤
获取本地仓库的绝对路径
- Linux/macOS:进入仓库目录后执行
pwd,复制输出的完整路径 - Windows(CMD):进入仓库目录后执行
echo %cd%,复制输出的完整路径 - Windows(PowerShell):进入仓库目录后执行
Get-Location,复制输出的完整路径
- Linux/macOS:进入仓库目录后执行
执行正确的全局配置命令
将刚才获取的绝对路径填入命令(路径包含空格时需加引号):git config --global --add safe.directory "/你的本地仓库绝对路径"示例(Linux):
git config --global --add safe.directory "/home/cleaner98/my-new-repo"示例(Windows):
git config --global --add safe.directory "C:\Users\Cleaner98\my-new-repo"(可选)允许所有本地仓库(仅临时方便用,不推荐长期开启)
要是不想逐个配置仓库,可执行:git config --global --add safe.directory '*'验证配置是否生效
执行以下命令,能看到你添加的路径就说明配置成功:git config --global --get-all safe.directory
验证问题是否解决
重新执行git status或git init,警告应该会消失。
内容的提问来源于stack exchange,提问作者Cleaner98
相关产品推荐
相关产品推荐

