Git Course 0%

git pull

Intermediate Git CLI Careful ≈ 6 min

Summary: git pull is git fetch followed by an integration of the fetched commits into your current branch: a merge by default, a rebase with --rebase.

Careful — it changes your working tree and can stop on a conflict. It refuses rather than overwrite uncommitted work, so nothing is lost, but it is not the command to run absent-mindedly mid-edit.

What it does

Fetches, then merges origin/<branch> into your current branch. With nothing of your own to integrate the merge is a fast-forward; with commits on both sides it creates a merge commit, or replays your commits with --rebase.

Why it exists

The two-step "get their work, then integrate it" is what you want most mornings, so Git provides it as one command.

When to use it

  • At the start of work, on main, before branching.
  • Before pushing, when your push was rejected because someone pushed first.
  • Whenever you actually want the team's changes in your files.

When not to use it

  • To look without acting: git fetch.
  • Mid-edit with uncommitted changes in files the incoming commits touch; commit or stash first.
  • After a rebase, when a push was rejected: pulling would merge the pre-rebase commits back in (lesson 6.7).

Syntax

Form Meaning
git pull Fetch and merge from the tracked remote branch
git pull --rebase Fetch and rebase your commits on top instead
git pull --ff-only Only if it fast-forwards; otherwise stop and change nothing
git pull --no-edit Accept the default merge message without opening an editor
git pull origin main From a specific remote and branch
git pull origin main --allow-unrelated-histories Join two histories with no common commit (the new-project README case)

The default is set once by configuration: git config --global pull.rebase false for merge, true for rebase (lesson 3.5).

Examples

The common case, when you have nothing of your own:

Terminal
$ git pull
Updating 216f956..01268bf
Fast-forward
 docs/faq.md | 4 ++++
 1 file changed, 4 insertions(+)

With commits on both sides, a merge commit:

Terminal
$ git pull
Merge made by the 'ort' strategy.
 data/trails.csv | 1 +
 docs/faq.md     | 2 +-
 2 files changed, 2 insertions(+), 1 deletion(-)

The same situation with --rebase, leaving a straight line:

Terminal
$ git pull --rebase
Successfully rebased and updated refs/heads/main.

Refused because of uncommitted work:

Terminal
$ git pull
error: Your local changes to the following files would be overwritten by merge:
	README.md
Please commit your changes or stash them before you merge.
Aborting

And a conflict, which is a pause rather than a failure:

Terminal
$ git pull
From https://gitlab.com/northwind-trails/trailguide
   b6ae751..e3e760c  main       -> origin/main
Auto-merging docs/faq.md
CONFLICT (content): Merge conflict in docs/faq.md
Automatic merge failed; fix conflicts and then commit the result.

Expected result

Your branch advances, your files change to match, and git status reports the new position. On a conflict, the pull pauses with unmerged paths and nothing is committed until you resolve (lesson 11.3) or git merge --abort.

Common mistakes

  • Pulling with a dirty working tree. Git names the files; commit or stash.
  • Pulling after a rebase because the push was rejected. That reintroduces the old commits; --force-with-lease is the right answer there.
  • fatal: refusing to merge unrelated histories on a new project created with a README. Once, with --allow-unrelated-histories, or recreate the project empty (lesson 7.3).
  • Expecting --rebase to be safe on shared branches. It only rewrites your unpushed commits, so it is safe; a plain git rebase on shared commits is not.
  • Pulling on the wrong branch. The integration lands wherever you are; check the first line of git status.

How to undo or recover

  • Mid-conflict: git merge --abort (or git rebase --abort after pull --rebase).
  • A completed pull that you did not want, not yet pushed: git reset --hard <hash> back to the commit from git reflog Dangerous, after checking git status for uncommitted work.

In VS Code

… menu → Pull, or the sync button, which pulls then pushes. Turning on Git: Autofetch makes the ahead/behind counts accurate so you know when a pull is needed.

In IntelliJ IDEA

Update Project (Ctrl+T / ⌘T), whose dialog offers merge or rebase and remembers the choice.

git fetch is the half that only looks · git merge and git rebase are the two integrations · git push is the other direction · git stash parks work that blocks a pull.

Lessons that use this command

fetch vs pull; origin/main vs main · When someone pushed before you · The daily routine · Lab 2.

Key terms

Pull Fetch Merge Rebase Fast-forward