I have noticed that sometimes when I git pull a project, there is a message saying:
\"warning: redirecting to \"
I tried search
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
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.