Clone a repository: Git clone

Learn how to clone a Git repository that is hosted on GitHub. The command is git clone, very easy to use. However, it is also important to know how to restore project dependencies once the repo has been cloned.

In this article we are going to talk about the operation of cloning a repository, the process you have to do when you want to bring the code of a project that is published on GitHub and you want to restore it on your computer, to be able to use it locally, modify it , etc.

This step is quite basic and very easy to do, but it is essential because you will need to do it many times in your work as a developer. We will also try to complement it with some useful information, so that you can learn useful and a little more advanced things.

Note that cloning repositories may require you to authenticate to GitHub, especially if it’s a private repository. Authentication must now be done using what is known as a Personal Access Token. Learn to .

Download vs Clone

At the beginning of using a site like GitHub, if we have no idea of ​​​​using Git, we can also get the code from a repository by downloading a simple Zip. You can get this option through the link in the following image.

However, downloading a repository like this, although very simple, does not allow you some of the interesting utilities of cloning it, such as:

  • It doesn’t create a local Git repository with the changes that the remote repository has had over time. That is, you download the code, but nothing else.
  • You will not be able to push changes to the remote repository, once you have made them locally.
See also  Get the time on a PHP server

In short, you will generally not be able to use the advantages of Git in the downloaded code. So it is better to clone, since learning how to perform this step is also very easy.

Clone the Git repository

So let’s see how you should clone the repository, so that you can actually benefit from Git with the downloaded code. The process is the following.

First you will copy the URL of the remote repository you want to clone (see the “Copy to clipboard” icon in the following image).

Then you will open a terminal window, to place yourself on the folder of your project that you want to clone. I would recommend that you directly create a folder with the name of the project that you are cloning, or any other name that seems best to you for this repository. You are located inside that folder (cd, cd…) and from it we launch the command to make the clone, which would be something like this:

git clone https://github.com/EscuelaIt/angular-avanzado.git . The last point tells you that you are going to place the clone in the folder where you are located, in your terminal window. The output of that command would be more or less as you have in the following image:

Install the dependencies

Git developments usually ignore dependencies using , so it is important that we install them again in the clone repository, locally.

For each type of project, with each language, there will be commands to install the dependencies. You would have to know this command yourself, if it is from , if it is from NodeJS (or Javascript) .

For example, this is how we reinstall the dependencies in an Angular project, which uses npm as a dependency manager.

npm install

But I insist that this depends on the dependency manager, or dependency managers of the project, if they are being used.

Push changes to the remote repository made by us

Once the code has been modified locally, you may want to upload the changes to the remote repository. It is something very simple, since when cloning the repository locally the remote origin from where you brought it is associated. However, we have to take into consideration some points:

  • Depending on the company or the work team, there may be some rules for committing changes, such as always working with and committing changes to a specific branch, which can then be merged with a .
  • If the repository you cloned isn’t yours and you don’t have permissions because you don’t belong to an organization that owns it, obviously you won’t be able to push changes.

We are going to keep it simple here, thinking that: 1) the repository is yours and 2) that nobody bothers to push changes directly to the master branch. So, to push changes to GitHub, or any other repository hosting, we would do:

git push

With that, the changes are sent instantly and you will be able to see them reflected in the remote repo.

Note: Obviously, in order for the changes to be sent to the repository, local you will have to have made some modification/s in the project’s code, and of course you will have to have confirmed them with the corresponding commit/s. But we assume that you already know this, as a reader of . You can find more information about this basic operation in the article about you .

How to push changes to GitHub if the repository is not yours (via fork)

If the repository you are changing is not yours and you intend to continue development, adding changes that you want to push to GitHub, you would have to clone the repository in another way.

First you have to create a “fork”. That is, a repository that is a copy of another that is already published on GitHub. The good thing about a fork is that the repository will be exactly the same, but it will be in your GitHub account, with your user, so you will have permissions to do whatever you want with it.

There is a fork button at the top of the repository page.

Once the fork is done, the repository already belongs to you. Then you can clone (the fork, not the original repository) and make the changes you want, which you can perfectly upload to your own repository (your fork).

At the top you can see that the repository is a fork of another existing repository on GitHub.

The rest of the process, to download the repository, modify it, and push the changes is exactly the same as described above, including the “git push” to push the changes. Only now, since it’s your own repository, it will let you update its code remotely.

Loading Facebook Comments ...
Loading Disqus Comments ...