Skip to content

Git 配置

由 HTTPS 切换为 ssh

服务器一般都采用公钥拉取代码,很少采用账号密码的方式

记录几条关键命令

  1. 查看当前仓库 URL 地址

    shell
    git remote -v

    这会列出所有远程仓库及其对应的 URL。例如,你可能会看到类似下面的输出:

    shell
    origin  https://gitee.com/username/repository.git (fetch)
    origin  https://gitee.com/username/repository.git (push)
  2. 复制项目的 ssh 地址,然后使用命令 git remote set-url 来重新设置 URL

    shell
    git remote set-url origin git@gitee.com:username/repository.git
  3. 用命令 git remote -v 查看一下,URL 是否已经变成了 ssh 地址

    shell
    git remote -v

    你应该看到类似下面的输出:

    shell
    origin  git@github.com:username/repository.git (fetch)
    origin  git@github.com:username/repository.git (push)
  4. 最后验证一下是否成功

    shell
    git pull

设置多个上游分支

远程仓库到本地项目,这里首先就是三个平台远程仓库地址添加到本地,我们需要用 git remote add 分别执行

shell
git remote add gitee   https://gitee.com/username/repository.git 
git remote add github  https://github.com/username/repository.git 
git remote add gitcode https://gitcode.com/username/repository.git
shell
# 推送到 GitHub 
git push github master 

# 推送到 Gitee 
git push gitee master 

# 推送到 GitCode 
git push gitcode master