Remote Repositories

So far, we've been working with Git locally on our machines. Now, it's time to take our Git skills to the next level by learning about remote repositories so you get your code off your local machine.

Remote Repositories

A remote repository is a version of your project hosted on the internet or a network. It allows you to push your local changes to a central location and pull changes made by others. This is the foundation of collaboration in Git.

Popular Platforms for Remote Repositories

Several platforms host Git repositories. Here are the most popular:

  1. GitHub
  2. GitLab
  3. Bitbucket

This chapter will focus on GitHub, which is the most widely used (and the one I use most personally).

Setting Up a GitHub Account

Your personal GitHub account is free! So, nothing is stopping you from getting started. To get an account:

  1. Go to https://github.com/signup
  2. Follow the prompts to create your account

Authenticating with GitHub using GitHub CLI

To interact with GitHub from your local machine, you need to authenticate. There are a few ways to achieve this, but the easiest way is using the GitHub CLI (Command Line Interface). Here's how:

  1. Install GitHub CLI:

  2. Restart your terminal to ensure you have access to to GitHub CLI.

  3. Once installed, open your terminal and run:

    gh auth login
    
  4. The CLI will guide you through the login process:

    • Choose "GitHub.com" if prompted
    • Choose "HTTPS" as your preferred protocol
    • When asked if you'd like to authenticate Git with your GitHub credentials, select "Yes"
    • Choose to log in with a web browser
  5. The CLI will open your default web browser. If it doesn't, it will display a device code. Copy this code and click on the URL provided to enter it.

  6. Authorize GitHub CLI in your browser when prompted.

  7. Return to your terminal. If successful, you'll see a message confirming that you're logged in.

[Suggested Visual Aid: Screenshot or GIF demonstrating the gh auth login process in the terminal]

That's it! You're now authenticated with GitHub. This method sets up authentication for both GitHub CLI and Git commands, so you won't need to enter your username and password for Git operations.

To verify that it worked, you can try:

gh auth status

This should show that you're logged in to GitHub.

Creating a Remote Repository

  1. After logging in to GitHub, click the "+" icon in the top right corner and select "New repository".
  2. Name your repository (e.g., "my-first-remote-repo")
  3. Choose to make it public or private
  4. Click "Create repository"

Red frame around the + icon and open menu for creating a repository on GitHub

Connecting Your Local Repository to the Remote

Now that we have a remote repository, let's connect it to our local repository:

  1. In your local repository, add the remote:

    git remote add origin https://github.com/yourusername/my-first-remote-repo.git
    

    Replace "yourusername" with your actual GitHub username.

  2. Verify the remote was added:

    git remote -v
    

    This should display the URL of your remote repository.

Pushing Changes to the Remote Repository

When you're ready to share your local commits with the remote repository:

  1. Push your changes:
    git push -u origin main
    
    The -u flag sets up tracking, simplifying future push and pull commands.

Cloning a Remote Repository

To create a local copy of a remote repository:

  1. On GitHub, navigate to the repository you want to clone
  2. Click the green "Code" button and copy the URL
  3. In your terminal, navigate to where you want the repository to be cloned
  4. Run:
    git clone https://github.com/yourusername/repository-name.git
    

You can use this to start working with any public code (or code you can access on GitHub). For example, you can clone the entire Codú codebase (this website) from this repository by running:

git clone https://github.com/codu-code/codu.git

Pulling Changes from the Remote Repository

When you're working on a project with others or on multiple machines, you'll often need to update your local repository with changes that have been pushed to the remote repository. This process is called "pulling" changes.

Before we dive into the commands, let's clarify two important concepts:

  1. Fetch: This operation brings changes from the remote repository to your local Git, but it doesn't automatically integrate these changes into your working files.

  2. Pull: This is a fetch followed by a merge. It brings changes from the remote repository and automatically merges them into your current branch.

The workflow

Let's go through the process of updating your local repository:

  1. First, make sure you're in the correct branch. Usually, this will be your main branch:

    git checkout main
    
  2. Fetch the latest changes from the remote repository:

    git fetch origin
    

    This command downloads all the changes that have been made in the remote repository since your last fetch or pull. However, it doesn't modify your working directory.

  3. You can see what changes have been made by comparing your local main branch with the remote main branch:

    git log main..origin/main
    

    This shows you the commits that exist in the remote main branch but not in your local main branch.

  4. If you're happy with the changes, you can merge them into your local branch:

    git merge origin/main
    

    This integrates the changes from the remote main branch into your current local branch.

  5. Alternatively, you can combine steps 2 and 4 with a single pull command:

    git pull origin main
    

    This fetches the changes from the remote main branch and immediately merges them into your local branch.

A few tips

  • Always pull changes before you start working on a project. I run this command several times a day when working with a team. This ensures you're working with the most up-to-date version of the code.
  • If you have uncommitted changes, commit them before pulling. This prevents conflicts between your local changes and the incoming changes.
  • If you're working on a feature branch, you might want to pull changes from the main branch regularly into your feature branch. This is called "keeping your branch up to date" and can help prevent significant merge conflicts later.

In this chapter, we've covered the basics of working with remote repositories and collaborating with others. We learned how to:

  • Set up a GitHub account
  • Create a remote repository
  • Connect local and remote repositories
  • Push and pull changes
  • Clone repositories

These skills form the foundation of using Git in a team environment. In the next chapter, we'll explore more collaboration techniques and see how you can branch and merge code on a team like a pro.

GitBeginner
Avatar for Niall Maher

Written by Niall Maher

Founder of Codú - The web developer community! I've worked in nearly every corner of technology businesses: Lead Developer, Software Architect, Product Manager, CTO, and now happily a Founder.

Loading

Fetching comments

Hey! 👋

Got something to say?

or to leave a comment.