When you’re working with the Git version control system, you’ll encounter situations where you want to fetch and download the code associated with a remote repository to your local machine.
For instance, if 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 to 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 and how to use the git pull command to pull code from a remote Git repository.
How to Pull Code
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. This means a copy of the code from the repository and the Git commits associated with the repo will be created and saved to your local machine.
Following this, a 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
Now we know the basics of how the git pull command line operation works, we can walk through a few examples of how to use the command.
Here is the basic syntax of the git pull 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.
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 using this command:
git pull origin
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. In this case, the README.md file in our repository was different from the one on our local machine. So, 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.
Pull a Branch from a Repository
When you’re working with a Git branch, you may decide that you want to pull the code from one specific branch and save it to your local machine. For instance, if you’re working with code on a branch called “dev-v0.9”, you may want to pull only 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>
So, 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. Then, when we run the git pull origin command, a copy of the code on the “dev-v0.9” branch is retrieved and saved to our local machine. Once the code has downloaded, a git merge operation will be executed.
Git Pull Options
There are a few options commonly used with the git pull command.
Prevent Merge Commit
If you want to fetch the remote content but you do not want to create a new merge commit, you can use the –no-commit flag. The syntax for this flag is as follows:
git pull --no-commit <remote>
This command performs the merge operation we discussed earlier and stops just before creating a merge commit. This gives you an opportunity to inspect the changes between your local repository and the remote repository before the merge operation completes.
Use Git Rebase
When you’re using the git pull command, the default is for a merge operation to be run after your code has been retrieved.
However, you can change the default behavior for the command so that a git rebase operation is run after code is retrieved from a remote repository. To do so, you can use this command:
git pull --rebase <remote>
This command will allow you to run a rebase instead of a merge operation.
Git Pull vs. Git Fetch
Some developers get confused with the differences between the git pull and the git fetch commands. Both these commands have the same purpose — to download content from a remote repository — but they operate in different ways.
The git fetch command is used to instruct your local Git repository to retrieve the latest metadata from the remote repository. When you run git fetch, the code from a remote repository will be downloaded but the local repository will not be changed. Instead, git fetch checks to see if any changes exist between a remote repository and your local copy of the repository.
The git pull command, on the other hand, checks for changes between your local and remote repositories, then copies any changes from a remote repository and pushes them to your local repository. This means that, when you are using the git pull command, you should make sure you are ready to accept the changes that could exist on a remote repository, otherwise your repository may encounter conflicts between your code.
The git pull command allows developers to fetch and download the code stored in a remote Git repository. This command is similar to git fetch, but after the code has been fetched from a remote repository it is pushed to the local machine.
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!