Git Remote Repositories - Push, Pull, Fetch, & Origin

Learn how to connect your local repo to GitHub/GitLab! Master git push, pull, fetch, and remotes!

Introduction

Let's connect your local repo to a remote one like GitHub!

What You Will Learn

  • git remote
  • git push
  • git pull
  • git fetch
  • Multiple remotes

Prerequisites

What is a Remote Repository?

A remote repo is a version of your project hosted on the internet or network (GitHub, GitLab, etc.)!

Adding a Remote!

# Add a remote called "origin"
git remote add origin https://github.com/YOUR-USERNAME/YOUR-REPO.git

# List remotes
git remote -v

"origin" is the default name for your main remote repo!

Push to Remote!

Send your local changes to GitHub!

# First time push (sets upstream)
git push -u origin main

# Subsequent pushes
git push

Fetch and Pull!

Get changes from GitHub!

git fetch

Downloads changes but doesn't merge them!

git fetch origin

git pull

Downloads and merges changes!

git pull origin main

Multiple Remotes!

You can have multiple remotes! Like for open source:

# Your fork (origin)
git remote add origin https://github.com/YOU/repo.git

# Upstream repo
git remote add upstream https://github.com/ORIGINAL-OWNER/repo.git

# Pull from upstream
git pull upstream main

Best Practices!

  1. Pull before pushing: Avoid conflicts!
  2. Push often: Back up your work!
  3. Use meaningful remote names: origin, upstream, etc.

Summary

You can now push/pull from GitHub!

Related Articles

Previous Tutorial

GitHub Account Setup

Next Tutorial

Let's learn collaboration! → Collaboration Workflows