Signup/Sign In

Git Pull

When working in a team we need to make sure that others have access to our work and we have access to their work. Just like there is a Git Push command to share our work with others, we also have a Git Pull command to download other people's work from a remote repository. In this tutorial, we will learn more about Git Pull and how to use it.

What is Git Pull?

  • Git Pull is a way to download changes that have occurred on the remote repository by other developers you are collaborating with.
  • It helps us to update our local repository and make sure that we are on the same page as other members of the team.
  • It is also a good idea to pull from the remote repository before pushing as we may not be aware of the changes on the remote repository and might need to change our local code accordingly.

Git Push and Git Pull

How does Git Pull work?

  • Git Pull is a two-step process - first, it fetches or downloads the files from a remote repository, and then it merges these files into our current working branch.
  • In the first step, Git Pull downloads all the updates or commits that have taken place in the remote repository that our local repository is unaware of. Git knows the commit point from where it should start fetching from the remote repository with the help of remote-tracking branches.
  • The new commits that are fetched are added to the remote-tracking branch.
  • Next Git merges the remote-tracking branch and the branch on which we are currently working into a single branch by creating a new commit point.
  • This new commit will have the changes that were made to the remote repository and will also have the changes which we made to our local repository.

To understand the working of git pull better consider the following example.

  • Suppose we cloned a remote repository that has three commits. Now in our local repository, we will have the remote-tracking branch(origin/master) and local master branch pointing at the last commit.

After cloning both remote tracking branch and local master branch ref point to the same commit.

  • Next suppose we add one more commit to the local repository and some other developer adds one more commit to the remote repository.

After adding the new commits to the remote repo and the local repo

  • Now when we run the git pull command, Git first fetches the new commit point of the remote repository and adds it to the remote-tracking branch referenced by origin/master.

After running the git pull command, Git first fetches and updates the remote tracking branch

  • Next Git merges these two branches and creates a new commit that has both the local as well as remote changes. This new merge commit point F will have two parents D and E.

After merging the two branches a new commit point is created having the changes of remote as well as local repository

Git Pull Command

Several options can be used with the git pull command. Let's take a look at some of the important ones.

To simply pull from a remote repository

$ git pull <remote-repo-url>

We can use --rebase flag to add the individual commit points of the remote repository and shift the local commit points instead of creating just a new single commit point. Consider the example where we had one new commit point in the remote repository along with a new commit on our local repository.

After running the git pull command, the remote tracking branch is updated with the new commits.

Now if we run the git pull command with the --rebase flag the remote repository commit point "E" will take the place of "D" and "D" itself would shift one place to the right and becomes the most recent commit.

Git pull with rebase flag will shift the commit of local master and add the commit of remote repository in its place.

$ git pull --rebase <remote-repo-url>

We can use --force flag to force a pull that otherwise might be blocked by Git. Git might block a pull if the pull overwrites the files which we have committed locally.

Git Pull vs Git Clone

Even though both git pull and git clone commands interact with a remote repository and fetch its content, their roles are quite different. We usually run git clone only once at the beginning to get a copy of the remote repository. But we will be using git pull quite often to get the updates made to the remote directory. When cloning a repository we get the entire history of the commits that were made to the repository but while pulling a repository we only get the updates that were not present in our local repository.


Git Pull is used to download and update our local repository with the changes made to the remote repository. It works by first fetching the changes and then merging them into our working branch. It internally uses the Git Fetch and Git Merge command to accomplish this. Remote-tracking branches play an important role in the fetching of the new commits. We learned about the --rebase flag that shifts the base of our branch to accommodate the new commits instead of merging them. We can also the --force flag to forcefully pull a branch that otherwise may be blocked by Git. I hope this tutorial was helpful and you learned something new. In the next tutorial, we will learn about the git fetch command.

About the author:
I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development. Founder @ Studytonight