Learn Computing from the Experts | The Rheinwerk Computing Blog

How to Clone a Git Repository

Written by Rheinwerk Computing | Oct 17, 2022 1:00:00 PM

When it’s time to set up a cloned repository in your GitHub account, you’ll need to follow these steps.

 

The repository you want to clone initially exists only online, and not yet on your computer. To create a local copy of the repository, you must open a terminal window, change to any directory, and run git clone, specifying the URL of your repository as a parameter. Thus, replace the URL (such as https://github.com/MichaelKofler/first-test.git) with the address of your own repository.

 

For your experiments, use a local directory that is not synchronized between multiple computers via the cloud or with other tools! Synchronization tools can upset Git.

 

cd my-work-directory

 

git clone https://github.com/MichaelKofler/first-test.git

   Clone after 'first-test' ...

   Username for 'https://github.com':           <account-name>

   Password for 'https://user-name@github.com': <token-code>

   remote: Enumerating objects: 3, done.

   remote: Counting objects: 100% (3/3), done.

   remote: Compressing objects: 100% (2/2), done.

   remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0

   Unpack objects: 100% (3/3), done.

 

cd first-test

 

ls (or dir in cmd.exe)

   README.md

 

During the initial execution of git clone to access a private repository, you must authenticate yourself. On macOS and Linux, you would enter the account name (or, if you have defined a GitHub organization in your account, the name of the organization) and the previously generated code of your personal access token.

 

On Windows, on the other hand, a window appears with three authentication variants to choose from. We recommend you use the Sign in with your browser option. A web browser window will then appear to log on to your GitHub account. (If you’re currently logged on, this step can be omitted.) Then, using the OAuth process, GitHub provides an authentication code that is stored by the Windows Credential Manager. This approach has an advantage in that later git commands (e.g., git push) won’t require repeated authentication.

 

After successful authentication, git clone creates a new directory with the same name as the repository, downloads all files from the external repository to it, and creates the .git/config file. The local configuration settings are stored in that file.

 

 

Don’t Forget “cd”! git clone creates a new directory. All further git commands are to be executed in this directory. So, don’t forget cd directory; otherwise, git will complain that it doesn’t recognize a Git repository in the current directory.

 

Editor’s note: This post has been adapted from a section of the book Git: Project Management for Developers and DevOps Teams by Bernd Öggl and Michael Kofler.