git pull
Fetch from and integrate with another repository or a local branch
The 'git pull' command fetches content from a remote repository and immediately integrates it into your local branch. It's essentially a combination of 'git fetch' followed by 'git merge' (or 'git rebase' with --rebase flag). This is the most common way to synchronize your local repository with changes made by others. When you run git pull, Git first downloads all new commits from the remote, then integrates them into your current branch. If your branch is simply behind, it fast-forwards; if both sides have new commits, modern Git stops and asks you to choose merge or rebase (--no-rebase / --rebase, or the pull.rebase setting). If there are conflicting changes, Git will pause and ask you to resolve the conflicts before completing the merge.
Synopsis
git pull [--rebase | --no-rebase] [--ff-only | --ff | --no-ff] [--commit | --no-commit] [--autostash | --no-autostash] [-q | --quiet] [-v | --verbose] [--depth=<depth>] [--prune] [--tags | --no-tags] [--recurse-submodules[=yes|on-demand|no]] [<repository> [<refspec>...]]Options
Choosing the Right Option
Pull Strategy: Merge vs Rebase
The fundamental choice that affects your commit history
| Command | Creates Merge Commit | History Style | Best For |
|---|---|---|---|
git pull | Depends on config (fails if diverged and unset) | Set by pull.rebase / pull.ff | Fast-forward when only behind |
git pull --rebase | No | Linear | Feature branches, clean history |
git pull --no-rebase | Yes (if diverged) | Preserves branches | Override rebase config, force merge |
git pull --ff-only | No | Linear (fails if diverged) | Ensuring no accidental merges |
Safety & Control Options
Options to prevent mistakes and maintain control
| Command | Prevents | Behavior | When to Use |
|---|---|---|---|
--ff-only | Unexpected merges | Fails if can't fast-forward | CI/CD, protected branches |
--no-commit | Auto-commit | Merges but stops before commit | Review merge before committing |
--autostash | Dirty workdir errors | Stash → pull → pop | Quick sync with uncommitted work |
--dry-run | Any changes | Shows what fetch would download; no merge | Preview incoming refs |
What Gets Fetched
Control which refs and history are downloaded
| Command | Fetches Tags | Fetches Full History | Use Case |
|---|---|---|---|
git pull | Auto (followed tags) | Yes | Normal workflow |
--tags | All tags | Yes | Sync release versions |
--no-tags | No | Yes | Save bandwidth, branch-only sync |
--depth=N | Auto (followed tags) | Last N commits only | Large repos, CI builds |
--unshallow | Auto (followed tags) | Converts shallow to full | Need full history after shallow clone |
Remote & Branch Management
Options for handling multiple remotes and stale refs
| Command | Scope | Cleans Up | Scenario |
|---|---|---|---|
git pull origin main | Single remote/branch | No | Standard pull from origin |
--all | All remotes | No | Sync from origin + upstream |
--prune | Single remote | Deleted branches | Remove stale tracking refs |
Advanced Scenarios
Options for complex or unusual situations
| Command | Solves | Risk Level | Typical Scenario |
|---|---|---|---|
--allow-unrelated-histories | No common ancestor | Medium | Merging two separate repos |
-s <strategy> | Special merge needs (does not avoid conflicts) | Low | Force specific merge algorithm |
--recurse-submodules | Outdated submodules | Low | Projects with git submodules |
Workflow Examples
git statusCheck for uncommitted changes that might conflict
git stashStash any uncommitted work if needed (or use --autostash)
git pull --rebase origin mainPull with rebase to keep history clean
git stash popRestore your stashed work if you stashed earlier
git checkout feature-branchSwitch to your feature branch
git pull --no-rebase origin mainMerge the latest main into your feature branch (explicit, since the branches have diverged)
git push origin feature-branchPush the updated feature branch
git fetch originDownload remote changes without modifying working directory
git log HEAD..origin/mainSee what commits you're about to pull
git diff HEAD...origin/mainReview the changes made on the remote
git pull origin mainPull once you're satisfied with the changes
git pull --no-rebase origin mainAttempt a merge pull - may result in conflicts
git statusSee which files have conflicts (marked as 'both modified')
(edit conflicted files)Open files and resolve conflict markers (<<<<<<<, =======, >>>>>>>)
git add <resolved-files>Stage the resolved files
git commitComplete the merge with a commit
git pull --rebase --autostash origin mainAutomatically stash changes, pull, then reapply them
Troubleshooting
Your local changes would be overwritten by merge
Cause: You have uncommitted changes in files that the pull would modify
Commit your changes first (git add . && git commit), stash them (git stash), or use git pull --rebase --autostash
git stash && git pull && git stash popRefusing to merge unrelated histories
Cause: The remote and local repositories have no common ancestor (e.g., pulling into a newly initialized repo)
Allow unrelated histories to merge, which is common when adding a remote to an existing local repo
git pull --allow-unrelated-histories origin mainMerge conflict in file(s)
Cause: Both you and the remote made changes to the same lines in a file
Open conflicted files, look for markers (<<<<<<<, =======, >>>>>>>), edit to resolve, then git add and git commit. Or abort with git merge --abort
git add <resolved-file> && git commitNot possible to fast-forward, aborting
Cause: You used --ff-only (or pull.ff=only is set) but your local branch has diverged from remote
Use rebase instead (git pull --rebase) or allow a merge commit (git pull --no-rebase)
git pull --rebase origin mainCannot pull with rebase: You have unstaged changes
Cause: A rebase pull needs a clean working tree, and you have uncommitted modifications
Commit or stash your changes, or use autostash to automatically handle them
git pull --rebase --autostashNo tracking information for the current branch
Cause: Your branch isn't set up to track a remote branch
Set the upstream branch explicitly, or specify the remote and branch in the pull command
git branch --set-upstream-to=origin/main main'origin' does not appear to be a git repository
Cause: No remote named 'origin' is configured
Check your remotes with git remote -v and either add origin or use the correct remote name
git remote add origin <url>You have divergent branches and need to specify how to reconcile them
Cause: Both sides have new commits and neither pull.rebase nor pull.ff is set, so modern Git refuses to guess (older 2.27+ only warned)
Specify merge (--no-rebase) or rebase (--rebase), or configure a default with git config pull.rebase
git config --global pull.rebase trueTips & Pitfalls
Best Practices
- ✓Always check git status before pulling to avoid conflicts with uncommitted changes
- ✓Use git pull --rebase to maintain a clean, linear commit history on feature branches
- ✓Configure pull.rebase=true globally if you prefer rebase by default: git config --global pull.rebase true
- ✓Use git fetch first when you want to review changes before integrating them
- ✓Use --ff-only when you want to ensure no merge commits are created accidentally
- ✓Set up branch tracking with git push -u on first push to simplify future pulls
- ✓Use --autostash when you have uncommitted changes but need to pull quickly
- ✓Pull from main/master regularly to avoid large, complex merges later
- ✓Resolve conflicts immediately - never leave your repository in a conflicted state
- ✓Use git pull --prune to clean up stale remote-tracking branches
- ✓Consider using git fetch + git merge for more control over the integration process
- ✓Always verify you're on the correct branch before pulling to avoid mixing changes
Common Mistakes
- ✗Pulling with uncommitted changes: This can cause merge conflicts or fail entirely. Always commit or stash your changes first, or use --autostash to automatically handle this.
- ✗Not understanding pull vs fetch: 'git pull' automatically merges, which can be surprising. Use 'git fetch' first if you want to review changes before integrating them.
- ✗Using merge when rebase is cleaner: On feature branches, 'git pull --rebase' creates a cleaner linear history without unnecessary merge commits.
- ✗Ignoring merge conflicts: After a pull with conflicts, you must resolve them and commit. Never leave your repository in a conflicted state.
- ✗Pulling into the wrong branch: Always check your current branch with 'git status' before pulling. Pulling into main when you meant to pull into a feature branch can cause issues.
FAQ
git fetch only downloads new commits from the remote without modifying your working directory or local branches. git pull does fetch AND automatically merges (or rebases) those changes into your current branch. Use fetch when you want to review changes before integrating them.
Use --rebase when you want a clean, linear history (especially on feature branches). This replays your local commits on top of the fetched commits instead of creating a merge commit. Use regular pull (merge) when you want to preserve the exact history of when changes were made.
Fast-forward happens when your local branch has no new commits that the remote doesn't have. Git simply moves your branch pointer forward to match the remote - no merge commit is needed. If you have local commits, Git must either create a merge commit or rebase - and unless you pass --rebase/--no-rebase or set pull.rebase, modern Git stops with 'Need to specify how to reconcile divergent branches'.
Use git pull --rebase to replay your local commits on top of the remote commits, creating a linear history. Or use git pull --ff-only which will only succeed if a fast-forward is possible (fails if you have local commits ahead).
It depends. With a merge pull, Git allows it if the incoming commits don't touch the files you modified; otherwise it refuses with 'Your local changes ... would be overwritten by merge'. With --rebase, any uncommitted change blocks the pull. Best practice: commit your changes first, or use git pull --rebase --autostash to automatically stash and restore your changes.
Git will pause the merge and mark conflicted files. Open each conflicted file, look for conflict markers (<<<<<<<, =======, >>>>>>>), edit to resolve, then git add the file and git commit to complete the merge. Use git merge --abort to cancel (or, for git pull --rebase, git rebase --continue / git rebase --abort).
Right after the pull, run git reset --hard ORIG_HEAD - Git saves your previous tip in ORIG_HEAD for merges, fast-forwards and rebases. Don't use HEAD~1: a fast-forward may have brought several commits. --hard also discards uncommitted changes. If ORIG_HEAD has moved since, find the old tip with git reflog.
This usually means you're pulling from the wrong branch or remote. Check: git branch -vv to see tracking info, git remote -v to verify remotes, and git fetch --all followed by git log origin/main to see remote commits.
git pull origin main explicitly specifies the remote and branch to pull from. git pull (no arguments) uses the tracking branch configured for your current branch. If tracking isn't set up, you'll get an error asking you to specify.
Set globally: git config --global pull.rebase true (for rebase) or git config --global pull.rebase false (for merge). You can also set per-repository without --global, or per-branch with git config branch.<name>.rebase true.