Skip to main content

Synchronizing with Remote

  • git fetch → Download objects & refs from remote
  • git pull → Fetch & merge from remote
  • git push origin <branch> → Push branch to remote
  • git push -u origin <branch> → Push branch & set upstream
  • git push → Push committed changes

git fetch

git fetch [<options>] [<repository> [<refspec>...]]
# Fetch from default remote (origin):
git fetch

# Fetch from a specific remote:
git fetch upstream

# Fetch all remotes:
git fetch --all

# Fetch a specific branch:
git fetch origin main

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

# Fetch tags only:
git fetch --tags
  • Description: The git fetch command is a safe way to synchronize your local repository with remote updates.
  • Usage: It updates remote-tracking branches (e.g., origin/main), downloads commits, files, and tags from the remote, does not change your local working branch or files, lets you inspect remote changes before merging or rebasing.
  • Think Tank: Fetch works with below options:
Option Description
--all Fetch from all remotes
--prune Remove remote-tracking branches that no longer exist on remote
--tags Fetch all tags from remote
--no-tags Do not fetch tags
--dry-run Show what would be fetched without actually fetching
--depth  Perform a shallow fetch with only the last n commits
--force Force update of local refs even if it discards commits
--multiple Fetch from multiple remotes at once
  • Best Practices:
    • Run git fetch regularly to stay updated with remote changes
    • Use --prune to clean up stale branches
    • Inspect changes with git log origin/main before merging
    • Prefer git fetch over git pull when you want control over integration
    • Use shallow fetch (--depth) in CI/CD to save time and bandwidth

How git fetch works

  • Remote-tracking branches: After fetching, you’ll see updates in branches like origin/main
  • Local branch untouched: Your current branch remains unchanged until you explicitly merge or rebase
  • Tags: By default, tags pointing into fetched histories are also retrieved. Options like --tags or --no-tags control this
Git - git-fetch Documentation

git pull

git pull [options] [<repository> [<refspec>...]]
# Update current branch from default remote (origin):
git pull

# Pull from a specific remote and branch:
git pull origin main

# Pull with rebase instead of merge:
git pull --rebase

# Pull all remotes:
git pull --all

# Abort a pull in progress (during conflicts):
git merge --abort
  • Description: Fetches changes from a remote repository and immediately integrates them into your current branch.
  • Usage: Its equivalent to running git fetch followed by git merge (or git rebase if configured), updates your local branch with commits from its remote-tracking branch, keeps your local repository in sync with the remote.
  • Think Tank: Equivalent to git fetch + git merge
Option Description
--rebase Rebase local commits on top of fetched commits instead of merging
--no-rebase Explicitly disable rebase (use merge)
--ff-only Only allow fast-forward merges; abort if merge commit would be required
--all Pull from all remotes
--tags Fetch all tags from remote
--prune Remove remote-tracking branches that no longer exist on remote
--depth  Perform a shallow pull with only the last n commits
--verbose Show detailed output of the pull process
  • Best Practices:
    • Use git fetch + git log to inspect changes before pulling
    • Configure git pull --rebase for cleaner, linear history (if your team agrees)
    • Use --ff-only to avoid unnecessary merge commits
    • Always resolve conflicts carefully and test after pulling
    • Coordinate with your team on pull strategies (merge vs. rebase)

How it works

  • Step 1: Fetch: Downloads new commits, branches, and tags from the remote.
  • Step 2: Merge/Rebase: Integrates those commits into your current branch.
  • Default behavior: Merge (creates a merge commit if histories diverge).
  • Alternative behavior: Rebase (if configured with git config pull.rebase true or using --rebase).
Git - git-pull Documentation

git push

git push [options] [<repository> [<refspec>...]]
# Push current branch to default remote (origin):
git push

# Push specific branch:
git push origin main

# Set upstream branch (first push):
git push -u origin feature-login

# Delete a remote branch:
git push origin --delete old-branch

# Push tags:
git push origin --tags

# Force push (overwrite remote history):
git push --force origin main

# Force push but safer (only if remote can be fast-forwarded):
git push --force-with-lease
  • Description: The git push command is how you publish your local work to a remote repository.
  • Usage: It updates remote branches and tags, establishes tracking relationships, and enables collaboration.
  • Think Tank: Use -u to set upstream tracking
Option Description
-u, --set-upstream Set upstream branch for tracking (used on first push)
--all Push all branches
--tags Push all tags
--delete  Delete a branch on the remote
--force Force push, overwriting remote history
--force-with-lease Safer force push; only overwrites if remote hasn’t changed unexpectedly
--dry-run Show what would be pushed without actually pushing
--mirror Push all refs (branches, tags, remotes) for exact mirroring
--atomic Ensure all refs are pushed together or none at all
--verbose Show detailed output of push process
  • Best Practices:
    • Use git push -u when pushing a branch for the first time
    • Avoid --force; prefer --force-with-lease if rewriting history is necessary
    • Push only relevant branches; don’t clutter remotes with experimental work
    • Always fetch and rebase/merge before pushing to avoid conflicts
    • Coordinate with your team before deleting or force-pushing shared branches

How it works

  • Local → Remote: Push takes commits reachable from your local branch and sends them to the remote.
  • Tracking branches: If your local branch is set to track a remote branch, git push knows where to send changes.
  • Fast-forward requirement: By default, pushes succeed only if the remote branch can be fast-forwarded (no conflicting history).
  • Force push: Overrides remote history with your local branch (dangerous if others rely on that branch).
Git - git-push Documentation

Tips, Tricks, Roadmaps, Resources, Networking, Motivation, Guidance, and Cool Stuff ♥

Updated on Dec 23, 2025