Git Cheatsheet
Git recipes for fast shallow clones, fetching all branches after a single-branch clone, and pruning stale remote branches.
On this page
Git commands that come up often in Avunu work, especially when pulling down large upstream projects to test a branch or patch.
Clone Only the Latest Commit of One Branch
Fetch a single branch with no history. This makes cloning large repositories, such as the Linux kernel or WooCommerce, take seconds instead of minutes:
git clone --depth 1 --branch <BRANCH> https://github.com/<OWNER>/<REPO>.git <DIRECTORY>--depth 1 implies --single-branch, so only <BRANCH> is fetched. --branch also accepts a tag.
Clone Without File History but with All Commits
A blobless partial clone downloads every commit and tree but fetches file contents only when you check them out. Unlike --depth 1, git log and git blame still work:
git clone --filter=blob:none https://github.com/<OWNER>/<REPO>.gitFetch the Full History of a Shallow Clone
Turn a shallow clone into a normal one:
git fetch --unshallowFetch All Branches After a Single-Branch Clone
A shallow or --single-branch clone configures the remote to fetch only one branch, so other branches never show up in git branch -r. Reset the remote's fetch refspec to track every branch, then fetch:
git remote set-branches origin '*'
git fetch --all --prunegit remote set-branches origin '*' is the same as setting the refspec directly:
git config remote.origin.fetch '+refs/heads/*:refs/remotes/origin/*'To fetch every branch from a remote once without changing its configuration, pass the refspec on the command line, for example for a remote named upstream:
git fetch upstream '+refs/heads/*:refs/remotes/upstream/*'Prune Deleted Remote Branches
Remove remote-tracking branches that no longer exist on any remote:
git fetch --all --pruneTo prune automatically on every fetch:
git config --global fetch.prune trueSources
This article is in the public domain (CC0 1.0), code samples included. Use it however helps you.