How do you merge two Git repositories?

后端 未结 23 3795
耶瑟儿~
耶瑟儿~ 2020-11-21 05:45

Consider the following scenario:

I have developed a small experimental project A in its own Git repo. It has now matured, and I\'d like A to be part of larger projec

23条回答
  •  离开以前
    2020-11-21 06:14

    I know it's long after the fact, but I wasn't happy with the other answers I found here, so I wrote this:

    me=$(basename $0)
    
    TMP=$(mktemp -d /tmp/$me.XXXXXXXX)
    echo 
    echo "building new repo in $TMP"
    echo
    sleep 1
    
    set -e
    
    cd $TMP
    mkdir new-repo
    cd new-repo
        git init
        cd ..
    
    x=0
    while [ -n "$1" ]; do
        repo="$1"; shift
        git clone "$repo"
        dirname=$(basename $repo | sed -e 's/\s/-/g')
        if [[ $dirname =~ ^git:.*\.git$ ]]; then
            dirname=$(echo $dirname | sed s/.git$//)
        fi
    
        cd $dirname
            git remote rm origin
            git filter-branch --tree-filter \
                "(mkdir -p $dirname; find . -maxdepth 1 ! -name . ! -name .git ! -name $dirname -exec mv {} $dirname/ \;)"
            cd ..
    
        cd new-repo
            git pull --no-commit ../$dirname
            [ $x -gt 0 ] && git commit -m "merge made by $me"
            cd ..
    
        x=$(( x + 1 ))
    done
    

提交回复
热议问题