I have noticed that sometimes when I git pull
a project, there is a message saying:
"warning: redirecting to <url>"
I tried searching what it means but I find nothing useful. What is it?
I have noticed that sometimes when I git pull
a project, there is a message saying:
"warning: redirecting to <url>"
I tried searching what it means but I find nothing useful. What is it?
warning: redirecting to
This is typical of a Git repo URL starting with git://
or http://
, but which is redirected at the server level to https://
(which is more secure, and allows for authentication)
This is set at the server level (as in this one) with a 301 Moved Permanently.
# enforce https location / { return 301 https://$server_name$request_uri; }
Check your remote:
git remote -v
Probably, your remote is https://server/../project
and does not end with .git
(https://server/../project.git
).
.git
to the remote url directly in command line: git remote set-url origin https://.../repo.git
–
Korney InsteadOf
settings that would change the protocol used. Running git remote show origin
will take InsteadOf
config into account and show you the actual urls that will be used. –
Kennard As the other answers have explained, there's a slight difference between the URL that you have saved and the one that the server uses. "Slight" means that it is there, but it can be fixed automatically, so Git doesn't give an error, but rather a warning.
To get rid of it you can update the URL that you are using, ensuring it matches the correct one. If you want to do it manually, you have to use the command
git remote set-url <remote_name> <correct_remote_path>
where the remote_name
is usually origin
, and comes from git remote
, and correct_remote_path
is the one shown by the warning.
I've written a small Bash script to check automatically for that warning. It will tell whether there's nothing to do, or it will print the command to use to remove the warning. It won't run it automatically, just to be safe.
I've chosen to use a function, which you can copy and paste directly in your shell, so you don't have to worry about saving it to a file, checking the file's path, and then deleting it. Here it is:
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
}
After you have copied the script and pasted it in your shell (required only once), just go to your Git directory where you are seeing the problem and enter check_git_redirection_warning
. Check the generated command and, if it makes sense (it should, but let's be safe!), just copy and paste it into the shell.
git remote
to get the name of the default remote (usually origin
)git remote get-url $remote_name
.git fetch
with the --dry-run
option (a dryrun does nothing, so nothing gets actually fetched. This helps in case you don't want to change anything, although there's normally no reason to avoid running fetch). If there's the warning, Git prints it to STDERR. To capture it I use process substitution, and then I parse the message with AWK (which is normally available on any system) and get the 4th word. I think this part would fail if there were spaces in the URL, but there shouldn't be any, so I haven't bothered to make it more robust.If you trust my script and want to also run the command, instead of just printing it, you can use this variant:
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's very similar to the first one, but instead of printing the command, it saves all the parts into an array called mycmd
and then runs it with "${mycmd[@]}"
.
So far we've seen how to fix the warning in one repo. What if you have many, and want to update them all? You can use this other script here:
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
It finds all the repositories by looking for directories containing .git (both as a file and as a directory: it's normally a directory, but it's a file for submodules). Then, for each repo it goes inside it, calls the function, and goes back.
warning: redirecting to
This is typical of a Git repo URL starting with git://
or http://
, but which is redirected at the server level to https://
(which is more secure, and allows for authentication)
This is set at the server level (as in this one) with a 301 Moved Permanently.
# enforce https location / { return 301 https://$server_name$request_uri; }
If you cloned from www.github.com it will give you that message because it prefers github.com (no www). Happened to me today.
In my case the only difference was a trailing slash /
on the URL address returned by the warning from GitHub.
Adding the trailing slash to my config file made the warning go away.
Oddly enough, I was doing a git fetch --all
and only my
remote required the final slash, the other (origin
and maintainer's) GitHub repos didn't need it. Rather confusing.
In my case, I'm using azure git and was sending the PAT with the git clone command in the server url. It's already included with http.extraHeader="etc". This gives this warning. When I removed the PAT from the url the warning went away.
In my case, I had set the URL as http://
instead of https://
. I fixed it with:
git remote set-url origin https://[email protected]/myrepo.git
I got this message when trying to sync a branch. It turned out the branch had been merged and deleted on the remote origin end.
© 2022 - 2024 — McMap. All rights reserved.
.git/config
the remote url was set tohttps://gitlab.com/group/subgroup/project
but gitlab redirected tohttps://gitlab.com/group/subgroup/project.git/
. Setting the url directly toproject.git
fixed it. – Ima