git splitting repository by subfolder and retain all old branches

后端 未结 2 1715
鱼传尺愫
鱼传尺愫 2020-12-12 15:47

I have a git repo with 2 directories and multiple branches, I want to split them and create all branches

`-- Big-repo
    |-- dir1
    `-- dir2

Branches : b         


        
2条回答
  •  轮回少年
    2020-12-12 16:27

    This script does the job for me:

    #!/bin/bash
    
    set -e
    
    if [ -z "$3" ]; then
            echo "usage: $0 /full/path/to/repository path/to/splitfolder/from/repository/root new_origin"
            exit
    fi
    
    repoDir=$1
    folder=$2
    newOrigin=$3
    
    cd $repoDir
    
    git checkout --detach
    git branch | grep --invert-match "*" | xargs git branch -D
    
    for remote in `git branch --remotes | grep --invert-match "\->"`
    do
            git checkout --track $remote
            git add -vA *
            git commit -vam "Changes from $remote" || true
    done
    
    git remote remove origin
    git filter-branch --prune-empty --subdirectory-filter $folder -- --all
    
    #prune old objects
    rm -rf .git/refs/original/*
    git reflog expire --all --expire-unreachable=0
    git repack -A -d
    git prune
    
    #upload to new remote
    git remote add origin $newOrigin
    git push origin master
    
    for branch in `git branch | grep -v '\*'`
    do
            git push origin $branch
    done
    

提交回复
热议问题