How to Clone a Git Repository?

0
186
Clone a Git Repository

While creating a repository on GitHub, you will notice that it is only a remote repository. But you can clone and store a copy of a repository locally on your computer. Also, you can sync the repository between remote and local servers. In this post, we are going to focus on how to clone a git repository. However, before we go on with the steps, it’s important for you to know about Git repositories in detail. So, let’s get going.

Creating a New Git Repository

If you use a repository other than the system repository of your PC, you can start controlling it using Git. To do that, you first need to find that project’s directory. The method to create a new Git repository is different depending on the operating system. First, you need to navigate to the project directly on your local computer using the Terminal app (in Linux and macOS) and Command Prompt (in Windows):

For Linux:

Cheap SSL
$ cd /home/user/my_project

For macOS:

$ cd /Users/user/my_project

For Windows:

$ cd C:/Users/user/my_project

Then you need to initialize Git by executing the following command:

$ git init

The new subdirectory will be created, and you can call it .git, where you will find all your necessary repository files. You can track these files to control versions of the existing files, and all it will take are a few significant ‘Git add commands’ to have full control of your repository. Here are a few examples of these Git commands:

git commit:
$ Git add *.c
$ Git add LICENSE
$ Git commit -m ‘Initial project version’

We will talk about these commands later in this post, but for now, let’s find out how to create a new repository on GitHub.

Create a New Repository on GitHub

To create a new repository on GitHub, follow these simple steps given below:

  1. Go to GitHub and sign in to your user account. If you don’t already have one, register and create a new GitHub account.
  2. When the main page opens, click on the + sign icon from the top-right corner and click on the New repository option.
  3. It will take you to a page where you can provide the git repository name, such as test-repo. Provide the description of the repository the same way.

Note: You can create a README file here to save this information and use it later while cloning.

  1. Next, you need to select your preferred language to add a .gitignore file on the same language
  2. Now, click on the Create repository button, and your repository will be created
  3. Finally, you can view your newly created repository in a new tab.

Cloning a Repository from GitHub

Cloning repositories from GitHub to your computer can help you carry out a lot of stuff like merging conflicts, removing files, pushing larger commits, etc. Cloning a repository means copying a repository from GitHub to your local system. The cloned repository will contain all the copies of the repository data along with the version history of files.

You can push the changes of your local files to the GitHub repository or pull the changes to other people’s files. The procedures of pushing and pulling changes happen in the remote repository of GitHub. If you want to know more about this, go to “Using Git.”

While cloning a Git repository, you will have two options. You can copy your already existing repository or copy another person’s existing repository to build your project.

Clone a Repository from GitHub to Your Local Machine

If you have created a repository on Github, you can clone that on your computer. First, you need to access and open the repository from your GitHub account and then click on the Clone or download button. After that, you need to choose the “Clone with HTTPs” option and copy the URL of the repository provided there. Once done, you have to follow the steps below:

Step 1: Open your bash shell and navigate to the location where you want to clone the repository.

For example, if you are using a Linux machine and you want to clone your repository in the Documents folder, you can use the following command:

cd Documents

Step 2: After navigating to the directory where you want to place your repository, you can use this command- git clone https://github.com/URL-TO-REPO-HERE

Note: Use the URL to your repository in place of “URL-TO-REPO-HERE” to get the result.

Step 3: The git clone command will copy your repository from GitHub to your local computer. The git specific command looks like this:

git clone https://github.com/YOUR-USERNAME/YOUR-REPOSITORY

Note: Put your username in place of “YOUR-USERNAME” and put your repository URL in place of “YOUR-REPOSITORY” to complete the step.

When you run the git clone repo-path-here command, the output that will appear will be something like this:

Cloning into ‘test-repo’...
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 5 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (5/5), done.
Checking connectivity... done.
The repository name and output numbers in your computer are a combination of the total file size, volume, and more. The output that you will see after executing the cloning command will be different from what we have shared with you. To check if the repository is cloned successfully, type ls in the terminal window, and it will list all the files and folders that are available in your current directory. The directory name will be the same name as the repository you have created on GitHub.

How to Clone a Repository with Sourcetree?

Sourcetree is a Git GUI that offers you a visual representation of your Git repository. This Git client comes free for Windows and Mac. You can use Sourcetree or any other client to clone a repository. However, if you don’t know how to use Sourcetree to clone a repository, don’t fret, just follow the steps mentioned below. Also, make sure to download the Sourcetree application first.

Step 1: First launch Sourcetree and it will show your repository, click on the Clone button

Step 2: Now a Clone this repository dialogue box will appear from where you can select the Clone in Sourcetree button.

Step 3: You can also update the Destination Path or Bookmark Name if you need. The destination path is the folder where the repository clone stays on your computer. And the bookmark name is the name of the folder.

Step 4: Finally, select the Clone button

How to Clone an Empty Repository?

As you know, while you are creating a repository on GitHub, you can save its information on a README file. If you don’t choose the README option while creating the repository, it will appear empty at the time of cloning. This is because it will have no files to clone, but you can follow the instructions below to clone the empty repository to your computer.

Step 1: Visit the main page of the repository on GitHub.

Step 2: Under “Quick setup,” Clone the repository with an SSH key.

Step 3: Or, to clone your repository on desktop, click on the Set up in Desktop option and follow the on-screen prompts.

Step 4: Open Git Bash.

Step 5: Change the current directory to where you want to clone the repository.

Step 6: Type git clone or paste the command below:

$ git clone https://github.com/YOUR-USERNAME/YOUR-REPOSITORY

Step 7: Press Enter and the local repository clone will be created. The output will be as follows:

$ git clone https://github.com/YOUR-USERNAME/YOUR-REPOSITORY
> Cloning into `Spoon-Knife`...
> remote: Counting objects: 10, done.
> remote: Compressing objects: 100% (8/8), done.
> remove: Total 10 (delta 1), reused 10 (delta 1)
> Unpacking objects: 100% (10/10), done.

Conclusion

Now you know the complete process of creating a new repository from GitHub and cloning it using different methods. To add further, every time you try to copy or clone something from GitHub, just remember the username and password. Once you have these, you can track your changes in the repository from the commit history anytime you want. If you have found this guide helpful, don’t forget to come back for more informational guides like this.

LEAVE A REPLY

Please enter your comment!
Please enter your name here