git clone command: github ssh configuration, --branch, --depth options
This article will cover how to use the git clone
command. I'll give you a quick overview of cloning over ssh, the --branch
option, and the --depth
option.
1. How to use git clone
The git clone
command copies the entire contents of a remote repository to your local environment. We can commit new changes to the cloned repository, or perform other operations on it, and then push it back to the remote repository.
Cloning is typically done on Github or a Git hosting service such as Gitlab or Bitbucket. It's easy to use. Take the address of your remote repository and type the command below.
As explained in the git remote post, Git supports a variety of communication protocols. Let's take the most popular one, the HTTP(S) protocol, as an example.
$ git clone https://github.com/user/repo.git
You will also notice that the remote repository address ends in .git
, as mentioned in the git init post when explaining the --bare
option.
Running the command will create a folder with the same name as the repository in the current directory and download the files belonging to the repository into it. Here is the result of running the git clone
command on the Github repo address with the highest number of stars in the world.
You can see that everything in the repo has been copied to the
free-programming-books
directory, which is the name of the remote repo. If you want to put it in a directory with a different name, you can write the address followed by the desired directory name like this
$ git clone https://github.com/user/repo.git directory-name
2. clone using ssh
SSH is a communication protocol that provides encryption using a public/private key pair. It is primarily used to connect to remote hosts, and when used with Git it benefits from easier authentication and faster response times than https.
See this post for a list of communication protocols supported by Git.
However, unlike the HTTPS protocol, which can be used without any configuration, SSH requires some initial setup. It's not difficult, so let's take a step-by-step look at how to use ssh on Github.
2.1. Generate a new SSH key
First, we need to generate a new SSH key, but before we do that, let's see if one already exists. Check the ~/.ssh
directory like this.
$ ls -al ~/.ssh
Three of the output files are available on Github: id_rsa.pub
, id_ecdsa.pub
, and id_ed26619.pub
. If they don't exist, create a new key file with the following command.
$ ssh-keygen -t ed25519 -C "Github email"
The result of running this will be
After typing the command, you'll get three prompts.
The first asks where to save the file, if you hit enter without typing it will be saved to the default location. The next is a prompt to enter a passphrase for ssh, which is a password to make sure that if someone gets their hands on your ssh key file, they can't do what they want with it. Type it again at the next prompt.
2.2. Registering your ssh key with your Github account
You'll need to register the ssh public key you created with Github. After logging in to Github, click on your profile in the upper right corner to open the menu. Double click on Settings.
On the Settings screen, navigate to SSH and GPG keys under the Access menu.
Click the New SSH Key button to display the registration screen. The contents of the ssh key file can be copied to the clipboard with the following command.
$ pbcopy < ~/.ssh/id_ed25519.pub
If you didn't use the ED25519 algorithm, replace it with the appropriate filename. Then paste the key you copied from the clipboard into the Key section below and click the green button to register the new ssh key. At this point, you will be prompted to log in again to confirm your account.
Once the registration is complete, run the following command to verify that it was successfully registered.
$ ssh -T git@github.com
At this point, based on my SSH keyfile algorithm, it will check to see if the fingerprint matches and then attempt to connect. The fingerprint can be found at GitHub's SSH key fingerprint page (opens in a new tab).
After verifying the fingerprint, I entered yes and the passphrase and was able to connect normally.
`2.3. git clone with ssh
Now the initial setup is done. Enter the git clone
command and the ssh address of the remote repository to proceed with the clone, as shown below.
$ git clone git@github.com:<username>/<remote-repo-name>
Here is the result of the execution
There are also ways to avoid entering the passphrase every time. We'll cover this in another post.
3. Cloning a specific branch using the --branch option
By default, the git clone
command will clone the default branch of the remote repo, usually the master
or main
branch.
If you want to clone another branch, there are two ways to do it: pull the other branch after the main branch has been cloned, or use the --branch
option to the clone command.
You should use the first method if you need to access more than one branch from a remote repo. This is because cloning with the --branch
option will not allow you to access the other branch.
Here's how to use it
$ git clone --branch <branch-name> <repo-address>
Let's try it in action
This way we can clone only the branches we want.
4. Clone only as much history as you want with the --depth option
As your project grows, so does the amount of data stored in your Git repository. The commit history will also be quite long, and in some cases it may be inefficient to get all the data from the beginning of that repository to the present.
The --depth
option is for these cases. With this option we can specify the number of commits to copy to our local environment, based on the last commit.
For example, if we specify only 1 depth, we will only get data for the last commit.
The command looks like this
$ git clone --depth <depth> <repo-address>
Let's see the results of the actual run
As you can see, since we cloned with the depth option set, only the last commit is displayed in the git log
command.
5. Conclusion
The git clone
command is an essential command for accessing not only your own projects, but also many other repositories.
I hope this post has helped you understand and use the git clone
command more.
