CSC Git Guide

Cloning a Repository

Cloning a Repository

If a remote Git repository exists (for example, on NCSU GitHub), you can get a copy of the existing repository. Cloning a remote repository gives Git a full copy of nearly all data that the remote repository has. Every version of every file for the history of the remote repository is pulled to your local .git repository directory by default when you execute the git clone command.

For this course, you will find the clone URL of a remote repository in NCSU GitHub.



Figure: Location of Clone URL in NCSU GitHub


You will have to authenticate each time you try to interact (clone, push, or pull) with your remote NCSU GitHub repository.

Best Practice: Using SSH Keys for Authentication

If you want, you can setup SSH Keys so that you do not have to authenticate each time you type the git clone, git push, or git pull commands. If you decide to use SSH, you will need to use the SSH clone URL.

You can clone a repository using either Git Bash or EGit.

Cloning in Git Bash

To clone a remote repository, open Git Bash, and browse to your GitRepositories base directory. Execute the following command:

 git clone [url]

where [url] is the clone URL for your NCSU GitHub repository.

The name of the created repository directory will be the name of your repository (for example: csc216-001-GP1-01 if your NCSU GitHub repository is https://github.ncsu.edu/engr-csc216-fall2016/csc216-001-GP1-01).



Figure: Using the git clone Command


Conceptual Knowledge: git clone in Practice

The git clone command creates a new project workspace directory, initializes a .git directory inside it, pulls down all data from the remote repository, and checks out a working copy of the latest version of the files in the remote repository.

Your directory hierarchy should now look like:

 GitRepositories  [a directory that contains one or more of your Git repositories]
    -> <repository-name> [a single Git repository]
       -> .git [may be a hidden folder, depending on your operating system]

Now, open Eclipse to your chosen workspace location for your course-related assignments. Eclipse will show that your workspace is empty. You will need to tell Eclipse where your local git repository is:

  1. On the Eclipse menu bar, open: Window -> Show View -> Other
  2. In the Show View window, expand the Git folder.
  3. Click Git Repositories, then click OK.

The Git Repositories window should now appear.



Figure: Git Repositories View


  1. Select Add an existing local Git repository
  2. Browse to the location of the repository you cloned. (in the example above: GitRepositories/csc216-[section]-[project]-[repo-number])



Figure: Add Git Repositories Window


  1. Click OK, then click Finish to close the window.
  2. The local csc216-[section]-[project]-[repo-number] repository should now be displayed in your Git Repositories window in Eclipse.



Figure: Git Repositories Window


Cloning in EGit

To clone a remote repository:

  1. Open Eclipse.
  2. Select Window > Show View > Other.
  3. Browse to select Git > Git Repositories.
  4. In the Git Repositories view, click Clone a Git Repository. That opens the Clone Git Repository wizard. You will be cloning remote repositories on NCSU GitHub to your local computer. You would also use this option if you were using a new or different computer that has not yet been connected to your remote repository.



Figure: Cloning in Eclipse


  1. On the clone wizard’s Source Git Repository page, enter remote repository information as follows (if you copy your repository URI from NCSU GitHub and paste into the URL textbox, the other fields should be automatically filled-in for you): * URI: The URI of your NCSU GitHub repository. * Host: github.ncsu.edu * Repository Path: The suffix of the URI that follows the host (after the github.ncsu.edu) * Connection Protocol (select only one): Select HTTPS if you do not have an SSH key. Select SSH if you do have an SSH key. But first, follow the instructions in Preparing to clone under SSH. * Port: Leave this field blank. * Authentication under HTTPS: User: Your unity id Password: Your unity password * Authentication under SSH: User: git Password: Leave this blank



Figure: Clone a Git Repository using HTTPS




Figure: Clone a Git Repository using SSH


  1. Click Next to continue to the next page.
  2. You may see a caution page saying Source Git repository is empty if you have not committed any files to the repository. Click Next to continue to the Local Destination page.



Figure: Warning for Empty Remote Repository


  1. On the Local Destination Page, click Browse to select a directory on your computer for your local repository.

Reminder: Default EGit Repository Location

Unless you change the default directory location, EGit will save all repositories in the C:/Users/[user]/git/ directory.



Figure: Select Local Repository Location


  1. Click Finish. Your Git Repository view shows the repository that you just cloned.



Figure: Git Repository View Showing One Repository