What does the warning "redirecting to" actually mean?
Asked Answered
G

8

53

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?

Geber answered 26/10, 2018 at 16:4 Comment(3)
Maybe git got a Redirect response from the repo to check in another URL?Mousetail
The problem was is my case that in the local .git/config the remote url was set to https://gitlab.com/group/subgroup/project but gitlab redirected to https://gitlab.com/group/subgroup/project.git/. Setting the url directly to project.git fixed it.Ima
Cloning from www.github.com will give a redirect warning because it prefers github.com without a www prefix.Saladin
V
17
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;
}
Vicar answered 27/10, 2018 at 6:46 Comment(0)
A
43

Check your remote:

git remote -v

Probably, your remote is https://server/../project and does not end with .git (https://server/../project.git).

Abode answered 15/8, 2019 at 9:44 Comment(3)
1.) Open the .git/config file 2.) Change " [remote "origin"] url = git.somehost.com/group/projectName " to "[remote "origin"] url = git.somehost.com/group/projectName.git " (i.e. add ".git" to projectName)Barium
You can add .git to the remote url directly in command line: git remote set-url origin https://.../repo.gitKorney
Also check you haven't got any 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
M
23

What the warning means

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.

How to get rid of the warning: manually...

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.

...And with a Bash script. Variant 1, safe but partly manual

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
}

How to run it

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.

How it works

  • First, it runs git remote to get the name of the default remote (usually origin)
  • Then it finds the URL that is currently configured, and that is (or could be) wrong, by running git remote get-url $remote_name.
  • Then it gets the correct URL. I haven't found a direct way to find it, so this is what I do: I run 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.
  • At this point it checks whether the warning has been found (together with the correct URL): if not, there's nothing to do, so it just prints a message and exits.
  • If the correct URL has been found, it prints a message showing both the old and the new one, and then it prints the command that must be run to apply the change.

Variant 2, unsafe but fully automatic

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
}

How it works

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[@]}".

How to run the script in all the Git repositories

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.

Maritzamariupol answered 11/3, 2020 at 2:57 Comment(0)
V
17
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;
}
Vicar answered 27/10, 2018 at 6:46 Comment(0)
S
2

If you cloned from www.github.com it will give you that message because it prefers github.com (no www). Happened to me today.

Saladin answered 24/8, 2020 at 23:8 Comment(0)
E
1

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.

Employment answered 9/11, 2020 at 22:56 Comment(0)
P
0

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.

Philately answered 6/9, 2021 at 11:41 Comment(0)
D
0

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
Derman answered 11/8, 2022 at 14:45 Comment(0)
E
0

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.

Ecdysiast answered 8/9, 2022 at 15:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.