Skip to content

Instantly share code, notes, and snippets.

@bpicolo
Created August 23, 2014 18:18
Show Gist options
  • Save bpicolo/71e408a45d6d0e727550 to your computer and use it in GitHub Desktop.
Save bpicolo/71e408a45d6d0e727550 to your computer and use it in GitHub Desktop.
How to git subtree
Git subtree allows you to split directories out with their own git history.
mkdir subtree && cd subtree && git init
mkdir lib && mkdir src
touch lib/__init__.py && touch src/__init__.py
printf 'def add(a, b):\n return a+b\n' > lib/add.py
printf 'from lib.add import add\nprint add(1, 4)\n' > src/secret_stuff.py
git add . && git commit -m "We have a super cool adder"
So now running "python src/secret_stuff.py" we can see our super cool 5 printed out.
So we have this awesome add function that other people might want to use,
but we don't want them to see our super secret source code.
Typing "git log" we can see our commit's sha, in this case f51226...
git show f51226 will show us our diff.
Let's split out that subtree!
>>git subtree split -P lib
-n 1/ 1 (0)
5b71377888704614c6155d6b6b04d5e047373422
The above is the sha is our subtree revision.
Now we can 'git checkout 5b713778' and 'git show':
commit 5b71377888704614c6155d6b6b04d5e047373422
Author: Ben Picolo
Date:
-n
We have a super cool adder
diff --git a/__init__.py b/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/add.py b/add.py
new file mode 100644
index 0000000..2ced335
--- /dev/null
+++ b/add.py
@@ -0,0 +1,2 @@
+def add(a, b):
+ return a+b
As you can see, this commit contains only the folder we had split in our subtree. Whereas "git show master" contains
the rest of the history.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment