Cloning a repository

Having a project folder on your computer is great if you work by yourself or with someone who shares the same computer and account with you.

What happens if your project folder is on someone else's computer, maybe you want the Git project offered by a company who don’t want you accessing the folder directly? Should we just copy the files and let them set up their own Git repository on their computer? Sure we could do that, but then if they add a branch or commit new files it wont magically update to our copy.

What we need is a way to copy not just the files, but the whole repository, and keep the version control and all that jazz so that if an update happens, we can refresh our downloaded version too.


Cloning our test repository

We are going to clone our existing repository into another folder, but you could use this method to clone a repository from another user’s account on the same computer.

  • Fire up the command line but don’t navigate to the Git repository folder, stay in the home folder (assuming you have your repository in the home folder, if not navigate to the folder your Git repository is in

  • Type git clone git-tutorial git-second

Cloning a Git repository

Take a look in the Home folder, you now have a copy of the repository and it is called git-second. The clone not only contains all the files from the folder, but the Git history, branches and logs.


Updating a cloned repository

The clone and the original are the same, but what do we do if the original changes and we want to update the clone? We can do this easily with the pull command.

If you get into the command line and navigate into the git-second folder where our clone is located, we can tell the repository to update itself with the following commands:

  • Open the command line and navigate into the git-second folder

  • Type git pull /home/username/git-tutorial master (use the correct path, not the example one given here)

Pulling Git files

You can see in the above example that the pull command has three elements:

  1. The first is git pull, which tells git we want to pull some files from somewhere

  2. Then the location of the files, notice this is not a relative location but starting from the root

  3. Lastly we say which branch we are pulling files from


Looking without merging with the Fetch command

The pull command downloads a list of changes that have been made, then merges those changes with your local repository. You might want to see what changes have been made, and then decide whether or not to merge those changes, we can check out the changes without also merging using the fetch command.

Let’s use the fetch command now, open the command line and get into your cloned repository (git-second) remember to use the correct file path, not the example one below:

  • From the cloned repository, type git fetch /home/username/git-tutorial master

  • Now type git log -p HEAD..FETCH_HEAD

The first command we entered fetched a file called HEAD from the git-tutorial folder and saved it in the git-second clone, renaming the file FETCH_HEAD.

The second command compared the new file FETCH_HEAD with the file HEAD which is already stored in the git-second repository.

If there are changes between the original repository and the clone, you will now see them (and thanks to the log -p command we get a description of those changes. We can now consider whether we want to pull the branch or not.

I know you are wondering what the hell these HEAD and FETCH_HEAD files have to do with anything, so now would be a good time to explain what HEAD is all about.

Each repository has branches, and each branch has commits along the way (scroll back to the diagram up the top if you don’t remember this). The HEAD file is the one which remembers which branch you are currently using and which commit was the last commit. It is part of the logging system.

So with our fetch command, we simple download the HEAD file from the original repository (which is renamed FETCH_HEAD so we don’t have two files with the same name) and compare it with the HEAD file in the current repository, the fetch command shows you the difference between the two, if they are different, you know you have commits and changes to pull (download) if you want to.


What did I just learn?

  • You can copy an entire repository using git clone git-tutorial git-second

  • A cloned repository can be updated if changes are made in the original using git pull /home/username/git-tutorial master

  • The fetch command compares changes in the original with the clone to see what’s worth downloading

  • The HEAD file contains information on what is the current branch and commit



Git head
Git pull
Git clone

Add new comment

Answer the below question to help us fight spam

What is the opposite of light?