美文网首页
Git多仓库同步全部分支代码

Git多仓库同步全部分支代码

作者: tenlee | 来源:发表于2017-03-06 19:08 被阅读344次

将一个git远程仓库的所有分支代码同步到另外一个git仓库。

#!/usr/bin/env python3
#coding: utf-8

import os
import sys

git_url = 'git@git.coding.net:tenlee'
remote_name = 'coding'

def get_all_branch_form_path(path):
    branchs = os.popen('cd {} && git branch -r'.format(path))
    all_branch = []
    for branch in branchs.readlines():
        branch = branch.strip()
        if branch.find('HEAD') > 0:
            branch = branch.split()[2]
        branch = branch.split('/')[1]
        all_branch.append(branch)
    return all_branch

def is_git_dir(dir):
    if os.path.isdir(dir):  # 是文件夹
        sub_dirs = os.listdir(dir)
        if '.git' in sub_dirs:
            return True
    return False

def do_push(path):
    for file in os.listdir(path):
        file_path = os.path.join(path, file)
        if is_git_dir(os.path.abspath(file_path)):
            all_branch = get_all_branch_form_path(file_path)
            for branch in all_branch:
                cmd = ('cd {path} &&' 
                        + ' git remote add {remote} {git_url}/{project}.git').format(
                                path=file_path, remote=remote_name,
                                git_url=git_url, project=file)
                os.system(cmd)

                cmd = ('cd {path} &&' 
                        + ' git checkout {branch}').format(
                            path=file_path, branch=branch)
                os.system(cmd)

                cmd = ('cd {path} &&  git push {remote} {branch}').format(
                            path=file_path, remote=remote_name,
                            branch=branch)
                print(cmd)
                if os.system(cmd) != 0:
                    print('Push Error')
                else:
                    print('Push Success')


def main():
    path = os.getcwd()
    if len(sys.argv) > 1:
        path = os.argv[1]
    do_push(path)

if __name__ == '__main__':
    main()

相关文章

  • Git多仓库同步全部分支代码

    将一个git远程仓库的所有分支代码同步到另外一个git仓库。

  • Git

    Git图解 仓库 配置 增加/删除文件 代码提交 分支 标签 查看信息 远程同步 撤销 其他

  • github常规操作

    从命令行创建一个新的仓库: 跟主支保持同步 git分支开发,分支(feature)同步主干(master)代码,以...

  • 常用Git命令

    远程同步 取回远程仓库的变化,并与本地分支合并:git pull [remote] [branch] 代码提交 添...

  • git同步远程仓库的所有分支

    git同步远程仓库的所有分支 git clone克隆远程仓库默认是只克隆master分支,当想把远程仓库上的所有的...

  • git常用指令

    下载远程仓库代码 git clone 代码仓库地址 从远程仓库拉取代码 git pull 提交代码到本地分支,并推...

  • Git Rebase

    保证本地分支和远程私有仓库分支是同步的 git fetchgit rebase origin develop (从...

  • git同步远程仓库分支

    git命令 何谓同步远程分支?有几种情况: 本地有新分支,远程仓库没有。 远程仓库有新分支,本地没有。 本地删除了...

  • 2020-06-14 git关联远程分支跟远程分支代码同步

    应用场景:假设我们现在git远程仓库已经有代码了 我现在要把我本地的分支代码跟远程同步合并然后推送到远程仓库st...

  • git 工具

    初始化git仓库 添加文件 查看状态 提交文件 提交文件到远程仓库 更新远程仓库代码 查看分支 创建分支 提交分支...

网友评论

      本文标题:Git多仓库同步全部分支代码

      本文链接:https://www.haomeiwen.com/subject/bpvugttx.html