Git sparse checkout


I just wanted to share something in git I learned rather recently. Don’t laugh either because I I know a whole lot of you are probably like, “git experts” and thinking… “c’mon son? You just now figuring that out?” If that is you keep your mouth shut. This post is not intended for you. It is instead intended for those of you who are still parked in the Subversion mindset. It is for Old timers like myself who are delighted when they finally figure out what the rest of the tech community has been raving about for years. Hi, I’m Cliff. you’re here because you’re getting long in the tooth and trying to keep up with technology. I’m here because my teeth are just as long. I’m also here to tell you that you really don’t need to clone the whole git repo.

Here’s the story (because a lot of my posts have some sort of backstory): I was trying to figure out how to get gdb working in a Gradle-managed Android Studio project. Y’see, I’ve already managed to get an Android Studio project with a native component to build and run on both the device and the emulator. Now I’m trying my hand at C++/gdb debugging. Something I’d never done even on a regular ant managed project. (What’s that? You didn’t know that you can use Android Studio and Gradle to build/run an Android project with native libraries? Stay tuned for the updated post that I thought I’d already told you about!) That venture got me looking for the source to the Android plugin for Gradle. There are a couple of android plugins but I wanted the one that was running as part of my Android Studio project. I found the source and was intimidated by the layout of the source on Google’s site. Intimidated, I immediately told myself I wasn’t trying to checkout the entire thing onto my tiny 256GB HD. I have a strong distaste for checking out projects with huge a source base. (I later learned that the part that I needed to check out wasn’t really all that big and negated the need for both my exploration into git sparse checkouts and this rather long blog update but it was a fun exercise none the less.) So here’s the short instructions on how to do it. I found out from this S/O post.

Assuming you want to checkout the new Gradle build system but only want the gradle subfolder from the 4.3_r0.9 tag as I did you would do something like this:

$ mkdir android-tools
$ cd android-tools/
$ git init
Initialized empty Git repository in /Users/clifton/dev/android-tools/.git/
$ git remote add -f origin https://android.googlesource.com/platform/tools/buildUpdating origin
remote: Counting objects: 15, done
remote: Finding sources: 100% (15/15)
remote: Total 11593 (delta 2852), reused 11593 (delta 2852)
Receiving objects: 100% (11593/11593), 6.09 MiB | 2.49 MiB/s, done.
Resolving deltas: 100% (2853/2853), done.
From https://android.googlesource.com/platform/tools/build
* [new branch] jb-mr1-dev -> origin/jb-mr1-dev
* [new branch] jb-mr1-dev-plus-aosp -> origin/jb-mr1-dev-plus-aosp
* [new branch] jb-mr1-release -> origin/jb-mr1-release
* [new branch] jb-mr1.1-dev -> origin/jb-mr1.1-dev
* [new branch] jb-mr1.1-dev-plus-aosp -> origin/jb-mr1.1-dev-plus-aosp
* [new branch] jb-mr1.1-release -> origin/jb-mr1.1-release
* [new branch] jb-mr2-dev -> origin/jb-mr2-dev
* [new branch] jb-mr2-release -> origin/jb-mr2-release
* [new branch] jb-mr2.0-release -> origin/jb-mr2.0-release
* [new branch] master -> origin/master
* [new branch] tools_r21 -> origin/tools_r21
* [new branch] tools_r22 -> origin/tools_r22
* [new branch] tools_r22.2 -> origin/tools_r22.2
* [new branch] version_0.5 -> origin/version_0.5
* [new tag] android-4.2.1_r1 -> android-4.2.1_r1
* [new tag] android-4.2.1_r1.1 -> android-4.2.1_r1.1
* [new tag] android-4.2.1_r1.2 -> android-4.2.1_r1.2
* [new tag] android-4.2.2_r1 -> android-4.2.2_r1
* [new tag] android-4.2.2_r1.1 -> android-4.2.2_r1.1
* [new tag] android-4.2.2_r1.2 -> android-4.2.2_r1.2
* [new tag] android-4.2_r1 -> android-4.2_r1
* [new tag] android-4.3_r0.9 -> android-4.3_r0.9
* [new tag] android-4.3_r0.9.1 -> android-4.3_r0.9.1
* [new tag] android-4.3_r1 -> android-4.3_r1
* [new tag] android-4.3_r2 -> android-4.3_r2
* [new tag] android-4.3_r2.1 -> android-4.3_r2.1
* [new tag] android-cts-4.2_r1 -> android-cts-4.2_r1
* [new tag] android-cts-4.2_r2 -> android-cts-4.2_r2
* [new tag] android-sdk-support_r11 -> android-sdk-support_r11
remote: Counting objects: 4, done
remote: Finding sources: 100% (4/4)
remote: Total 4 (delta 0), reused 4 (delta 0)
Unpacking objects: 100% (4/4), done.
From https://android.googlesource.com/platform/tools/build
* [new tag] version_0.5.0 -> version_0.5.0
* [new tag] version_0.5.1 -> version_0.5.1
* [new tag] version_0.5.2 -> version_0.5.2
* [new tag] version_0.5.3 -> version_0.5.3
$ git config core.sparseCheckout true
$ echo 'gradle/*' >.git/info/sparse-checkout
$ git checkout android-4.3_r0.9
Note: checking out 'android-4.3_r0.9'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

git checkout -b new_branch_name

HEAD is now at 70d8da9... Merge "Move version to 22."

As you can see above there are quite a few tags associated with this project but so far the sparse checkout seems to have worked as advertised. I’m not sure how much space savings I’m seeing over cloning the entire project but hopefully this tip will serve to help others, or myself as I sometimes look over old posts to remember how I did something.

One thought on “Git sparse checkout

Leave a comment