Split large Git repository into many smaller ones

前端 未结 5 2100
故里飘歌
故里飘歌 2020-11-27 10:12

After successfully converting an SVN repository to Git, I now have a very large Git repository that I want to break down into multiple smaller repositories and maintain hist

5条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-27 10:25

    You could use git filter-branch --index-filter with git rm --cached to delete the unwanted directories from clones/copies of your original repository.

    For example:

    trim_repo() { : trim_repo src dst dir-to-trim-out...
      : uses printf %q: needs bash, zsh, or maybe ksh
      git clone "$1" "$2" &&
      (
        cd "$2" &&
        shift 2 &&
    
        : mirror original branches &&
        git checkout HEAD~0 2>/dev/null &&
        d=$(printf ' %q' "$@") &&
        git for-each-ref --shell --format='
          o=%(refname:short) b=${o#origin/} &&
          if test -n "$b" && test "$b" != HEAD; then 
            git branch --force --no-track "$b" "$o"
          fi
        ' refs/remotes/origin/ | sh -e &&
        git checkout - &&
        git remote rm origin &&
    
        : do the filtering &&
        git filter-branch \
          --index-filter 'git rm --ignore-unmatch --cached -r -- '"$d" \
          --tag-name-filter cat \
          --prune-empty \
          -- --all
      )
    }
    trim_repo MyHugeRepo MyABRepo DIR_1 DIR_2
    trim_repo MyHugeRepo My12Repo DIR_A DIR_B
    

    You will need to manually delete each repository’s unneeded branches or tags (e.g. if you had a feature-x-for-AB branch, then you probably want to delete that from the “12” repository).

提交回复
热议问题