Synchronizing with Remote
git fetch→ Download objects & refs from remotegit pull→ Fetch & merge from remotegit push origin <branch>→ Push branch to remotegit push -u origin <branch>→ Push branch & set upstreamgit 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 fetchcommand 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 fetchregularly to stay updated with remote changes - Use
--pruneto clean up stale branches - Inspect changes with
git log origin/mainbefore merging - Prefer
git fetchovergit pullwhen you want control over integration - Use shallow fetch (
--depth) in CI/CD to save time and bandwidth
- Run
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
--tagsor--no-tagscontrol 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 fetchfollowed bygit merge(orgit rebaseif 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 logto inspect changes before pulling - Configure
git pull --rebasefor cleaner, linear history (if your team agrees) - Use
--ff-onlyto avoid unnecessary merge commits - Always resolve conflicts carefully and test after pulling
- Coordinate with your team on pull strategies (merge vs. rebase)
- Use
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 trueor 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 pushcommand 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
-uto 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 -uwhen pushing a branch for the first time - Avoid
--force; prefer--force-with-leaseif 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
- Use
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 pushknows 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


Updated on Dec 23, 2025