把已有的git项目push到多个remote仓库上

2018-04-27 2581点热度 1人点赞 0条评论

已有项目介绍

比如已经有了一个名叫util的项目,默认推送到origin的远程仓库上,git的config配置类似如下:

[core]
        repositoryformatversion = 0
        filemode = false
        bare = false
        logallrefupdates = true
        symlinks = false
        ignorecase = true
[remote "origin"]
        url = ssh://wangxianfeng@aiuyo.com:58922/volume1/homes/wangxianfeng/gitrepositorys/util/.git/
        fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
        remote = origin
        merge = refs/heads/master

推送到另外一个git仓库里

现在有个需求就是要把这个项目push到2个远程仓库上,操作步骤如下:

1. 初始化要推送的远程仓库

比如在gitlab上创建了一个远程仓库,地址是

https://gitlab.com/wangxianfeng/util.git

也可以使用命令行进行初始化,操作命令如下:

mkdir util
cd util
git init

2.添加远程仓库地址

git remote add gitlab https://gitlab.com/wangxianfeng/util.git

3.添加本地所有文件

git add .

4.提交本地修改

git commit -m "push to gitlab init"

5.push到远程仓库,需要指定仓库名和远程仓库分支

git push gitlab master

push的时候输入正确的用户名和密码即可推送成功。
第一个gitlab是本地分支名,第二个master是远程分支名

具有2个远程仓库的git config文件内容

[core]
        repositoryformatversion = 0
        filemode = false
        bare = false
        logallrefupdates = true
        symlinks = false
        ignorecase = true
[remote "origin"]
        url = ssh://wangxianfeng@aiuyo.com:58922/volume1/homes/wangxianfeng/gitrepositorys/util/.git/
        fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
        remote = origin
        merge = refs/heads/master
[remote "gitlab"]
        url = https://gitlab.com/wangxianfeng/util.git
        fetch = +refs/heads/*:refs/remotes/gitlab/*

git push 的其他命令

(1) git push -u origin master 如果当前分支与多个主机存在追踪关系,则可以使用 -u 参数指定一个默认主机,这样后面就可以不加任何参数使用git push,

不带任何参数的git push,默认只推送当前分支,这叫做simple方式,还有一种matching方式,会推送所有有对应的远程分支的本地分支, Git 2.0之前默认使用matching,现在改为simple方式

如果想更改设置,可以使用git config命令。git config --global push.default matching OR git config --global push.default simple;可以使用git config -l 查看配置

(2) git push --all origin 当遇到这种情况就是不管是否存在对应的远程分支,将本地的所有分支都推送到远程主机,这时需要 -all 选项

(3) git push --force origin git push的时候需要本地先git pull更新到跟服务器版本一致,如果本地版本库比远程服务器上的低,那么一般会提示你git pull更新,如果一定要提交,那么可以使用这个命令。

(4) git push origin --tags //git push 的时候不会推送分支,如果一定要推送标签的话那么可以使用这个命令

王显锋

激情工作,快乐生活!

文章评论