How to clone GitHub repository and branches

Clone a repository from GitHub means to copy the entire project to a directory on your system. This directory is created automatically by Git, and it has the same name as the GitHub repository. This article is a step-by-step guide on how to clone GitHub repository and branches to your computer. But before that we will explain what is GitHub and how to use it?.

To follow along this article make sure you have Git installed on your computer and a GitHub account. To see how to install Git and how to use it check out this guide: Complete Guide on how to use Git and GitHub.

What is gitHub?

GitHub is a collaborative cloud-based platform owned by Microsoft and offers an interesting free plan. It is built for developers to offer them a better way to work together. GitHub is known as the largest online storage space where you can host, review, share and publish code, manage projects, and build software alongside millions of developers. This interaction between developers makes GitHub a networking site that aims to encourage open source development and helps young projects grow.

Actually, GitHub is a web-based graphical interface that provides a Git repositories hosting service which allows you to keep track of and share your Git version control projects outside of your local computer. 

Besides the hosting feature, GitHub offers many other features that make it powerful, such as:

  • Fork: fork a repository is the process of copying a repository from one user’s account to another. This allows you to copy a project and modify it under your own account name.
  • Pull request: After making your changes to a project and you want to share them, you can send a notification called a “pull request” to the original owner.
  • Merge: The project owner can add your recent changes to his project by merging your repository with the original repository and give you credit for your contribution.

 

Create a GitHub repository

To create a new GitHub repository, we need first to create a GitHub account.

Create a GitHub account

To create a new account go to GitHub website and click on Sign up.

GitHub website

Next, enter your username, email and password then click on Create account.

create new GitHub account

Once your account is created, log in and click on the plus sign in the upper-right corner of your homepage to create a new repository.

Enter a repository name, choose whether you would like your repository to be public or private, and check Add a README file. Once you have completed this, you click on Create repository.

create GitHub repository

How to Clone GitHub repository/branch?

Clone a repository from GitHub means Download the entire project to a local directory on your computer and create a remote to the GitHub repository automatically. So Cloning a GitHub repository allows you to push directly your changes by running git push command.

There are two ways to clone a GitHub repository:

  • Clone a new repository that has created or
  • clone a repository you have already forked and duplicated.

1. How Clone a new GitHub repository?

Once your GitHub repository has been created, navigate to the new repository and click on Code, then grab the HTTPS link: The link will look similar to this: https://github.com/YourUsername/your-repository-name.git.

clone a GitHub repository

Then switch back over to your terminal. Navigated to your working directory where you want to put your repository and run the command:

  $ git  clone  https://github.com/devdotcode-account/repository-example.git

Now you can start making your changes, then commit and push them to the remote repository using the following command:

 $ git push

2. How Clone a GitHub branch?

Clone a branch of a GitHub repository makes you save time from downloading the total repository. To do that, navigate to the repository on your GitHub account and click on Code, then grab the HTTPS link: https://github.com/YourUsername/your-repository-name.git.

clone a GitHub branch

Then switch back over to your terminal. Navigated to your working directory where you want to have your repository branch and run the command:

  $ git clone  -branch  branch_name  https://github.com/devdotcode-account/repository-example.git

Now you can start making your changes, then commit and push them to the remote repository using the following command:

 $ git push -u origin branch_name

3. How to Clone a fork GitHub repository?

Fork a repository is one of the most important GitHub features. It allows you to copy a repository from another user’s account, then modify it and make changes freely (including creating branches and opening pull requests If you are hoping to contribute back to the original repository) under your own account. In this section, we will show you how to clone a GitHub repository but before that let’s see how to fork a GitHub repository and copy it to your account.

You can fork a repository in three simple steps:

  1. First login to your GitHub account.
  2. Then, navigate to the repository you want to fork to your account.
  3. Finally, click on the Fork button to create a copy of the repository in your account.
Fork a GitHub repository

Once you have a fork repository in your GitHub account, you can clone your fork repository locally on your computer. To do that:

  • Login to your GitHub account, navigate to your fork repository of the original repository(which is the repository you want to fork).
  • Then click on Code, grab the HTTPS link: https://github.com/YourUsername/your-fork-repository-name.git.
clone a fork repository

Then switch back over to your terminal. Navigated to your working directory where you want to put your repository and run the command:

  $ git  clone  https://github.com/YourUsername/your-repository-name.git.

Now you can start making your changes, then commit and push them to the remote repository using the following command:

 $ git push

Once you have a local copy of your fork repository, you can configure Git to synchronize your fork repository with the original repository or duplicate your fork to make a private version.

Configure Git to sync your clone repository with the original repository

In order to keep Git pulling changes from the original repository, you need to upstream it into the local clone of your fork.

  1. On GitHub, navigate to the original repository, click on Code, then grab the HTTPS link.
  2. Open Terminal.
  3. Change directory and navigate to the location of the fork repository you cloned in already.
  4. Then type and run git remote add upstream with the URL you copied in Step 1 :
 $ git remote add upstream https://github.com/TheOwnerUsername/original-repository.git

To verify the new upstream repository, type:

 $ git remote -v

How to duplicate and clone a GitHub repository?

To duplicate a GitHub repository, follow these steps:

  1. First Login to your GitHub account.
  2. And create a new repository, let’s name it new-repository.git.
  3. Next in your account navigate to the repository you want to duplicate (named original-repository.git) and click on Code, then grab the HTTPS link.
  4. Now, open your terminal, navigate to your working directory and create a bare clone of original-repository.git.
 $ git clone --bare https://github.com/YourUsername/original-repository.git

5. Navigate to original-repository.git:

 $ cd original-repository.git

6. And push a mirror to the new-repository.git created in step 2 using the following command:

 $ git push --mirror https://github.com/YourUsername/new-repository.git

7. That’s it, you have just duplicated your original-repository.git in your GitHub account. Now go out from original-repository.git :

 $ cd ..

8. And remove the temporary original-repository.git from your working directory:

 $ rm -rf original-repository.git

Now, you can clone your new duplicated repository by following the same steps mentioned already in how to clone a new repository section of this article.

Summary

In this article we explained in depth how to clone a GitHub repository and branches by covering the following points:

  • How to clone a new GitHub repository.
  • How clone a branch.
  • How to clone a fork repository.
  • Configure Git to sync your clone repository with the original repository.
  • How to duplicate and clone a GitHub repository.

Hope this was helpful for you.

Translate »