How to clone a git tree or branch?

It is easy to clone a git repository to your local machine using the git clone command. But this command will fail with a fatal error if you simply try to clone a branch or a tree of the repository by using the branch or tree URL.

So first let us see the command to clone a git repository

%MINIFYHTML29e35a3e31f54967d44db47a3e9f10b943%

git clone repository_path

For example

git clone https://github.com/alpinejs/alpine

However, if you try to clone its branch or tree https://github.com/alpinejs/alpine/tree/fix-ie11 using the above method, the command will fail.

git clone https://github.com/alpinejs/alpine/tree/fix-ie11

The above command will return an error message “fatal: repository ‘https://github.com/alpinejs/alpine/tree/fix-ie11/’ not found”

The trick is to use the branch flag to clone a specific branch or a tree from git repository as follows

git clone -b branch_name repository_path

So, to clone the branch fix-ie11 of the repository, instead of using the tree url use the root url and mention the branch name using the branch flag. The command hence would be

git clone -b fix-ie11 https://github.com/alpinejs/alpine

Note that we have used the master branch url of the repository and only mentioned the branch name in the -b option. Now the specific branch would be cloned to the local machine successully. Happy Gitting!