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.
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.
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.
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:
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:
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:
You can also use git stash, which lets you store the changes and “pop” the stash open at a later time: