警告的含义
正如其他答案所解释的,您保存的 URL 与服务器使用的 URL 之间存在细微差别。 “轻微”表示存在,但可以自动修复,因此 Git 不会给出错误,而是给出警告。
如何摆脱警告:手动...
要摆脱它,您可以更新您正在使用的 URL,确保它与正确的 URL 匹配。如果你想手动做,你必须使用命令
git remote set-url <remote_name> <correct_remote_path>
其中remote_name 通常是origin,来自git remote,而correct_remote_path 是警告显示的那个。
...还有一个 Bash 脚本。变体 1,安全但部分手动
我编写了一个小型 Bash 脚本来自动检查该警告。它会告诉你是否无事可做,或者它会打印用于删除警告的命令。为了安全起见,它不会自动运行。
我选择了一个函数,你可以直接复制粘贴到你的shell中,这样你就不用担心将它保存到文件中,检查文件的路径,然后删除它。这里是:
function check_git_redirection_warning {
remote_name="$(git remote)";
wrong_remote_path="$(git remote get-url $remote_name)";
correct_remote_path="$(git fetch --dry-run 2> >(awk '/warning: redirecting to/ { print $4}'))";
if [ -z "${correct_remote_path-}" ]; then
printf "The path of the remote '%s' is already correct\n" $remote_name;
else
printf "Command to change the path of remote '%s'\nfrom '%s'\n to '%s'\n" $remote_name $wrong_remote_path $correct_remote_path;
printf "git remote set-url %s %s\n" $remote_name $correct_remote_path;
fi
}
如何运行
在您复制脚本并将其粘贴到您的 shell(只需要一次)后,只需转到您发现问题的 Git 目录并输入 check_git_redirection_warning。检查生成的命令,如果有意义(应该这样做,但要注意安全!),只需将其复制并粘贴到 shell 中即可。
工作原理
- 首先,它运行
git remote来获取默认远程的名称(通常是origin)
- 然后它通过运行
git remote get-url $remote_name 找到当前配置的(或可能)错误的 URL。
- 然后它会得到正确的 URL。我还没有找到直接的方法来找到它,所以这就是我要做的:我使用
--dry-run 选项运行 git fetch(dryrun 什么都不做,所以实际上什么都没有获取。如果你不这样做,这会有所帮助想要改变任何东西,尽管通常没有理由避免运行 fetch)。如果有警告,Git 会将其打印到 STDERR。为了捕获它,我使用process substitution,然后我使用 AWK(通常在任何系统上都可用)解析消息并获得第 4 个单词。我认为如果 URL 中有空格,这部分会失败,但不应该有,所以我没有费心让它更健壮。
- 此时它会检查是否已找到警告(连同正确的 URL):如果没有,则无需执行任何操作,因此它只是打印一条消息并退出。
- 如果找到正确的 URL,它会打印一条消息,同时显示旧 URL 和新 URL,然后打印必须运行以应用更改的命令。
变体 2,不安全但全自动
如果您信任我的脚本并且还想运行命令,而不仅仅是打印它,您可以使用这个变体:
function remove_git_redirection_warning {
remote_name="$(git remote)"
wrong_remote_path="$(git remote get-url $remote_name)"
correct_remote_path="$(git fetch --dry-run 2> >(awk '/warning: redirecting to/ { print $4}'))"
if [ -z "${correct_remote_path-}" ]; then
printf "The path of the remote '%s' is already correct\n" $remote_name;
else
mycmd=(git remote set-url "$remote_name" "$correct_remote_path")
printf '%s ' "${mycmd[@]}"; printf "\n";
"${mycmd[@]}"
fi
}
工作原理
它与第一个非常相似,但不是打印命令,而是it saves all the parts into an array 调用mycmd,然后使用"${mycmd[@]}" 运行它。
如何在所有 Git 存储库中运行脚本
到目前为止,我们已经了解了如何在一个 repo 中修复警告。如果您有很多,并且想要全部更新怎么办?您可以在此处使用此其他脚本:
git_directories="$(find . -name ".git" -exec dirname {} \;)"
for git_dir in $git_directories; do
printf "Entering directory %s\n" $git_dir
cd $git_dir
remove_git_redirection_warning
printf "\n"
cd -
done
finds all the repositories 通过查找包含 .git 的目录(作为文件和目录:它通常是目录,但它是子模块的文件)。然后,对于每个 repo,它都会进入其中,调用函数,然后返回。