工作中有个别项目因为一些原因,需要需要在两个不同的代码仓库间提交、同步同一份代码,这时,开发者可以通过在同一个本地git仓库中,关联不同的远程仓库来实现。首先要确认的是,开发者同时拥有两个仓库的账号及操作权限,比如要操作的是两个不同gitlab的仓库,需要分别在gitlab账号中添加SSH Key。
$ git remote origin使用这个命令会列出所有远程仓库的简短别名,我们使用git clone命令克隆一个仓库时,git会默认给它命名为origin。
$ git remote -v origin ssh://git@gitlab.xxx.cn/git-demo.git (fetch) origin ssh://git@gitlab.xxx.cn/git-demo.git (push)加上-v参数后,会列出别名及地址详细信息,表示我们拥有对应远程仓库的拉取、推送权限。
另外,所有远程仓库信息被保存在本地文件.git/config中。
$ git remote add <name> <url>重命名
$ git remoet rename <old-name> <new-name>删除远程仓库
$ git remove <name> $ git rm <name>
$ git fetch <remote-name> <branch-name>检出远程分支到本地:
$ git checkout -b <local-branch-name> <remote-name>/<remote-branch-name>git add, git commit之后推送代码到远程分支
$ git push -u <remote-name> <branch-name> $ git merge <branch-name>
$ git merge <branch> fatal: 拒绝合并无关的历史通过添加--allow-unrelated-histories来解决:
$ git merge <branch> --allow-unrelated-histories本地分支名与远程分支名不同时,有可能错误操作推送产生新的分支,可以使用以下命令删除不需要的远程分支:
$ git push origin -d "branch-name"总结一下