Git Pull



The git pull command is used to fetch and download content from the remote repository and directly integrates those changes into the local repository. In the Git-based workflow, it is a common process to merge changes into the local repository. The git pull command is a combination of two commands, git fetch followed by git merge.


How git pull works

The git pull command starts by executing a git fetch command to download content from the remote repository. After the fetching, a git merge command is invoked to integrate remote content into a new local merge commit. To better understand the git pull command, let us assume the following example. We have a repository with a master branch and a remote origin.

before git pull

In our example, if we execute the git pull command, it will download all the modifications from the point where the local and master branch diverges. In our example, that point is B. Here the remote diverged commits are E, F, and G that will be fetched by git pull. Then a new local merge commit, including the content of the three remote diverged commits, will be created.

after git pull

The above illustration shows a new commit H, which is a merge commit that includes all the contents of E, F, and G remote commits and has a combined log message. This is one of the git pull merging strategies.

There are other git pull merging strategies. A rebase merging strategy can be used by passing --rebase to the git pull. The illustration below shows a rebase pull strategy. Let us assume that we are at the beginning of the first illustration, and we have run git pull --rebase.

git pull --rebase

The illustration above shows that the git pull --rebase does not create a new H commit. However, the git pull rebase copied the remote commits E, F, G and rewrote the local commits B, C, D to put them in the last commits in the local origin/master.


Git pull command options and usage

The git pull has several options. The following list shows different use cases:

  • git pull <remote>: It will fetch the remote content and immediately merge it into the local branch. It is the same as git fetch <remote> followed by git merge origin/<current_branch>.

    $ git pull <remote>
    
  • git pull --no-commit <remote>: It will fetch the remote content but does not create a new merge commit.

    $ git pull --no-commit <remote>
    
  • git pull --rebase <remote>: It will fetch the remote content, but instead of using git merge to integrate the dowloaded content, it will use git rebase

    $ git pull --rebase <remote>
    
  • git pull --verbose: It will give verbose output about the content being downloaded and the merge details.

    $ git pull --verbose 
    

Git pull and syncing

The git pull is one of the commands that play a role in the syncing process. Other commands have a responsibility in the syncing process. The git remote specifies what remote endpoints the syncing will operate on. The git push upload content to the remote repository.

The git fetch command look similar to git pull. They both download remote content. However, git fetch download just content, and the other hand, git pull download content and integrate them into the local repository. This is why git fetch is considered safe, and git pull is considered unsafe.


Pulling via Rebase

Many developers prefer rebasing over merging. The --rebase option ensures a linear history by preventing unnecessary merge commits.

Due to its popularity of pulling with --rebase, there is a dedicated configuration option for it:

$ git config --global branch.autosetuprebase always 

After executing the command above, the git pull command will always integrate downloaded content using a git rebase instead of a git merge.


Git pull examples

The following examples show the usage of git pull in different scenarios.


Default Behavior

Executing git pull is the same as running git fetch origin HEAD followed by git merge HEAD. The HEAD here is a reference that points to the current branch.

$ git pull

Git pull on remotes

In the following example, we execute a checkout and switch to the new_feature branch. After that, a git pull <remote_repository> is invoked to download the new_feature branch from the <remote_repository>. When the download will finish, a git merge will be initiated.

$ git checkout new_feature
$ git pull <remote_repository>

Git pull rebase instead of merge

The following example shows a way to synchronize a central repository's master branch using a rebase.

$ git checkout master
$ git pull --rebase origin

The command above moves local changes onto the top of what other developers have already contributed.



ExpectoCode is optimized for learning. Tutorials and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy.
Copyright 2020-2021 by ExpectoCode. All Rights Reserved.