Pulling
Unlike pushing with Git where your local changes are sent to the central repository’s server, pulling with Git takes the current code on the server and ‘pulls’ it down from the repository’s server to your local machine. This topic explains the process of pulling code from a repository using Git as well as the situations one might encounter while pulling different code into the local copy.
Updating with local changes
Section titled “Updating with local changes”When local changes are present, the git pull command aborts reporting :
error: Your local changes to the following files would be overwritten by merge
In order to update (like svn update did with subversion), you can run :
git stashgit pull --rebasegit stash popA convenient way could be to define an alias using :
git config --global alias.up '!git stash && git pull --rebase && git stash pop'git config --global alias.up 'pull --rebase --autostash'Next you can simply use :
git upPull, overwrite local
Section titled “Pull, overwrite local”git fetchgit reset --hard origin/masterBeware: While commits discarded using reset --hard can be recovered using reflog and reset, uncommitted changes are deleted forever.
Change origin and master to the remote and branch you want to forcibly pull to, respectively, if they are named differently.
Pull code from remote
Section titled “Pull code from remote”git pullKeeping linear history when pulling
Section titled “Keeping linear history when pulling”Rebasing when pulling
Section titled “Rebasing when pulling”If you are pulling in fresh commits from the remote repository and you have local changes on the current branch then git will automatically merge the remote version and your version. If you would like to reduce the number of merges on your branch you can tell git to rebase your commits on the remote version of the branch.
git pull --rebaseMaking it the default behavior
Section titled “Making it the default behavior”To make this the default behavior for newly created branches, type the following command:
git config branch.autosetuprebase alwaysTo change the behavior of an existing branch, use this:
git config branch.BRANCH_NAME.rebase trueAnd
git pull --no-rebaseTo perform a normal merging pull.
Check if fast-forwardable
Section titled “Check if fast-forwardable”To only allow fast forwarding the local branch, you can use:
git pull --ff-onlyThis will display an error when the local branch is not fast-forwardable, and needs to be either rebased or merged with upstream.
Pulling changes to a local repository
Section titled “Pulling changes to a local repository”Simple pull
Section titled “Simple pull”When you are working on a remote repository (say, GitHub) with someone else, you will at some point want to share your changes with them. Once they have pushed their changes to a remote repository, you can retrieve those changes by pulling from this repository.
git pullWill do it, in the majority of cases.
Pull from a different remote or branch
Section titled “Pull from a different remote or branch”You can pull changes from a different remote or branch by specifying their names
git pull origin feature-AWill pull the branch feature-A form origin into your local branch. Note that you can directly supply an URL instead of a remote name, and an object name such as a commit SHA instead of a branch name.
Manual pull
Section titled “Manual pull”To imitate the behavior of a git pull, you can use git fetch then git merge
git fetch origin # retrieve objects and update refs from origingit merge origin/feature-A # actually perform the mergeThis can give you more control, and allows you to inspect the remote branch before merging it. Indeed, after fetching, you can see the remote branches with git branch -a, and check them out with
git checkout -b local-branch-name origin/feature-A # checkout the remote branch# inspect the branch, make commits, squash, ammend or whatevergit checkout merging-branches # moving to the destination branchgit merge local-branch-name # performing the mergeThis can be very handy when processing pull requests.
Pull, “permission denied”
Section titled “Pull, “permission denied””Some problems can occur if the .git folder has wrong permission. Fixing this problem by setting the owner of the complete .git folder. Sometimes it happen that another user pull and change the rights of the .git folder or files.
To fix the problem:
chown -R youruser:yourgroup .git/Syntax
Section titled “Syntax”- git pull [options [
[ …]]
Parameters
Section titled “Parameters”| Parameters | Details |
|---|---|
--quiet | No text output |
-q | shorthand for --quiet |
--verbose | verbose text output. Passed to fetch and merge/rebase commands respectively. |
-v | shorthand for --verbose |
--[no-]recurse-submodules[=yes | on-demand |
Remarks
Section titled “Remarks”git pull runs git fetch with the given parameters and calls git merge to merge the retrieved branch heads into the current branch.