Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 40

Remote Repository Operations

Recommended resources

Hello! Welcome to the final lesson in our Git fundamentals module.

In our last session, we explored git reset, a powerful tool for rewriting local commit history. We ended with the golden rule: never reset commits that have been pushed to a shared repository. This naturally leads to our topic today: how do we work with those shared, or "remote," repositories?

This lesson is all about bridging the gap between your local machine and the collaborative world. As a DevOps engineer, you'll spend most of your time synchronizing work between your local repository and shared repositories hosted on platforms like GitHub, GitLab, or Bitbucket. Today, you will learn to work with remote repositories using git clone, fetch, pull, and push. These four commands are the backbone of collaborative development in Git.

The Core Concept: Remotes and Tracking Branches

Before we dive into the commands, we need to build a solid mental model. So far, we've only worked in our isolated, local repository. A remote repository is simply a version of your project hosted on a different machine, typically a server accessible over a network.

When you work with a remote, your local Git repository keeps a special kind of reference called a remote-tracking branch. Think of it as a read-only bookmark that remembers the state of a branch on the remote server the last time you communicated with it. By default, the main remote is named origin, and its main branch is tracked by a local reference like origin/main.

Understanding how your local branch (main) and this remote-tracking branch (origin/main) interact is the key to mastering remote operations.

Git CLONE, PUSH & FETCH Tutorial

To visualize this crucial concept, let's watch the beginning of the "Git CLONE, PUSH & FETCH Tutorial" from The Modern Coder. It does an excellent job explaining the relationship between local, remote, and remote-tracking branches.

Please watch from the beginning until 02:13. Focus on how cloning a repository creates a local remote-tracking branch (the 'bookmark' called origin/main) that keeps track of the remote server's state.

With that model in mind, let's explore the commands that manage this synchronization.


1. git clone: Getting a Local Copy

The most common way to start working on a project is to clone an existing remote repository. This command creates a new directory on your machine, initializes a Git repository inside it, and copies all the data for that project.

git clone <URL>

Crucially, git clone does more than just copy files. It also:

  1. Downloads the entire project history.
  2. Checks out a working copy of the default branch (e.g., main).
  3. Sets up a "remote" named origin that points back to the URL you cloned from. This establishes the link for future fetching, pulling, and pushing.

How to clone, push, and pull with git (beginners GitHub tutorial)

Let's watch a practical demonstration of cloning a repository from GitHub.

Watch from 09:38 to 11:46. Observe how git clone with a URL downloads the repository into a new folder on the local machine.

After cloning, you can use git remote -v to see the remote connection that was automatically configured. The v stands for "verbose" and shows the URLs used for fetching (reading) and pushing (writing).

$ git remote -v
origin  https://github.com/someuser/someproject.git (fetch)
origin  https://github.com/someuser/someproject.git (push)

2. git fetch: Downloading Changes Safely

Imagine you've cloned a repository, and while you were away, a teammate pushed new commits to the remote. How do you update your local repository with their work?

The first command to know is git fetch.

git fetch <remote-name> (e.g., git fetch origin)

This command connects to the remote repository and downloads any data that you don't have yet—new commits, new branches, etc. It then updates your remote-tracking branches (e.g., origin/main).

Importantly, git fetch does not change your local branches or your working directory. It is a safe, read-only operation. After fetching, git status will tell you that your local branch is "behind" its remote counterpart.

Git CLONE, PUSH & FETCH Tutorial

The "Git CLONE, PUSH & FETCH Tutorial" has a great visualization and demonstration of what git fetch does.

First, watch the conceptual explanation from 02:37 to 03:16. Then, see it in action from 06:11 to 07:00. Notice how fetch downloads the changes and git status reports that the local branch is now behind origin/main.

After fetching, the new commits exist in your local .git directory, but they are not yet integrated into your local branches. To integrate them, you would use git merge origin/main. This gives you a chance to inspect the changes (git log origin/main) before applying them to your own work.


3. git pull: Fetching and Merging in One Step

The process of fetch then merge is so common that Git provides a convenient shortcut: git pull.

git pull <remote-name> <branch-name> (e.g., git pull origin main)

git pull is essentially git fetch followed immediately by git merge. It downloads the new changes and attempts to merge them into your current working branch all in one go.

Git Basics - Working with Remotes

The official Git documentation provides a concise, authoritative explanation of the difference between fetching and pulling. Reading this will solidify your understanding.

Please read the section titled "Fetching and Pulling from Your Remotes". Focus on the distinction it makes: fetch only downloads data, while pull fetches and merges.

Fetch vs. Pull: Which should you use?

  • git pull is convenient for simple workflows where you just want to get up-to-date.
  • git fetch followed by a manual git merge or git rebase is safer and gives you more control. It allows you to review the incoming changes before they are applied to your local branch. As a DevOps professional, understanding this separation is vital for troubleshooting complex merge scenarios.
Test your understanding!

You are about to start working on a new feature. You want to make sure you have the absolute latest code from the main branch on origin, but you want to review your team's recent commits before applying them to your local main branch.

Which command should you use first, and what would be your next step?

Show answer

You should first run git fetch origin. This will download all the latest changes from the remote origin repository and update your origin/main tracking branch without touching your local main.

Your next step would be to review the changes using a command like git log main..origin/main. Once you are satisfied, you can then merge the changes into your local branch with git merge origin/main.


4. git push: Sharing Your Changes

Once you've made and committed changes locally, you need a way to share them with your team. This is done with git push.

git push <remote-name> <branch-name> (e.g., git push origin main)

This command uploads your local branch's commits to the remote repository.

How to clone, push, and pull with git (beginners GitHub tutorial)

Let's see this in practice. This video demonstrates making a local commit and then pushing it to the remote.

Watch from 07:37 to 09:12. The video shows a typical workflow: modify a file, add, commit, and then git push to send the changes to GitHub.

The "Rejected Push" Scenario
git push will only succeed if your local branch includes the latest changes from the remote branch. If your teammate pushed new commits while you were working, and you try to push, Git will reject it with an error.

This is a critical safety feature. Git is preventing you from overwriting history. To resolve this, you must first pull (or fetch and merge) the new remote changes, integrate them with your local work, and then try to push again.


A Note on Authentication

When you try to push to a remote repository for the first time, you might see a permission denied error. This is because most remote repositories require you to authenticate—to prove you have the right to make changes. While you can sometimes use a username and password, the most common and secure method in DevOps is using SSH keys.

We will not solve the authentication problem today. Instead, we'll dedicate our entire next lesson to it. For now, it's important to know that pushing requires write access, and securing that access is a fundamental DevOps task.


Conclusion

You've now learned the four essential commands for interacting with remote repositories, completing your foundational knowledge of Git's core mechanics.

Key Takeaways:

  • A remote repository is a version of your project hosted on another server. origin is the default name for the remote you cloned from.
  • git clone creates a local copy of a remote repository and sets up the tracking connection.
  • git fetch downloads new data from the remote but doesn't merge it, making it a safe way to check for updates.
  • git pull is a shortcut for git fetch followed by git merge, automatically applying remote changes to your local branch.
  • git push uploads your local commits to the remote repository, sharing your work with others.
  • You must have the latest remote changes integrated locally before you are allowed to push.

This lesson concludes our module on Git. You now have the skills to track changes, manage history, and collaborate with others using version control.

In our next module, we will dive into GitHub. We'll start by creating an account and setting up SSH keys for secure authentication, directly addressing the "permission denied" issue and unlocking the full collaborative power of Git.