How to clone or download a specific branch from Github – CloudSavvy IT

Github is great for saving files, but sometimes the files you want are stored on another Git branch and are not easily accessible from the main page. We show you how to download and clone files from other branches.

“Download ZIP” will work

Github does not quite make the behavior of their download feature very clear. If you switch to another branch with the drop-down menu, you will continue to see that branch, at least until you navigate away from the file viewer.

While on a branch, clicking “Download Zip” from the Code drop-down menu will take you to a download for the specific branch you are on. However, it does not tell you this on the site, so you need to make sure that the file name of the download URL matches the correct branch named in format.

The same goes for direct downloads via the “Raw” button – they will link to a specific branch that you can change in the URL.

Cloning a single branch (the right way)

But downloading as a zip has many drawbacks and breaks the Git story. You want to clone the branch using git clone command.

You may have only tried this to find out that you accidentally downloaded master department. This is because even if you have switched to a branch on the website, Github only gives you 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 usually download the default branch master. You can change this with some flags, which are usually done in one of two ways:

git clone --branch dev

git clone --branch dev --single-branch

The first will clone the entire repo and check out dev department. The other, using --single-branch flag will only download updates related to the branch being downloaded. This can be faster if you have many files on other branches that you do not care about.

Repair of a repo downloaded from Master Branch

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

git switch dev

If you have made changes to master branch without being aware of it, you can move these changes to the new branch by using git checkout instead of -b flag:

git checkout -b

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

git stash
git switch dev
git stash pop

Leave a Comment