The git pull command retrieves and downloads the contents of a repository to your local machine. Your local repository is updated so that it shows the content from the remote repository.
You’ll probably encounter situations where you want to fetch and download the code associated with a Git repository to your local machine.
Let’s say you’re working on an application that tracks orders at a local grocery store. As you make changes to the app, you’ll want to pull code from the remote repository. This will ensure you are always working with the most up-to-date code.
That’s where the git pull command comes in. The git pull command is used to retrieve content from a remote repository which is then saved to your local machine. In this tutorial, we’ll discuss, with examples, the basics of pulling code using the git pull command.
What is the Git Pull Command?
The term “pulling code” describes the process of downloading the content from a remote repository and saving it to your computer.
To pull code in Git, you can use the git pull command. The git pull command is a helpful command that executes two other commands: git fetch and git merge.
Let’s break down how the git pull command works.
First, when you run git pull, the remote repository you are pulling will be downloaded. A copy of the code from the repository and the Git commits associated with the repo will be saved to your machine.
Following this, a Git merge operation is executed. This operation merges the code on your local machine with the newly-retrieved code, creating one final version of the codebase. This version will be equal to the one you have retrieved from a remote branch.
The Git Pull Command
The git pull command retrieves a remote repository and downloads its code to your local version of a repository. Let’s take a look at the command:
git pull <remote>
The “remote” parameter refers to the remote repository you want to pull to your local machine. When you run this command, the remote repository will be retrieved then merged into your local copy of the repository.
The git pull command does not affect untracked files. You will only receive changes that have been made to files on the remote branches that are being tracked by Git. These changes will be saved to your local working tree.
git pull origin
Suppose we wanted to retrieve the contents of our “origin” repository. This is the default “remote” value used with a repository. We could do so by pulling the origin master branch:
git pull origin/master
This command returns:
From https://github.com/jamesgallagher432/demo-repository b53b22d..a7d8dc2 master -> origin/master Updating b53b22d..a7d8dc2 Fast-forward README.md | 2 ++ 1 file changed, 2 insertions(+)
First, the command retrieved the remote repository with which we are working. Then, the changes identified are merged into the local copy of our code.
The README.md file in our repository was different from the one on our local machine. When we ran the git pull command, the changes were identified and merged to our local machine.
Now that we have run the git pull command, our local machine stores the same code as our remote repository. This means that, when we make changes to our code, we know that they will be compatible with the latest version of the repository. When new commits are pushed to the remote repository, we can use git pull again to get an updated version of the repository.
We can check that our files have been updated by running the git log command. This command shows us all of the commits that have been made to a repository.
Git Pull Remote Branch from a Repository
Let’s say that you want to pull the code from one specific branch and save it to your local machine. You’re going to work with code on a branch called “dev-v0.9”. You only want to pull the code for that branch.
You can do so by executing the git checkout command, followed by the git pull command. Here’s the syntax for this operation:
git checkout <branch name> git pull <remote>
For our example, we would run the following commands:
git checkout dev-v0.9 git pull origin
When we run the git checkout command, we are moved to the “dev-v0.9” branch. The git pull origin command creates a copy of the code on the “dev-v0.9” branch is retrieved and saved. Once the code has downloaded, a git merge operation will be executed. This will update our local repository so it shows the same code as our remote.
Git Force Pull
You can force a pull operation. Forcing a git pull common if you encounter an untracked file error that you want to discard.
Before you force a pull operation, you should make sure that you are sure that you are comfortable with losing any local changes. Forcing a pull will overwrite all your local changes.
To force a pull, first fetch the metadata for the commit you want to save to your local machine:
git fetch --all
Next, you should back up your current branch. Backing up your current branch will make sure that you don’t lose any changes that you may want to come back to later:
git branch -b backup
Now that we have a backup of our branch, we can pull our changes. We can do this by using the reset command:
"Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!"
Venus, Software Engineer at Rockbot
git reset --hard origin/master
You should replace origin/master with the origin and branch names associated with the project you are working with. Origin represents the origin repository. Master is the name of the branch from which we are fetching our code.
Read more about how to force a Git pull in our git force pull guide.
Git Pull vs. Git Fetch
Both git fetch and git pull retrieve the contents of a remote repository. Some developers get confused with the differences between the git pull and the git fetch commands.
The git fetch command only retrieves the metadata from the remote repository. This metadata is used to check if there are any changes available that can be pulled to a local machine. The git pull command downloads both metadata and the files that have been changed.
The git pull command first executes a fetch operation. If changes are identified between a local and remote repository, those changes will be downloaded.
When you are using the git pull command, you should make sure you are ready to accept the changes. Otherwise, your repository may encounter conflicts between your code when you go to commit your work.
In summary, use git fetch to check if any changes exist between your local repository and a remote. Use the git pull command if you want to retrieve the metadata for your remote repository and download any changes that have been made.
The git pull command fetches and downloads the code stored in a remote Git repository. The git pull command is similar to git fetch. git fetch only retrieves metadata. git pull retrieves metadata and the changes made to files in a repository.
This tutorial explored the basics of pulling code and how to use the git pull command to pull code. Now you’re equipped with the knowledge you need to start using the git pull command like a professional developer!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.