Git & GitHub ยท Chapter 27 of 42

git pull

`git pull` downloads new commits from a remote repository and integrates them into your current local branch. It's essentially a `git fetch` followed by a `git merge`.

Running `git pull` regularly keeps your local branch up to date with your teammates' work, reducing the chance of large, painful merge conflicts later.

Syntax
git pull
git pull --rebase

Basic pull

Run `git pull` on a branch that's tracking a remote branch to fetch and merge new commits in one step.

Pull with rebase

Run `git pull --rebase` to fetch remote changes and replay your local commits on top of them, keeping history linear instead of creating a merge commit.

Example 1 (bash)
git pull
Output
remote: Enumerating objects: 5, done.
Updating a3f5c9e..9c2e1aa
Fast-forward
 app.js | 3 +++

Fetches and merges new commits from the tracked remote branch into your current branch.

Example 2 (bash)
git pull --rebase origin main
Output
Successfully rebased and updated refs/heads/main.

Fetches from origin's main branch and replays local commits on top for a linear history.

Key points

  • git pull fetches and merges remote changes in one step.
  • It's equivalent to git fetch followed by git merge.
  • git pull --rebase avoids extra merge commits.
  • Pulling regularly reduces the risk of large merge conflicts.
๐Ÿ’ก Note: Commit or stash local changes before pulling if you have uncommitted edits that might conflict.

๐Ÿ“ Quick Quiz

1. What does git pull do?

2. git pull is equivalent to which two commands combined?

3. What does git pull --rebase do differently?