DEV Community

Tony Metzidis
Tony Metzidis

Posted on

Archive Legacy Github Repos with Subtree

If you're like me your github account is littered with hundreds of legacy experimental repos that you don't want to toss.

git subtree is the magic needed to consolidate them into an archive, preserving all of the commit history . Once consolidated, all of those legacy repos can be archived to your cloud storage and deleted from github.

Clone the Archive Repo

First step is to choose (or create) your archive repo that will contain all of your experiments

USERNAME=tony
REPO=archive
git clone git@github.com:$USERNAME/$REPO.git
cd $REPO
Enter fullscreen mode Exit fullscreen mode

Clone one or more Experiment Repos (you would like to delete)

USERNAME=tony
REPO=experiment-web
git clone git@github.com:$USERNAME/$REPO.git
Enter fullscreen mode Exit fullscreen mode

Add the local experiment-web as a "remote"

note: Git remotes can be copies from a local directory. This is the magical source of subtree's power to fan-in or fan-out repos

cd archive
name=experiment-web ; git remote add subtree/$name ../$name  
Enter fullscreen mode Exit fullscreen mode

Do this any number of times for all of your experiment Repos

Subtree The Repo and Test the Results

# stay within Archive
name=experiment-web
git subtree add -P $name subtree/$name master # or any branch
Enter fullscreen mode Exit fullscreen mode

Test that all the contents match exactly

cd $name
diff -r . ../../$name
# Only in ../../experiment-web: .git
Enter fullscreen mode Exit fullscreen mode

Test that the subtree'd log matches the log of the original repo

git log --graph -4
*   a19d970 - (HEAD -> master, tonymet/master) Add 'oh-my-zsh/' from commit '9f3f282699dccc11221debbdbec4be654c63ac83' (11 minutes ago) <tony>
|\
| * 9f3f282 - (subtree/oh-my-zsh-origin/anthony) macbook (14 minutes ago) 

Enter fullscreen mode Exit fullscreen mode

Archive and Delete The Repo

Now you can archive the repo to your cloud storage ( Google Cloud , etc).

# Save to TGZ file and copy to your Google Drive
name=experiment-web && tar -zcf archive/$name-`date +%Y-%m-%d`.tgz $name &&  rm -rf $name  
# Visit repo on the web to delete
Enter fullscreen mode Exit fullscreen mode

Top comments (0)