Git Clone Branch & Repository

git clone command is the first step when you want to clone a remote repository or branch from github or bitbucket.

Syntax is:

git clone [--template=<template_directory>]
  [-l] [-s] [--no-hardlinks] [-q] [-n] [--bare] [--mirror]
  [-o <name>] [-b <name>] [-u <upload-pack>] [--reference <repository>]
  [--dissociate] [--separate-git-dir <git dir>]
  [--depth <depth>] [--[no-]single-branch] [--no-tags]
  [--recurse-submodules[=<pathspec>]] [--[no-]shallow-submodules]
  [--jobs <n>] [--] <repository> [<directory>]

As you see it’s a really big amount of options and I’ll explain to you how to implement the most popular use cases.

Git Clone Repository

git clone memeFirst of all, you should know the URL of a target repository.

Git supports 4 types of URL:

  • ssh://[user@]host.url[:port]/path/repo.git/
  • git://host.url[:port]/path/repo.git/
  • http[s]://host.url[:port]/path/to/repo.git/
  • ftp[s]://host.url[:port]/path/to/repo.git/

Github supports 2 of them – SSH and HTTPS.

Examples:

  • git@github.com:explainjava/javascript-snake.git
  • https://github.com/explainjava/javascript-snake.git

It’s a git repository for my javascript snake game.

Bitbucket supports SSH and HTTPS as well.

How to Clone Remote Repository?

The most popular operation is to clone a remote repository.

HTTPS example:

git clone https://github.com/explainjava/javascript-snake.git

SSH example:

git clone git@github.com:explainjava/javascript-snake.git

Success result is:

Cloning into 'javascript-snake'...
remote: Counting objects: 28, done.
remote: Total 28 (delta 0), reused 0 (delta 0), pack-reused 28
Receiving objects: 100% (28/28), 19.54 KiB | 0 bytes/s, done.
Resolving deltas: 100% (11/11), done.
Checking connectivity... done.

That means remote repository is copied to your local directory and everything is fine.

If your result is:

Cloning into 'javascript-snake'...
Permission denied (publickey).
fatal: Could not read from remote repository.
 
Please make sure you have the correct access rights
and the repository exists.

Then you should go to “How To Solve Git Clone Permission Denied (publickey) Problem” part.

How to Clone Local Repository?

It’s nothing special when you want to clone local repository – just specify a path and that’s it.

git clone /path/to/repo

How to Use Shallow Clone?

Usual clone gets each revision of each file.

It could be a problem in really big projects.

Git allows specifying --depth argument to load every file until specified revision depth.

This is called shallow clone in Git.

Example:

git clone --depth 1 https://github.com/explainjava/javascript-snake.git

This will copy only the latest revision in the repository.

You’ll see a really huge difference in downloading time while copying big projects.

Usually, copy only last revision takes just a few seconds – this is a way to reduce Git clone time.

There was a limited support of shallow cloning until Git 1.9, but nowadays push and pull operation were improved and you can use it without any problems.

How to Clone SubDirectory?

Git doesn’t have a command to clone subdirectory, that’s why it’s a little bit tricky.

First of all, you need to create a local directory manually and add remote origin.

mkdir <repo>
cd <repo>
git init
git remote add -f origin <url>

You can get URL here:

Example:

mkdir javascript-snake
cd javascript-snake
git init
git remote add -f origin https://github.com/explainjava/javascript-snake.git

These commands will create a local repository assigned to a remote repository, fetch all objects, but will not check it out.

Then we need to enable sparseCheckout feature:

git config core.sparseCheckout true

Now you need to specify directories and files that you want to check out:

echo "/dir/" >> .git/info/sparse-checkout

In my case I’ve added 2 files:

echo "snake.js" >> .git/info/sparse-checkout
echo "snake.css" >> .git/info/sparse-checkout

The last step is to update repository:

git pull origin master

Result:

dshvechikov@dshvechikov-N552VX:~/workspace/javascript-snake$ ll
total 28
drwxrwxr-x  3 dshvechikov dshvechikov 4096 mar 30 22:26 ./
drwxrwxr-x 12 dshvechikov dshvechikov 4096 mar 30 22:23 ../
drwxrwxr-x  8 dshvechikov dshvechikov 4096 mar 30 22:26 .git/
-rw-rw-r--  1 dshvechikov dshvechikov 1049 mar 30 22:26 snake.css
-rw-rw-r--  1 dshvechikov dshvechikov 8372 mar 30 22:26 snake.js

I’ve cloned 2 of 5 files.

What is a Difference Between Clone –mirror and –bare?

2 more git clone arguments that you have to know: --bare and --mirror.

Let’s take a look what is it and what is a difference.

Imagine that origin have:

branches (master, branch1, branch2)
tags (v1, v1.1, v1.2)
remote branches (r1/master, r2/master)
refs (refs/remotes/origin/r1, refs/remotes/origin/r2).

Let’s compare non-bare, bare and mirror clones and what they will copy:

Clone TypeLocal BranchesTagsRemote BranchesRefs
non-baremaster+origin/master
origin/branch1
origin/branch2
bareaster
branch1
branch2
+
mirrormaster
branch1
branch2
+++

Non-bare clone example: git clone .

Bare clone example: git clone --bare .

Mirror clone example: git clone --mirror .

Git Clone Branch

Clone branch in Git is one more task that you can be looking for.

Usually, you want to clone a specific branch or clone all existing branches.

Let’s take a look how to do that.

How to Clone a Specific Branch?

To clone a specific branch you can use -b parameter.

Syntax:

git clone -b <branch> <URL>

Example:

git clone -b test https://github.com/explainjava/javascript-snake.git

How to Clone All Branches?

As for me, usual cloning without parameters is enough.

Remote branches are “hiding”.

You can see it using the following command:

git branch -a

Result:

* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
  remotes/origin/test

Switch to specific branch:

git checkout -b <local-branch-namae> <remote-branch-name>

Example:

git checkout -b test origin/test

As an alternative, you can use bare or mirror clone described above.

How To Solve Git Clone Permission Denied (publickey) Problem?

If you’re reading this part that means you’ve got an error like this:

Cloning into 'javascript-snake'...
Permission denied (publickey).
fatal: Could not read from remote repository.
 
Please make sure you have the correct access rights
and the repository exists.

Fix it really easy – you need to generate SSH public key and upload it to your remote repository, e.g. github.

Open your terminal and type: cd ~/.ssh && ssh-keygen (go to ssh folder and generate SSH key).

The id_rsa.pub file should appear in ssh folder.

-rw-r--r-- 1 dshvechikov dshvechikov 412 feb 9 2017 id_rsa.pub

Open it using a text editor and copy all text inside to the clipboard.

Then go to github (of course, if your code is there) to Settings:

Go to SSH and GPG Keys:

Add a New SSH Key and paste a text from your id_rsa.pub into a Key field:

Save your changes and try to clone repository again.

Conclusion

I’ve explained the most popular use cases of git clone command.

It should be working on Windows, Linux, and MasOS as well.

If something is wrong or you need to do something special – leave a comment and I’ll try to help.

Leave a Comment