Skip to content
Skip to the article
In Git & Dev Tools: 3 articles
Git & Dev Tools

Git Cheatsheet

Git recipes for fast shallow clones, fetching all branches after a single-branch clone, and pruning stale remote branches.

Updated
Applies to
  • Git 2.40+
Tags
  • git
  • cheatsheet
Reading time
2 min

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>.git

Fetch the Full History of a Shallow Clone

Turn a shallow clone into a normal one:

git fetch --unshallow

Fetch 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 --prune

git 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 --prune

To prune automatically on every fetch:

git config --global fetch.prune true

Sources

This article is in the public domain (CC0 1.0), code samples included. Use it however helps you.