Fetch, Pull & Push in Git
This article belongs to the Git Basics to Advanced course, which is currently being revisited with updated architectural patterns and content. Content may be refined shortly.
Table of Contents
13 sections
- Understanding Remote Repositories
- Interactive Remote Synchronization Explorer
- Setting Up a Local and Cloned Repository
- Checking Remote Repositories
- 1. Fetching (Getting Changes Without Applying Them)
- 2. Pulling (Fetching + Merging in One Step)
- Pull with Rebase
- 3. Pushing Code
- What Does -u (--set-upstream) Do?
- 4. Handling Rejected Pushes ([rejected - non-fast-forward])
- How to Resolve Safely:
- 5. Summary: Fetch vs Pull vs Push
- 6. Housekeeping: Pruning Stale Remote Branches
Understanding Remote Repositories
When working with Git, we often deal with remote repositories (repos hosted on GitHub, GitLab, etc.). The three key actions when working with remotes are:
- Fetching → Get the latest changes from the remote repo without applying them to your working directory.
- Pulling → Get the latest changes and apply them immediately (fetch + merge).
- Pushing → Send your local changes to the remote repo.
Interactive Remote Synchronization Explorer
Step through the interactive timeline below to see how changes move between the remote server, local tracking branch, and local working branch:
Remote Synchronization: Fetch vs Pull vs Push
Step 1 of 4: All Tiers in Sync at Commit C1
$ git status Remote (GitHub), Local Tracking (origin/main), and Local Branch (main) all match.
Setting Up a Local and Cloned Repository
mkdir project
cd project
git init
echo 'hello first time' > file
git add file
git commit -am file
cd ..
git clone project cloned_project
Now we have two directories:
project(the original repository)cloned_project(a clone of the original repository)
If we compare the config of both, we see how Git tracks remotes:
cat cloned_project/.git/config
cat project/.git/config
The cloned repo has a remote section configured called origin:

Checking Remote Repositories
A cloned repository automatically sets up a remote connection:
cd cloned_project
git remote -v

This shows origin pointing to the original repository for both fetch and push operations.
1. Fetching (Getting Changes Without Applying Them)
git fetch downloads commits, files, and refs from the remote repo into your local repository, updating local tracking branches like origin/master.
Crucially, fetch does not modify your working directory or alter your local branches:
# Make some changes in the remote project:
cd ../project
echo 'Another line added' >> file
git commit -am "remote update"
# Fetch changes in the cloned repository:
cd ../cloned_project
git fetch origin master
# Verify that changes have been fetched but not applied:
git log --oneline --graph
git branch --all

Notice that the changes live on the remote-tracking branch remotes/origin/master. Your local master is untouched until you merge:
git merge origin/master
2. Pulling (Fetching + Merging in One Step)
Instead of running fetch and merge separately, git pull downloads and integrates incoming changes in a single command:
git pull origin master
Pull with Rebase
By default, running git pull performs a merge commit if remote and local have diverged. To maintain a clean linear history without cluttering merge commits, use --rebase:
git pull --rebase origin master
3. Pushing Code
When you create commits locally and want to publish them to the remote repository for collaborators to access:
cd cloned_project
git checkout -b feature
echo 'Added new feature' >> file
git commit -am 'Feature added'
# Push feature branch to origin and configure upstream tracking:
git push -u origin feature
What Does -u (--set-upstream) Do?
The -u (or --set-upstream) flag links your local feature branch to origin/feature on the remote server. Git records this tracking relationship in .git/config:
- Once set, you no longer need to type
git push origin featureorgit pull origin feature. - You can simply type
git pushorgit pull. git statuswill accurately inform you:Your branch is ahead of 'origin/feature' by 1 commit.
4. Handling Rejected Pushes ([rejected - non-fast-forward])
A common scenario in team environments: you run git push origin main and Git refuses:
! [rejected] main -> main (fetch first)
error: failed to push some refs to 'https://github.com/organization/project.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref.
How to Resolve Safely:
- Fetch incoming remote changes:
git fetch origin - Inspect what changed on remote:
git log HEAD..origin/main --oneline - Rebase your local commits on top of the remote tip:
git pull --rebase origin main - Push cleanly:
git push origin main
5. Summary: Fetch vs Pull vs Push
| Command | Action | Working Directory | Remote Server |
|---|---|---|---|
git fetch | Downloads remote commits into local tracking cache (origin/*). | Untouched (safe to run anytime) | Unchanged (read-only) |
git pull | Downloads remote commits and integrates them into active branch. | Updated (may produce merge commits or conflicts) | Unchanged (read-only) |
git push | Uploads local branch commits to the remote repository. | Untouched | Updated with new commits |
6. Housekeeping: Pruning Stale Remote Branches
When teammates delete merged branches on GitHub, your local repository still retains their remote-tracking pointers (origin/feature-xyz). Clean up stale tracking branches with:
git remote prune origin