Git Fetch, Pull, and Push: Understanding the Differences

Master git fetch, pull, and push — understand the differences, when to use each, prune options, force push dangers, and synchronization best practices.

published: reading time: 23 min read author: Geek Workbench updated: November 17, 2026
Quick Summary

`git fetch` downloads remote refs without integrating them; `git pull` fetches then merges or rebases; `git push` updates remote refs. Whether a push runs CI depends on repository configuration. Before rewriting published history, check who has based work on it and follow team policy. A force-with-lease can guard against an unexpected remote update, but it does not replace coordination.

Introduction

The trio of git fetch, git pull, and git push are the commands that synchronize your local repository with remote repositories. They’re among the most frequently used Git commands, yet their differences and implications are often misunderstood.

Fetch downloads changes without modifying your working files. Pull downloads and merges in one step. Push uploads your commits to a remote. Confusing these operations — especially pull and fetch — is a common source of merge conflicts and lost work.

Understanding what each command changes helps you choose when to inspect first and when to integrate. For upstream setup, see tracking branches and upstream configuration. For integration policy, compare rebase and merge and consult the focused interactive rebase and merge strategy guides.

When to Use / When Not to Use

When to Use Fetch

Fetch is the safe way to see what the remote has without touching your working directory. You run it when you want information, not integration. The classic pattern: fetch, review with git log main..origin/main, then decide whether to merge or rebase. This two-step approach gives you control over when and how remote changes enter your branch.

Fetch also matters before rebasing. If you rebase onto stale remote state, you are rebuilding your feature on an outdated foundation — the resulting commits will need another rebase once you do fetch for real. Fetch first, look at what is new, then rebase knowing what you are getting into. In CI/CD pipelines, fetch is the right move when you need to inspect commits or check whether a build is necessary, without pulling anything into the working tree.

The --prune flag on fetch is worth setting globally. Remote branches get deleted; your local repo does not automatically forget them. Running git fetch --prune (or git fetch -p) removes references to deleted remote branches in one step. Set fetch.prune=true and you will stop seeing stale entries in git branch -r.

When to Use Pull

Pull is fetch plus integration in a single command — convenient when you already know you want the remote changes and do not need to review them first. In solo development or on short-lived feature branches where you are the only one touching the code, pull works fine. You make commits, you pull, you keep working.

git pull --rebase replays local commits on top of the fetched remote work, creating new commit IDs. It is common for private local work. For a published branch, check whether another person or system has based work on those commits and whether repository policy allows rewriting them. Coordinate first when others may depend on the branch. If a rewrite is permitted, fetch and review the remote state, then use git push --force-with-lease as a guard against replacing an unexpected remote tip. See the git pull and git rebase documentation.

For teams using trunk-based development with short-lived branches, pull is the default integration step. You finish your feature, push your branch, then pull from main into your branch before creating the PR — or pull after rebasing onto main, depending on your merge strategy. The key point: pull modifies your branch. If you want to review before integrating, use fetch first.

When to Use Push

Push is how you share commits. Until you push, your commits exist only on your machine — no collaborator, CI pipeline, or backup system can see them. Push sends your local branch state to its configured remote. The first time you push a new branch, use git push -u origin <branch> to set upstream tracking at the same time.

Whether a push starts CI depends on the repository’s hooks, webhooks, branch and tag filters, and provider settings. Some pipelines run on pushes; others run for pull requests, tags, schedules, or manual dispatch. Follow the checks required for the target branch. Teams may push work-in-progress to an isolated branch for review or backup, while protected branches can require passing checks before merge. See the git push documentation for what the command updates; CI behavior is configured separately.

One thing to know: git push without arguments uses your branch’s configured upstream. If your branch has no upstream set, Git needs you to be explicit about both the remote and branch name. New developers sometimes wonder why git push fails on a fresh branch — the answer is that Git does not know where to send the commits. Use -u on first push to avoid this.

When Not to Push

These situations catch developers off guard because there is no immediate feedback. The damage surfaces on the remote afterward, hitting teammates, CI pipelines, or production systems.

  • To a protected branch before required checks pass — follow the repository’s branch policy; an isolated draft branch may still be useful for backup or review
  • Using --force without checking the remote — you could move commits out of other contributors’ history
  • To protected branches — use pull requests instead
  • Force push without coordination — can destroy teammates’ work

For example, you rebase feature-x and plan to update it while a teammate is reviewing the branch. If they push new commits in the meantime, a plain force push can move the remote ref away from their work. Fetching first can reveal changes already present, but still coordinate and check the expected remote value when updating a shared branch.

Core Concepts

The three commands operate in different directions and with different safety profiles:

  • fetch — Remote → Local (safe, read-only to your branches)
  • pull — Remote → Local + Merge (modifies your branches)
  • push — Local → Remote (modifies the remote)

fetch:  Downloads remote changes to remote-tracking branches
        origin/main updated, your main unchanged

pull:   fetch + merge (or rebase)
        origin/main updated, your main updated too

push:   Uploads your commits to the remote
        origin/main updated with your commits

graph LR
    Local["Local Repository"] -->|push| Remote["Remote Repository"]
    Remote -->|fetch| Local
    Remote -->|pull = fetch + merge| Local

    Local -. "Your commits" .-> Push["git push origin main"]
    Remote -. "Their commits" .-> Fetch["git fetch origin"]
    Fetch -. "Then merge" .-> Pull["git pull origin main"]

Architecture or Flow Diagram


flowchart TD
    A["Working locally\nwith commits"] --> Decision{"Need to sync\nwith remote?"}
    Decision -->|Check remote changes| Fetch["git fetch origin"]
    Decision -->|Share your work| Push["git push origin main"]
    Decision -->|Get and integrate| Pull["git pull origin main"]

    Fetch --> Inspect["git log main..origin/main\nInspect changes"]
    Inspect --> Integrate{"How to integrate?"}
    Integrate -->|Merge| Merge["git merge origin/main"]
    Integrate -->|Rebase| Rebase["git rebase origin/main"]

    Push --> Check{"Remote has\nnew commits?"}
    Check -->|Yes| Reject["Push rejected\nFetch first!"]
    Check -->|No| Success["Push successful"]
    Reject --> Fetch

    Pull --> Conflict{"Conflict?"}
    Conflict -->|Yes| Resolve["Resolve conflicts"]
    Conflict -->|No| Done["Up to date"]
    Resolve --> Done

Step-by-Step Guide / Deep Dive

Git Fetch


# Fetch all remotes
git fetch --all

# Fetch a specific remote
git fetch origin

# Fetch a specific branch
git fetch origin main

# Fetch and prune deleted remote branches
git fetch --prune

# Fetch with verbose output
git fetch -v

# Fetch without updating remote-tracking branches (dry run)
git fetch --dry-run

Git Pull


# Pull (fetch + merge)
git pull origin main

# Pull with rebase instead of merge
git pull --rebase origin main

# Pull with automatic rebase (configured globally)
git config --global pull.rebase true
git pull origin main

# Pull without auto-merging (fetch only, review first)
git pull --no-commit origin main

# Pull a specific branch into current branch
git pull origin feature-x

Git Push


# Push current branch to its upstream
git push

# Push a specific branch
git push origin feature-x

# Push and set upstream tracking
git push -u origin feature-x

# Push all branches
git push --all origin

# Push all tags
git push --tags origin

# Push with lease (safer than force)
git push --force-with-lease origin feature-x

# Force push (dangerous)
git push --force origin feature-x

Pruning


# Prune deleted remote branches during fetch
git fetch --prune

# Prune without fetching
git remote prune origin

# Configure automatic pruning
git config --global fetch.prune true

# See what would be pruned
git remote prune origin --dry-run

Setting Upstream Tracking


# Set upstream for current branch
git push -u origin feature-x

# Set upstream without pushing
git branch --set-upstream-to=origin/main main

# View tracking configuration
git branch -vv

Production Failure Scenarios

Scenario Impact Mitigation
Force push to shared branch May move the ref away from commits teammates use Coordinate rewrites; follow branch policy and protect restricted branches
Pull without fetching first Unexpected merge conflicts Always fetch before pulling or rebasing
Pushing to wrong branch Code deployed from wrong version Verify branch name; use branch protection
Stale remote-tracking branches References to deleted branches Use --prune regularly
Push without local tests Code may reach the remote before checks run Use configured hooks and required CI checks

Pre-Push Hook Example


#!/bin/bash
# .git/hooks/pre-push
echo "Running tests before push..."
npm test
if [ $? -ne 0 ]; then
    echo "Tests failed. Push aborted."
    exit 1
fi

Trade-off Analysis

Approach Pros Cons
Fetch then merge Review remote changes first Two-step process
Pull (fetch + merge) One command, convenient Integrates changes immediately
Pull –rebase Linear local history Replays local commits with new IDs
Force push Replaces the remote ref Can move commits out of teammates’ history
Force-with-lease Checks an expected remote value Requires coordination; default ref can change after background fetch
Auto-prune Clean remote-tracking list May lose references you wanted

Implementation Snippets


# Safe daily sync workflow
git fetch origin --prune
git log main..origin/main  # review what's new
git switch main
git rebase origin/main     # or merge
git push

# Push new feature branch
git switch -c feature/auth
# ... work ...
git push -u origin feature/auth

# Force push safely after rebase
git rebase origin/main
git push --force-with-lease origin feature/auth

# Push everything
git push --all origin
git push --tags origin

Observability Checklist

  • Logs: Record push operations with commit SHAs in CI logs
  • Metrics: Track push frequency and rejection rate
  • Alerts: Alert on force pushes to protected branches
  • Traces: Link pushes to CI/CD pipeline triggers
  • Dashboards: Display branch synchronization status

Security & Compliance Considerations

  • Force push should be blocked on protected branches
  • Use signed commits and signed pushes for supply chain security
  • Audit push history for compliance requirements
  • Pre-push hooks can enforce security checks before code leaves your machine
  • Verify remote URLs before pushing to prevent credential leakage

Common Pitfalls / Anti-Patterns

  • Confusing fetch and pull — fetch is safe; pull modifies your working state
  • Force pushing without checking the remote state — can move commits out of teammates’ history
  • Not pruning — stale remote-tracking branches accumulate and confuse
  • Pushing without pulling — results in rejected pushes and extra merge commits
  • Ignoring upstream tracking — git push without upstream configuration is ambiguous
  • Pushing WIP commits — use --no-verify sparingly; clean your history first

Quick Recap Checklist

  • git fetch downloads changes safely without modifying your branches
  • git pull = fetch + merge (or rebase with --rebase)
  • git push uploads your commits to the remote
  • Use --prune to clean up deleted remote branches
  • Coordinate any permitted rewrite and check the expected remote value before force-pushing
  • Set upstream tracking with -u on first push
  • Fetch before pushing to review remote changes; coordinate and check the expected remote value before a shared-branch rewrite
  • Run tests locally and use configured pre-push hooks or required CI checks

Architecture: Data Flow for Fetch, Pull, Push


graph LR
    subgraph "Local Repository"
        Working["Working Directory"]
        LocalBranch["Local Branch\n(main)"]
        RemoteTracking["Remote-Tracking\n(origin/main)"]
    end

    subgraph "Remote Server"
        RemoteBranch["Remote Branch\n(main)"]
    end

    RemoteBranch -. "git fetch" .-> RemoteTracking
    RemoteTracking -. "git merge" .-> LocalBranch
    RemoteBranch -. "git pull\n(fetch + merge)" .-> LocalBranch
    LocalBranch -. "git push" .-> RemoteBranch

    classDef local color:#00fff9
    class Working,LocalBranch,RemoteTracking,RemoteBranch local

Key distinction: fetch only updates remote-tracking references (origin/main). It never touches your local branches or working directory. pull does fetch + merge (or rebase), modifying your local branch. push uploads your local commits to the remote.

Production Failure: Force Push Overwriting Team Work

Scenario: A developer rebases their feature branch locally and runs git push --force origin feature-x. Unbeknownst to them, a teammate had also pushed two commits to the same branch. The force push silently overwrites the teammate’s commits, destroying their work.

Impact: Lost commits, broken teammate trust, hours of recovery via reflog, and potential production issues if the lost commits contained critical fixes.

Mitigation:

  • Avoid --force for shared branches; follow the repository’s rewrite policy
  • Block force pushes on protected branches through platform settings
  • Communicate and agree before rewriting a shared branch
  • If policy permits the rewrite, use --force-with-lease as an additional check on the expected remote value; background fetches can update the default tracking ref

# Coordinate first; the lease checks an expected remote value
git push --force-with-lease origin feature-x

# By default, the expected value comes from a local remote-tracking ref.
# Background fetches can update that ref, so an explicit expected SHA
# provides a clearer check. A lease does not replace team agreement.

# Configure alias for safety
git config --global alias.push-force 'push --force-with-lease'

# Push rejection: non-fast-forward
# If you get this error:
# ! [rejected] main -> main (non-fast-forward)
# Fix: fetch first, then integrate
git fetch origin
git rebase origin/main  # or git merge origin/main
git push origin main

Trade-offs: Pull Strategies

Approach How It Works History Conflict Frequency Best For
git pull --merge (default) Fetch + merge commit Non-linear, shows integration point Lower — conflicts resolved once Teams that prefer merge workflows
git pull --rebase Fetch + rebase local commits Linear, clean Higher — conflicts per commit Solo developers, clean history preference
git pull --ff-only Fetch + fast-forward only Linear N/A — fails if diverged Trunk-based development, CI pipelines
git fetch + manual review Download only, no integration Unchanged None — review before integrating Cautious workflows, complex integrations

Security/Compliance: Push Protection


# Platform-level push protection (GitHub)
# Branch protection rules:
# - Require pull request reviews before merging
# - Require status checks to pass before merging
# - Include administrators (no bypassing)
# - Restrict who can push to matching branches

# GitLab equivalent:
# - Protected branches: Developers can merge, Maintainers can push
# - Merge request approvals required
# - Pipeline must succeed

# Pre-push hook for local enforcement
#!/bin/bash
# .git/hooks/pre-push
protected_branches=("main" "develop" "release/*")
current_branch=$(git symbolic-ref --short HEAD)

for branch in "${protected_branches[@]}"; do
    if [[ "$current_branch" == $branch ]]; then
        echo "ERROR: Direct push to $branch is not allowed."
        echo "Please create a feature branch and submit a PR."
        exit 1
    fi
done

# Run tests before push
npm test || { echo "Tests failed. Push aborted."; exit 1; }

Compliance notes:

  • Push protection rules should be enforced at the platform level, not just locally
  • Audit push history regularly for unauthorized direct pushes to protected branches
  • Document push policies in your security policy
  • Use signed pushes for supply chain security (git push --signed)

Best Practices for Daily Git Synchronization

Daily Sync Workflow

Follow this sequence for safe synchronization:

  1. Start your session with fetch — git fetch --prune updates all remote-tracking branches without touching your local work
  2. Review what’s new — git log main..origin/main shows commits waiting to be pulled
  3. Integrate deliberately — merge keeps existing commit IDs; rebase replays commits with new IDs. Follow team policy and coordinate before rewriting commits others may use.
  4. Check before sharing — run local tests and use any configured pre-push hooks
  5. Check CI when configured — confirm required pipeline checks passed for the target branch

Branch Naming Conventions

Clear branch names make sync operations safer:

  • feature/<ticket-id>-description — feature work
  • fix/<ticket-id>-description — bug fixes
  • hotfix/<description> — emergency production fixes
  • release/<version> — release preparation

Avoid generic names like dev, test, or mydemo — they increase the risk of pushing to the wrong branch.

Remote Management

Syncing Multiple Remotes

In monorepos or fork workflows, you may have multiple remotes:


# List all remotes
git remote -v

# Fetch from all remotes
git fetch --all

# Fetch from a specific remote
git fetch upstream

# Push to a specific remote
git push upstream feature-x

Handling Stale Remote Branches

Stale references accumulate over time. Clean them regularly:


# See what will be pruned (dry run)
git remote prune origin --dry-run

# Prune all remotes
git remote prune --all

# Remove a specific stale reference
git branch -r -d origin/deleted-branch

# Verify cleanup
git branch -r | sort

Git Configuration for Safety

Push Safety Checklist

Before running git push, verify:

  • git status shows only intended changes
  • Tests pass locally (npm test or equivalent)
  • git log origin/main..main shows only commits you intend to share
  • Branch name matches the target (check with git branch -vv)
  • No sensitive data or credentials in committed files

Configuring Git Defaults

Set sensible defaults in your global config:


# Always prune on fetch
git config --global fetch.prune true

# Set default push behavior (simple = current branch to matching upstream)
git config --global push.default simple

# Rebase by default on pull (cleaner history)
git config --global pull.rebase true

# Auto-follow tags
git config --global push.followTags true

# Alias for safe force push
git config --global alias.pf 'push --force-with-lease'

Interview Questions

1. What is the difference between git fetch and git pull?

git fetch downloads remote changes and updates remote-tracking branches (like origin/main) but does not modify your local branches or working files. git pull does a fetch plus a merge (or rebase), immediately integrating remote changes into your current branch.

2. What does --force-with-lease check before updating a remote branch?

--force-with-lease asks Git to update a remote ref only if it matches the expected value, often the local remote-tracking ref. If the remote value differs, the push is rejected. A background fetch can update that tracking ref, so specify an expected SHA when you need a clearer check. Coordinate any rewrite with people using the branch; a lease does not replace agreement.

3. What does git fetch --prune do?

It removes local references to remote branches that have been deleted on the server. If someone deletes origin/feature-x on GitHub, your local repo still tracks it. --prune cleans up these stale references so git branch -r shows only branches that actually exist on the remote.

4. What happens when you run git push without specifying a remote or branch?

Git pushes the current branch to its configured upstream (set with -u on first push). If no upstream is set, the behavior depends on push.default configuration — typically it pushes to a branch of the same name on origin, or fails if ambiguous.

5. What is the difference between git pull and git pull --rebase?

git pull (default) fetches remote changes and creates a merge commit that combines your local commits with the remote's commits. git pull --rebase fetches and then replays your local commits on top of the remote's commits, creating a linear history without merge commits. Rebase rewrites commit history, so use it only on private branches.

6. How do remote-tracking branches differ from local branches?

Remote-tracking branches (like origin/main) are local copies of the remote's state — they are updated only during fetch operations and never modified by your commits. Local branches (like main) are your actual working branches where you make commits. Remote-tracking branches serve as a reference point to see how far the remote has diverged from your local work.

7. When should you use git fetch --all instead of git fetch?

Use git fetch --all when you have multiple remotes configured and want to update all of them in one command. Regular git fetch only fetches from the default remote (typically origin). --all is useful in monorepos or when working with a fork upstream pattern.

8. What causes a push rejection and how do you resolve it?

A push is rejected when your local branch diverged from the remote — the remote has commits that your local branch does not have (non-fast-forward error). To resolve: run git fetch to see the remote state, then either git merge origin/main (creates a merge commit) or git rebase origin/main (linear history), then push again.

9. What is the purpose of push.default and what are its values?

push.default determines what happens when you run git push without specifying a remote and branch. Values include: simple (push current branch to its upstream, requires matching names), current (push current branch to a branch of the same name on the remote), upstream (push to the configured upstream), and matching (push all branches that have the same name on the remote).

10. How does git push --tags work and when should you use it?

git push --tags uploads all your local annotated tags to the remote. Lightweight tags are not included by default. Use it after releasing a version (e.g., v1.0.0) to share version tags with teammates. Note: --tags does not push regular commits — only the tags.

11. How do you recover from an accidental force push?

Recovery steps: (1) Use git reflog to find the commit hash before the force push. (2) Create a backup branch from the reflog: git branch backup main@{1}. (3) Reset your local branch: git reset --hard backup. (4) Push the recovered branch: git push --force-with-lease origin main. (5) Notify teammates — they will need to re-sync their local branches.

12. What happens to your working directory during git pull?

git pull can modify your working directory if there are unmerged changes that conflict with incoming changes. If your working directory is clean, the merge/rebase proceeds without modifying untracked files. If you have local modifications on the current branch, Git may block the pull or create a merge conflict that requires manual resolution.

13. How does git remote prune origin differ from git fetch --prune?

git fetch --prune fetches from the remote and then removes stale remote-tracking branch references in one step. git remote prune origin only removes stale remote-tracking branches without fetching — useful for cleaning up without checking for new commits. Use git remote prune --dry-run origin to preview what will be deleted.

14. How does git push -u set upstream tracking?

The -u (or --set-upstream) flag sets the remote branch as the upstream for your current local branch. After running git push -u origin feature-x, Git records that feature-x should track origin/feature-x. Subsequent git push or git pull commands on that branch work without specifying the remote and branch.

15. What are the risks of force pushing to a shared branch?

A force push can move the remote ref away from commits teammates use, leaving their local branches divergent. It may also affect links from builds or reviews to old commit IDs, depending on repository automation and retention. Before a permitted rewrite, agree with branch users and use --force-with-lease with a known expected value as an additional check.

16. When would you use git pull --ff-only?

Use git pull --ff-only when you want to update only if the remote is a direct ancestor of your local branch — no merge commits or rebasing. If the branches have diverged, the command fails with an error. This is ideal for CI/CD pipelines and trunk-based development where you want to ensure the build is always based on a clean, linear history.

17. What does git fetch --dry-run show you?

git fetch --dry-run simulates a fetch without actually downloading any data — it shows which remote-tracking branches would be updated and which would be pruned. Useful for CI/CD scripts to check if there are new commits before running expensive operations, or to inspect what would change without making any modifications.

18. Why is verifying remote URLs important before pushing?

Incorrect remote URLs can send your code to the wrong repository — exposing proprietary code or credentials to unintended parties. Before pushing, verify with git remote -v that the URL matches your expected repository. Also check for git hooks that might redirect pushes to a different remote.

19. How do you view the difference between your local branch and the remote?

Use git log origin/main..main to see commits on your local branch not on the remote (what you would push). Use git log main..origin/main to see commits on the remote not on your local branch (what you would pull). For detailed diffs, use git diff origin/main main to see actual line-by-line differences.

20. What is the relationship between origin/main and the remote's main branch?

origin/main is a local remote-tracking branch — a snapshot of the remote's main branch as of your last git fetch. It is not a live connection; it only updates when you fetch. Your local main branch is independent and diverges as you make commits. The remote's actual main only changes when someone pushes to it.

Further Reading

Conclusion

Fetch updates remote-tracking refs without changing your local branches or working tree. Pull fetches and then merges or rebases into the current branch; push updates a remote ref. Check the configured repository policy before integrating or rewriting shared work.

Category

Related Posts

Centralized vs Distributed VCS: Team Trade-offs

Compare centralized and distributed version control by offline work, repository size, access control, branching, review, and migration effort.

#git #version-control #svn

Automated Changelog Generation

Generate release notes from structured commits, choose a release policy, and add changelog generation to CI without treating every merge as a release.

#git #version-control #changelog

Choosing a Git Team Workflow

Compare trunk-based development, short-lived feature branches, and release branches against your release, review, rollback, and compliance needs.

#git #version-control #branching-strategy