How to split subdirectory into a separate git repository

less than 1 minute read

In this post, I’ll show you how to split tools/perf in linux tree into an separate git repository without losing the commit history.

Clone linux git repo from https://git.kernel.org/?q=linus

git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git

Create a new branch:

pushd ~/perf/linux
git subtree split -P tools/perf -b perf-tools
popd

Note: You need to run this command from the toplevel of the working tree.

Pull from branch created above:

mkdir ~/linux-tools-perf
pushd ~/linux-tools-perf
git init
git pull ~/perf/linux perf-tools

Push new git repo to github:

git remote add origin git@github.com:fudongbai/linux-tools-perf.git
git push origin -u master
popd

Resources

Detach (move) subdirectory into separate Git repository