# Fetching & Pulling

> How to safely download your team's changes, and the crucial difference between fetching and pulling.

Subject: Version Control | Lesson 16 of 29 | 6 min read
Interactive lesson: https://codewithanimation.in/git/git-fetch-and-pull
Previous: [Pushing Changes](https://codewithanimation.in/git/git-push.md) | Next: [Tracking Branches](https://codewithanimation.in/git/tracking-branches.md)

## git pull = fetch + merge

`git fetch` is a safe command that only downloads data (commits, branches, and tags) from a remote repository. It updates your remote-tracking branches (like `origin/main`), but it does **not** touch your local working files.

`git pull` is an aggressive command. It runs `git fetch` to download the data, and then immediately runs `git merge` to combine that data into your current working branch.

## Why it matters

When working on a team, the remote repository is constantly moving forward as teammates push their work.

To see their work, review their code, or integrate their features with your own, you must bring that data down to your local machine. Understanding the difference between `fetch` and `pull` prevents you from accidentally triggering unwanted merge conflicts.

## How it works

When you run `git fetch`, Git communicates with the remote server, grabs all the commits you don't have, and stores them locally. Your working directory remains unchanged. You can inspect the new commits safely.

When you run `git pull`, Git fetches that same data, but then attempts to merge it into whatever branch you are currently on. If your local work conflicts with the pulled work, the pull will halt with a merge conflict.

## Common mistake: Pulling into a dirty working tree

The #1 common mistake is running `git pull` while you have uncommitted changes (a dirty working tree).

Git will often abort the pull to protect your uncommitted work from being overwritten. Beginners get confused by this error. **The Fix:** Always commit (or stash) your local changes before running `git pull`.

## Tip: Fetch is safe, Pull is destructive

Pro tip: If you just want to see what your teammates have been doing, or check if the remote has new commits, use `git fetch`. It's a completely safe, read-only operation.

Only use `git pull` when your local branch is clean and you are absolutely ready to integrate the team's code.

## Try it: Separate fetching from pulling

Fetch the remote history first, then integrate it into the local branch.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/git/git-fetch-and-pull

## Check yourself

### Question 1

What two Git commands are executed sequentially when you run `git pull`?

- A. `git commit` then `git push`
- B. `git fetch` then `git merge`
- C. `git clone` then `git checkout`
- D. `git fetch` then `git rebase`

Correct answer: B

`git pull` is essentially a macro. It first runs `git fetch` to download the remote data, and then immediately runs `git merge` to integrate it into your current branch.

- Why not A: That is how you send work, not receive it.
- Why not C: `clone` is only used once at the beginning of a project.
- Why not D: By default, `pull` uses `merge`, not `rebase` (unless configured otherwise).

### Question 2

Why is `git fetch` considered a 'safe' operation compared to `git pull`?

- A. Because it requires a password to execute.
- B. Because it only downloads data and does not modify your working directory or local branches.
- C. Because it automatically deletes broken code.
- D. Because it only downloads code from branches named 'main'.

Correct answer: B

`git fetch` only updates your remote-tracking references (like `origin/main`). It leaves your actual local branch and the files in your editor completely untouched.

- Why not A: Both commands require authentication if the repo is private.
- Why not C: Git never automatically deletes your code to fix it.
- Why not D: It fetches data from all tracking branches.

### Question 3

What is the most common reason Git will abort a `git pull` command?

- A. Your internet connection is too fast.
- B. The remote repository is empty.
- C. You have uncommitted changes in your working directory that would be overwritten by the merge.
- D. You are on the `main` branch.

Correct answer: C

Git is designed to never lose your work. If a `git pull` involves a merge that would overwrite changes you haven't committed yet, Git will safely abort the operation.

- Why not A: Not a real error.
- Why not B: If it's empty, `pull` just does nothing.
- Why not D: You can pull on any branch.

## Remember this

- `git pull` = `git fetch` + `git merge`.
- `git fetch` safely downloads data without touching your files.
- Always commit or stash your work before running `git pull`.
