Cloning a Remote Repository

Scenario

You're working a new project with a team, and you have been added to the project team on github. Now you have to clone the project to your local machine. Here's how to go about this.

Step 1: Choosing the Cloning Method

To clone a remote repository, you have two primary options: HTTPS and SSH. The choice depends on factors such as the repository's accessibility and your preferred authentication method.

Cloning with HTTPS

  • HTTPS cloning is convenient when you want to quickly clone a repository without setting up SSH keys.

  • To clone with HTTPS, open your terminal and navigate to the directory where you want to clone the repository.

  • Run the following command, replacing repository-url with the URL of the remote repository:

git clone repository-url

Cloning with SSH

  • SSH cloning is ideal if you have set up SSH keys and want to authenticate using them.

  • To clone with SSH, open your terminal and navigate to the desired directory.

  • Run the following command, replacing repository-url with the URL of the remote repository:

git clone git@github.com:user/repository-url

Step 2: Setting Up the Remote Origin

After successfully cloning the repository, you can set up the remote origin to simplify future interactions with the remote repository.

Change to the Repository's Directory

  • In your terminal, navigate to the cloned repository's directory:

cd repository-directory

Add Remote Origin

  • To associate the cloned repository with the remote origin, run the following command:

git remote add origin repository-url

Verify Remote Origin

  • To verify that the remote origin is correctly set up, execute:

git remote -v
  • You should see the repository's URL displayed as the remote origin.

Step 3: Fetching the Latest Changes

To keep your local repository up to date with the latest changes from the remote repository, you need to fetch the changes periodically.

Fetching Changes

  • To fetch the latest changes, use the following command:

git fetch

Checking for New Branches

  • To check for any new branches added to the remote repository, run:

git branch -r
  • This command lists all the remote branches, and any new branches will be displayed here.

Pulling Changes

  • To incorporate the fetched changes into your local branch, execute:

git pull
  • This command merges the fetched changes into your current branch, ensuring your local repository is up to date.

Last updated