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.
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.
The illustration shows the H new commit which contains all the contents of A, B, and C commits with combined log message.
The git pull with --rebase option is used for merging instead of git merge.
|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.|
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 on remotes¶
In the following example, firstly we execute a checkout and switch to the new_feature branch. Then, we run git pull <remote repo> to pull down the new_feature branch from <remote repo>. After downloading, it will initiate a git merge.
git checkout new_feature git pull <remote repo>
Pulling via rebase¶
Rebasing is prefered over merging. The --rebase option prevents unnecessary merge commits ensuring a linear history. There is a configuration option for pulling with --rebase due to its popularity.
git config --global branch.autosetuprebase always
After running, all git pull commands will be integrated via git rebase instead of git merge.
Let’s assume another example that shows how to synchronize with the central repository's master branch using a rebase which will put your local changes on top of what everybody else has already done.
git checkout master git pull --rebase origin