3) Collaboration with Git & GitHub Lesson

Git Pull

10 min to complete · By CodingNomads Team

When collaborating with others using Git, you'll often need to update the contents in your local repository to match those of a remote repository. To help you stay in sync, Git provides a powerful command, git pull.

To familiarize yourself with git pull, you'll explore the following:

  • What is Git Pull?
  • Git Fetch vs. Pull
  • Git Pull Command
  • Git Pull Example

What is Git Pull?

The git pull command is used to fetch the contents of a remote repository and "pull" those contents onto your local machine. It updates the local repository to match the remote repository.

Git Pull

Imagine you and a friend are working on the same project remotely. Your friend made some changes and added those changes to the remote repository. Now you need those changes in your local working directory. The process you need to follow now is called pulling. You will pull the new changes that your friend pushed to the project's GitHub repository that you're both working on.

Colorful illustration of a light bulb

Info: You will learn how to push changes to a remote repository in the next lesson: Git Push.

Git Fetch vs. Pull

The git fetch command retrieves changes in the remote repository without integrating them into your local working directory, while the the git pull command downloads the contents from your remote repository and merges it into your working directory.

When you try out both variants, you might think that git pull and git fetch do similar things. So here's a look at how these two commands differ:

What Does Git Fetch Do?

The git fetch command updates the repository data without changing the local files. It tells us to retrieve the latest metadata info from the remote repository.

What Does Git Pull Do?

The git pull command combines two commands: git fetch and git merge. You can use these commands individually, or you can use git pull, which combines the actions of both commands into one. Therefore, the git pull command retrieves the updates from your remote repository and merges them into your working directory. If any conflicts are detected during the merge, you'll be alerted and will need to resolve these conflicts.

Colorful illustration of a light bulb

Info: You'll learn more about conflict resolution a bit later.

As you can see from the descriptions above, git pull is a convenience command that allows you to fetch and merge the data from your remote repository in one go. It is very common to do both of these actions together, but there may be situations when you want to divvy them apart. If you want to do the steps separately, stick with using first git fetch followed by a git merge, otherwise you can usually do what you need to do with git pull:

Git pull and git fetch

Git Pull Command Syntax

The git pull command takes two arguments:

  1. Remote Name: The name of the remote you're pulling from
  2. Branch Name: The name of the branch of your remote that you want to pull from

Here's the code for git pull in an example:

# git pull <Remote Name> <Branch Name>
$ git pull origin main

In the example above, origin is the name of the remote, and main is the name of the branch on that remote that you want to pull from.

Git Pull Example

Practice builds mastery, so go ahead and give this process a real-world walk-through by following the steps outlined below:

  1. Log in to your GitHub account
  2. Create a new repository on GitHub titled push_pull_test. This time, initialize this repository with a README.md file
  3. Clone the repository to a new directory on your local computer using the CLI
  4. Open the README.md file in a text editor, but don't make any changes
  5. Return to the GitHub project in your browser
  6. Edit and commit the README.md file from within your browser
  7. Return to the CLI, and be sure you're in the root folder of the push_pull_test Git repository
  8. Run the command git pull origin main
  9. Inspect your local README.md file in your text editor. You should see the updates you made to the file through GitHub on your web browser

And you are done. You just went through the process of fetching changes done to your shared repository from a different source and merging them into your local Git repository. Good work! In this situation, you did both changes, but the process would be the same if someone else made changes to your shared repository and pushed them to GitHub.

However, it won't always go as smoothly as it did this time. If any changes are made to the same lines of code from different sources, Git cannot automatically merge the files together without making assumptions about the "right" version. Git is just a computer program after all, so it won't make any such decisions for you. Instead, it will consult you if a situation like that arises. You will learn about merging and merge conflicts in detail in the upcoming sections.

Summary: Git Pull

In this lesson, you learned about the git pull command and got some hands-on practice.

What is Git Pull? The git pull command is used to fetch the contents of a remote repository and "pull" those contents onto your local machine. It updates the local repository to match the remote repository.

Git Fetch vs. Pull The git fetch command retrieves the changes in the remote repository without integrating them into your local working directory, whereas git pull downloads the contents from your remote repository and merges it into your working directory.

Git Pull Syntax Run the git pull command using the following syntax:

$ git pull <Remote Name> <Branch Name>

Up next, you'll put yourself into the shoes of a collaborator and learn about the other side of the process: pushing your local code to a remote repository.