What does the warning “redirecting to” actually mean?

前端 未结 5 493
终归单人心
终归单人心 2021-01-03 22:45

I have noticed that sometimes when I git pull a project, there is a message saying:

\"warning: redirecting to \"

I tried search

5条回答
  •  走了就别回头了
    2021-01-03 23:16

    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  
    

    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.

提交回复
热议问题