Git Pull
In short
Git pull is a Git command that downloads new commits from a remote repository and immediately integrates them into your current local branch.
What is git pull?
git pull updates your current branch with work that others have pushed to the remote repository. It is really two commands in one: git fetch, which downloads new commits and updates remote-tracking branches such as origin/main, followed by a step that combines those commits with your local branch. Developers typically pull before starting new work and before pushing, so their branch is based on the latest code.
How the combining step works depends on the situation and your settings. If you have no local commits of your own, Git simply fast-forwards your branch to match the remote. If both sides have new commits, git pull either merges them, creating a merge commit, or, with git pull --rebase, replays your local commits on top of the remote ones. When the branches have diverged and no preference is configured, modern versions of Git stop and ask you to choose by setting pull.rebase or pull.ff.
Pulling is like syncing a shared calendar before adding your own appointments: you first see what everyone else has booked, then fit your plans around it. Because the integration step can hit the same conflicts as any merge or rebase, pulling small and often keeps conflicts rare and easy to resolve. The cautious git pull --ff-only updates your branch only when no merge is needed and otherwise stops so you can decide.
The most common confusion is git pull versus git fetch. Fetch only downloads new commits and never touches your files or branches, so it is always safe to run, while pull goes on to change your current branch. Despite the name, git pull is also unrelated to a pull request: a pull request is a hosting platform feature for asking others to review and merge your branch, while git pull brings other people's commits into yours.
Key takeaways
git pullrunsgit fetchand then merges or rebases the result into your current branch.- It fast-forwards when you have no local commits of your own.
git pull --rebasereplays your local commits on top of the remote ones instead of creating a merge commit.git fetchis the download-only alternative that never changes your working files.- Pulling can cause merge conflicts, which you resolve like any other merge.
Example
# Update the current branch from its upstream branch
git pull
# Roughly the same as these two steps (when on main):
git fetch origin
git merge origin/main
# Replay local commits on top of the remote ones instead of merging
git pull --rebase
# Only update if no merge is needed (never creates a merge commit)
git pull --ff-only
# Set a default once so Git doesn't have to ask
git config --global pull.rebase trueReaders ask
What is the difference between git fetch and git pull?
git fetch downloads new commits from a remote and updates remote-tracking branches like origin/main, but leaves your own branches and files alone. git pull does the same fetch and then merges or rebases those commits into your current branch.
Should I use git pull --rebase?
Many teams prefer it for syncing a personal branch, because it keeps history linear without extra merge commits. Avoid it if your local commits have already been shared with others, since rebasing rewrites them.
How do I undo a git pull?
Right after the pull, git reset --hard ORIG_HEAD moves the branch back to where it was before, discarding the pulled changes. Make sure you have no uncommitted work first, because --hard also throws that away.
Often compared
See also
- Git PushVersion Control, p. 18Git push is a Git command that uploads your local commits to a remote repository, updating the matching branch there so others can see and use your work.
- Git RemoteVersion Control, p. 19A Git remote is a named shortcut, such as origin, to another copy of your repository, usually on a server, that you push commits to and pull commits from.
- MergeVersion Control, p. 29A merge in Git combines the changes from one branch into another, joining separate lines of development back together into a single, shared history.
- RebaseVersion Control, p. 33A rebase in Git replays a branch's commits on top of another commit, rewriting history to produce a cleaner, linear sequence of changes without merge commits.
- Merge ConflictVersion Control, p. 30A merge conflict happens when Git can't automatically combine two branches because both changed the same lines of a file, so a person must decide the result.
- Git CloneVersion Control, p. 13Git clone is a Git command that downloads a complete copy of an existing repository, including its full history, and sets it up on your computer for work.
- Git FetchVersion Control, p. 15git fetch downloads new commits, branches and tags from a remote and updates remote-tracking branches like origin/main but not your own branches or files.
Spotted a mistake or something missing on this page?Suggest an edit