Git Clone (Repository, Branch) Tutorial

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

Syntax is:

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.

git repository url on github

Bitbucket supports SSH and HTTPS as well.

How to Clone Remote Repository?

The most popular operation is to clone a remote repository.

HTTPS example:

SSH example:

Success result is:

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

If your result is:

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.

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:

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.

You can get URL here:

git repository url on github

Example:

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:

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

In my case I’ve added 2 files:

The last step is to update repository:

Result:

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 Type Local Branches Tags Remote Branches Refs
non-bare
  • master
+
  • origin/master
  • origin/branch1
  • origin/branch2
bare
  • master
  • branch1
  • branch2
+  –
mirror
  • master
  • branch1
  • branch2
+ + +

Non-bare clone example:  git clone <URL>.

Bare clone example:  git clone --bare <URL>.

Mirror clone example:  git clone --mirror <URL>.

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:

Example:

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:

Result:

Switch to specific branch:

Example:

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:

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.

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:

github settings

Go to SSH and GPG Keys:

github ssh and gpg keys

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

github add ssh key

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.

1 Star2 Stars3 Stars4 Stars5 Stars
Loading...
Scroll Up