Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to merge a git repository with submodules into another with subtree merge?

I have a dotfiles repository. I want to merge another one with submodules (prezto) into it, so I can conveniently have everything in one repository without the inconvenience of submodules.

How can I subtree merge prezto into my dotfiles repository with all its submodules?

I can use submodules, I can split and merge subtrees either the "old fashioned" way or with the newer git subtree tool.

I just don't know how to do this specific case.

like image 624
kissgyorgy Avatar asked Dec 05 '15 15:12

kissgyorgy


1 Answers

Alexander Mikhailian created a script to convert all the submodules in subtrees, you should be able to adapt it to your case.

Presenting here the steps:

cat .gitmodules |while read i
do
  if [[ $i == \[submodule* ]]; then
    mpath=$(echo $i | cut -d\" -f2)
    read i; read i;
    murl=$(echo $i|cut -d\  -f3)
    mcommit=`eval "git submodule status ${mpath} |cut -d\  -f2"`
    mname=$(basename $mpath)
    echo -e "$name\t$mpath\t$murl\t$mcommit"
    git submodule deinit $mpath
    git rm -r --cached $mpath
    rm -rf $mpath
    git remote add $mname $murl
    git fetch $mname
    git branch _$mname $mcommit
    git read-tree --prefix=$mpath/ -u _$mname
fi
done
git rm .gitmodules

I think the easiest route for you would be to apply the script on the prezto repo and then make the repo a subtree in the dotfiles repository.

After executing the script in the prezto repository you will use the following steps to create the subtree:

  1. Add a new remote URL pointing to the prezto repo into the dotfiles.

    git remote add -f prezto [email protected]:path/prezto.git
    
  2. Merge the prezto project into the local dotfiles project. This doesn't change any of your files locally, but it does prepare Git for the next step.

    git merge -s ours --no-commit prezto/master
    
  3. Create a new directory called prezto-subdir (or whatever you like), and copy the Git history of the prezto project into it.

    git read-tree --prefix=prezto-subdir/ -u prezto/master
    
  4. Commit the changes to keep them safe.

    git commit -m "Subtree merged in prezto"
    
like image 160
dan Avatar answered Sep 20 '22 13:09

dan