Mak*_*gan 12 git url warnings pull
我注意到有时当我git pull一个项目时,会有一条消息说:
"warning: redirecting to <url>"
Run Code Online (Sandbox Code Playgroud)
我尝试搜索它的含义,但没有发现任何有用的信息。它是什么?
Fab*_*ica 13
正如其他答案所解释的那样,您保存的 URL 与服务器使用的 URL 之间略有不同。“轻微”意味着它在那里,但它可以自动修复,因此Git不会给出错误,而是警告。
要摆脱它,您可以更新您正在使用的 URL,确保它匹配正确的 URL。如果你想手动完成,你必须使用命令
git remote set-url <remote_name> <correct_remote_path>
Run Code Online (Sandbox Code Playgroud)
其中remote_name
通常是origin
,来自git remote
,并且correct_remote_path
是警告显示的那个。
我编写了一个小的 Bash 脚本来自动检查该警告。它会告诉是否有什么可做的,或者它会打印用于删除警告的命令。它不会自动运行它,只是为了安全。
我选择使用一个函数,您可以直接在 shell 中复制和粘贴该函数,因此您不必担心将其保存到文件、检查文件的路径,然后将其删除。这里是:
git remote set-url <remote_name> <correct_remote_path>
Run Code Online (Sandbox Code Playgroud)
在您复制脚本并将其粘贴到您的 shell 中(仅需要一次)后,只需转到您看到问题的 Git 目录并输入check_git_redirection_warning
. 检查生成的命令,如果它有意义(它应该,但让我们安全点!),只需将其复制并粘贴到 shell 中。
git remote
以获取默认远程的名称(通常为origin
)git remote get-url $remote_name
.git fetch
使用该--dry-run
选项运行(dryrun 什么都不做,所以实际上没有获取任何东西。这有助于万一你不想改变任何东西,虽然通常没有理由避免运行 fetch)。如果有警告,Git 会将其打印到 STDERR。为了捕获它,我使用了进程替换,然后我用 AWK(通常在任何系统上都可用)解析消息并获得第 4 个单词。我认为如果 URL 中有空格,这部分会失败,但不应该有空格,所以我没有费心让它更健壮。如果您信任我的脚本并且还想运行该命令,而不仅仅是打印它,您可以使用以下变体:
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
}
Run Code Online (Sandbox Code Playgroud)
它与第一个非常相似,但它不是打印命令,而是将所有部分保存到一个名为的数组中mycmd
,然后使用"${mycmd[@]}"
.
到目前为止,我们已经看到了如何在一个 repo 中修复警告。如果您有很多,并且想要全部更新怎么办?您可以在此处使用其他脚本:
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
}
Run Code Online (Sandbox Code Playgroud)
它通过查找包含 .git 的目录(作为文件和目录:它通常是一个目录,但它是子模块的文件)来找到所有存储库。然后,对于每个 repo,它进入其中,调用该函数,然后返回。
Run Code Online (Sandbox Code Playgroud)warning: redirecting to
这是典型的Git存储库URL,以git://
或开头http://
,但在服务器级别重定向到https://
(更安全,并允许进行身份验证)
这是在服务器级别设置的(如本例所示),其中301已永久移动。
Run Code Online (Sandbox Code Playgroud)# enforce https location / { return 301 https://$server_name$request_uri; }
小智 5
远程检查
git remote -v
Run Code Online (Sandbox Code Playgroud)
可能您的远程地址为 https://server/../project 且不以.git结尾(https://server/../project.git)