Definition

The git pull command fetches and downloads content from the remote repository and integrates changes into the local repository. The git pull command is called as the combination of git fetch followed by git merge.

gitpull

Watch a course Git & GitHub - The Practical Guide

Git pull usage

The git pull command is one of the commands that are involved in the "syncing" process. These commands work on the remote branches that are configured with the git remote command. The commits are uploaded with git push and download with git fetch and git pull. After making changes in both cases, git merge is used to integrate changes. Both git fetch and git pull are used for downloading the content from the remote repository. The git fetch command does not force to merge the changes into the repository while the git pull command not only downloads the remote content but also merges it into the current working copy.

How it works

To understand the pull and merging process let’s assume the following example. There is a repository with a master branch and a remote origin. The git pull command downloads the changes from the point where the local and the master branches are diverged. The illustration shows that point is E. Here you can see the A, B and C remote commits that will be fetched by git pull. Then a new local merge commit with the content of the new diverged remote commits will be created.

gitpull1

The illustration shows the H new commit which contains all the contents of A, B, and C commits with combined log message.


gitpull2

The git pull with --rebase option is used for merging instead of git merge.

gitpull3

Common options

git pull <remote> Fetches the remote content and directly merges it into the local copy (equivalent to git fetch <remote> followed by git merge origin/<current-branch>).
git pull --no-commit <remote> Fetches the remote content but does not create a merge commit.
git pull --rebase <remote> Integrates the remote branch with the local one.
git pull --verbose Shows downloaded content and merge details giving verbose output during a pull.

Examples

Invoking git pull is equivalent to git fetch origin HEAD and git merge HEAD. HEAD is reference that points to the current branch.

git pull

Pull changes from a specific branch of a remote repository:

git pull <remote> <branch>

This command will fetch and merge the changes from the specified branch of the remote repository.

For example, if you want to pull changes from the develop branch of the origin remote, you would use the following command:

git pull origin develop

Pull changes from a remote repository and rebase your changes on top of them:

git pull --rebase

This command will fetch the changes from the remote repository and rebase your local changes on top of them, rather than merging them. This can be useful if you want to keep your commit history linear and avoid merge commits.

  1. Pull changes from a remote repository, but don't merge them automatically:
git pull --no-commit

This command will fetch the changes from the remote repository, but won't automatically merge them into your local branch. Instead, it will leave the changes as staged changes, which you can review and commit manually.

Practice Your Knowledge

Which of the following statements about the 'git pull' command are true?

Quiz Time: Test Your Skills!

Ready to challenge what you've learned? Dive into our interactive quizzes for a deeper understanding and a fun way to reinforce your knowledge.