在 Git 中,你不能直接配置同时向两个仓库进行 pull 和 push。但是,你可以通过为远程仓库设置多个 URL 来达到这个目的。这里以 GitHub 和 GitLab 为例。
首先,你需要在本地仓库中添加两个远程仓库,一个为 GitHub 的仓库,一个为 GitLab 的仓库。在你的 Git 仓库的目录下,打开终端,输入以下命令:
# 添加 GitHub 仓库
git remote add github https://github.com/你的用户名/你的仓库名.git
# 添加 GitLab 仓库
git remote add gitlab https://gitlab.com/你的用户名/你的仓库名.git
接下来你可以通过以下命令来 pull 或 push 到指定的远程仓库:
# 从 GitHub 仓库 pull
git pull github master
# 从 GitLab 仓库 pull
git pull gitlab master
# push 到 GitHub 仓库
git push github master
# push 到 GitLab 仓库
git push gitlab master
如果你想要同时 push 到两个仓库,你可以在 Git 里设置一个新的远程,它包含了你想要 push 的所有仓库:
git remote set-url --add --push origin https://github.com/你的用户名/你的仓库名.git
git remote set-url --add --push origin https://gitlab.com/你的用户名/你的仓库名.git
这样,当你使用 git push origin
时,Git 会同时将代码 push 到这两个仓库。
注意,上述操作都需要你有对应仓库的访问权限。另外,每一个远程仓库可能都有自己的认证方式,你需要确保你有正确的认证信息才能进行这些操作。
也可以直接编辑.config
文件:
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
[remote "origin"]
url = https://github.com/你的用户名/你的仓库名.git
fetch = +refs/heads/*:refs/remotes/origin/*
url = https://gitlab.com/你的用户名/你的仓库名.git
[branch "main"]
remote = origin
merge = refs/heads/main
评论区