简体   繁体   English

将系统上所有 git 存储库的远程从 http 更改为 ssh

[英]Change the remote of all git repositories on a system from http to ssh

Recently Github came up with a deprecation notice that the HTTP method of pushing to our repositories is going to expire soon.最近 Github 提出了一个弃用通知,即推送到我们存储库的 HTTP 方法即将到期。 I've decided to change to the SSH method.我决定改用 SSH 方法。 On doing that I found that we need to change the remote URL of the repos after setting up keys.在这样做时,我发现我们需要在设置密钥后更改存储库的远程 URL。

But the change is a tedious process and to do it for all the repositories I have on my local system is quite a lengthy job.但是更改是一个乏味的过程,并且对我本地系统上的所有存储库进行更改是一项相当漫长的工作。 Is there some way we can write a Bash script that will go through the directories one by one and then change the remote URL from the HTTP version to the SSH version? Is there some way we can write a Bash script that will go through the directories one by one and then change the remote URL from the HTTP version to the SSH version?

This makes the necessary change from HTTP -> SSH.这使得从 HTTP -> SSH 进行必要的更改。

git remote set-url origin git@github.com:username/repo-name

The things that we need to change would be the repo-name which can be the same as the directory name.我们需要更改的是repo-name ,它可以与目录名称相同。

What I thought about was to run a nested for loop on the parent directory that contains all the git repos.我想到的是在包含所有 git 存储库的父目录上运行一个嵌套的 for 循环。 This would be something like:这将是这样的:

for DIR in *; do
    for SUBDIR in DIR; do
        ("git remote set-url..."; cd ..;)
    done
done

This will identify all subfolders containing a file or folder named .git , consider it a repo, and run your command.这将识别包含名为.git的文件或文件夹的所有子文件夹,将其视为存储库,然后运行您的命令。

I strongly recommend you make a backup before running it.我强烈建议您在运行之前进行备份。

#!/bin/bash

USERNAME="yourusername"

for DIR in $(find . -type d); do

    if [ -d "$DIR/.git" ] || [ -f "$DIR/.git" ]; then

        # Using ( and ) to create a subshell, so the working dir doesn't
        # change in the main script

        # subshell start
        (
            cd "$DIR"
            REMOTE=$(git config --get remote.origin.url)
            REPO=$(basename `git rev-parse --show-toplevel`)

            if [[ "$REMOTE" == "https://github.com/"* ]]; then

                echo "HTTPS repo found ($REPO) $DIR"
                git remote set-url origin git@github.com:$USERNAME/$REPO

                # Check if the conversion worked
                REMOTE=$(git config --get remote.origin.url)
                if [[ "$REMOTE" == "git@github.com:"* ]]; then
                    echo "Repo \"$REPO\" converted successfully!"
                else
                    echo "Failed to convert repo $REPO from HTTPS to SSH"
                fi

            elif [[ "$REMOTE" == "git@github.com:"* ]]; then
                echo "SSH repo - skip ($REPO) $DIR"
            else
                echo "Not Github - skip ($REPO) $DIR"
            fi
        )
        # subshell end

    fi

done

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM