2 minute read

The git pull command is used to pull and download content from a remote repository and instantly update the local repository to reflect that content.

Merging remote top-level changes to your local repository is a common task in Git-based collaboration workflows.

The git pull command is actually a combination of two commands, git fetch followed by git merge.

In the first stage of the git pull operation you will execute a git fetch on the local branch pointed to by HEAD.

After the content is downloaded, git pull will enter a merge workflow.

A new merge commit will be created and HEAD will be updated to point to the new commit.

How does it work

The git pull command first runs git fetch, which downloads the contents of the specified remote repository.

Then git merge is run to merge the remote content headings and references into a new local merge commit.

To better illustrate the incorporation and merge process, let’s look at the following example.

Suppose we have a repository with a master branch and a remote source.

git pull example

In this scenario, git pull will unload all changes from the local branch and master branch breakpoint.

In this example, that point is E.

git pull will pull out the divergent remote commits which are A, B, and C.

The push process will then create a new local merge commit that includes the content of the new divergent remote commits .

git pull example b

In the above diagram, we can see the new commit H, which is a new merge commit that includes the content of the remote commits A, B, and C, and has a combined log message.

This is an example of one of the git pull merge strategies.

A –rebase option can be combined with git pull to use a reorganize merge strategy instead of a merge commit.

The following example shows how a reorganization change check-in works.

Suppose we are at the starting point of our first diagram and we have executed git pull –rebase.

git pull example c

In this diagram, we can now see that checking in reorganization changes does not create the new commit H.

Instead, the reorganization has copied remote commits A, B, and C, and has rewritten local commits E, F, and G to that appear after them in the local origin or master commit history.

Git pull analysis

This is an easy way to sync your local repository with previous changes. The following diagram explains each step in the incorporation process.

git pull example d

At first you think your repository is in sync, but then git fetch reflects that the version of the source’s master branch has changed since you last checked. Then git merge instantly integrates the master branch into the local one.