X
Popular Searches

How to Clone or Download a Specific Branch from Github

Github is great for storing files, but sometimes the files you want are stored on a different Git branch, and aren’t easily accessible from the main site. We’ll show how to download and clone files from other branches.

“Download ZIP” Will Work

Github doesn’t quite make the behaviour of their download feature very clear. If you swap to a different branch with the dropdown, you’ll continuing viewing that branch, at least until you navigate away from the file viewer.

While on a branch, clicking “Download Zip” from the Code dropdown will lead you to a download for the specific branch you’re on. It doesn’t tell you this on the site though, so you’ll have to make sure that the filename for the download URL matches the correct branch, named in reponame-branchname.zip format.

The same goes for direct downloads via the “Raw” button—they’ll link to a specific branch, which you can change in the URL.

https://raw.githubusercontent.com/username/Repo/Branch/readme.md

Cloning A Single Branch (The Right Way)

However, downloading as a zip has a lot of downsides, and breaks Git history. You’ll want to clone the branch using the git clone command.

Advertisement

You may have tried this only to find that you accidentally downloaded the master branch. This is because, even if you’re switched to a branch on the website, Github only gives you the URL to download the repo from. It does not tell you how you should download it.

https://github.com/username/Repo.git

If you take this URL, and run git clone, it will download the default branch, usually master. You can change this with some flags, usually done in one of two ways:

git clone --branch dev https://github.com/username/Repo.git

git clone --branch dev --single-branch https://github.com/username/Repo.git

The first will clone the entire repo, and checkout the dev branch. The second, using the --single-branch flag, will only fetch updates that pertain to the branch being downloaded. This can be faster if you have a lot of files on other branches you don’t care about.

Fixing a Repo Downloaded From Master Branch

If you’re reading this because you already ran git clone without the --branch flag, don’t worry, you can simply switch to the other branch:

git switch dev

If you made changes on the master branch without realizing, you can move those changes to the new branch by using git checkout instead with the -b flag:

git checkout -b

You can also use git stash, which lets you store the changes and “pop” the stash open at a later time:

git stash
git switch dev
git stash pop
Anthony Heddings Anthony Heddings
Anthony Heddings is the resident cloud engineer for LifeSavvy Media, a technical writer, programmer, and an expert at Amazon's AWS platform. He's written hundreds of articles for How-To Geek and CloudSavvy IT that have been read millions of times. Read Full Bio »

The above article may contain affiliate links, which help support CloudSavvy IT.