git push & git pull
Syncing with remotes using git push and git pull — upstream tracking and force push safety.
What you'll learn
- How git push sends local commits to the remote
- How git pull fetches and merges remote changes
- Setting up upstream tracking with -u
- When and why to use force push (and its dangers)
Concept
git push — Sending Changes Upstream
git push uploads your local commits to the remote repository. This is how you share your work with teammates and publish changes to GitHub.
git push origin main
This sends your local main branch to the main branch on origin. If you have set up tracking (with -u), you can simply run git push without specifying the remote and branch.
Upstream Tracking
When you create a new local branch and want to push it to the remote for the first time, use the -u flag:
git checkout -b feature-login
git push -u origin feature-login
The -u (or --set-upstream) flag tells Git to remember the connection. After this, git push and git pull on this branch know which remote branch to use without you specifying it each time.
git pull — Getting Changes Down
git pull does two things in sequence:
git fetch— downloads new commits from the remotegit merge— merges them into your current branch
git pull
Always pull before you push when collaborating. If someone else pushed changes you do not have, your push will be rejected. Pull first to integrate their work, then push.
Push Rejections
If the remote has commits you do not have, Git rejects your push:
! [rejected] main -> main (fetch first)
error: failed to push some refs
The fix is to pull first, resolve any conflicts, then push again:
git pull
# resolve conflicts if any
git push
Force Pushing
git push --force (or -f) overwrites the remote branch with your local version, discarding any remote commits that are not in your local history. This is dangerous on shared branches because it can erase other people's work.
# Dangerous — overwrites remote history
git push --force origin main
# Safer — only force push if no one else added commits
git push --force-with-lease origin main
--force-with-lease is the safer alternative: it only force pushes if the remote has not changed since you last fetched. Always prefer this over --force.
When Force Push Is Appropriate
Force pushing is acceptable on:
- Your own feature branches that no one else is using
- After a rebase or amend that rewrote local history
Never force push to main or any shared branch.
Example
# First push of a new branch — set up tracking
git checkout -b feature-payment
git push -u origin feature-payment
# Subsequent pushes — tracking is set, so just:
git push
# Pull the latest changes from teammates before pushing
git pull
# resolve any merge conflicts if they occur
git push
# If push is rejected (remote has newer commits):
git pull --rebase # rebase your commits on top of remote
git push
# Force push — ONLY on your own branches, never on main
git push --force-with-lease origin feature-payment
# Push all branches at once (rarely needed)
git push --all
# Push tags (covered in the git-tags lesson)
git push --tags
# Delete a remote branch
git push origin --delete feature-payment
Shows setting up upstream tracking, the standard push/pull cycle, handling push rejections with pull --rebase, safe force pushing with --force-with-lease, pushing tags, and deleting a remote branch. The pull-before-push discipline is the most important habit to learn.
Try it
- SSH Key Generator
Set up SSH to avoid repeated password prompts when pushing.
- CI/CD YAML Generator
Pushes trigger CI pipelines — generate one to see the connection.
Common mistakes
The mistake
Force pushing to a shared branch like main and erasing teammates' commits
The fix
Never use --force on shared branches. Use --force-with-lease on your own feature branches only. If you need to rewrite shared history, coordinate with your team first and use git revert instead of force pushing.
The mistake
Forgetting to pull before pushing and getting a rejection error
The fix
Make 'pull before push' a habit. Run git pull (or git pull --rebase) before every git push when collaborating. This integrates remote changes first and avoids rejection errors.
Related Resources
Related Snippets
- Git Remote Snippet
Advanced push and pull patterns.
Related Cheatsheets
- Git Cheatsheet
push, pull, and upstream tracking commands.
Practice git push & git pull
8 exercises
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.