Make a local Git branch match the remote

Fetch and reset deliberately when remote contents should replace local work, preserve commits and edits first, and preview optional untracked-file cleanup.

Best practices
Illustration of Git branches and version history

To make the current local branch point to a remote branch and replace its tracked files, fetch and then reset to the remote-tracking ref. git pull --force is not a general command for discarding local edits.

This operation can discard staged and unstaged work and move the branch away from local commits. Check the current branch and preserve anything you want before resetting.

Save the state you may need

git status
git branch --show-current
git branch backup-before-reset
git stash push -u -m "Local work before remote reset"

Use an unused backup branch name. The backup branch retains committed history; it does not save uncommitted edits. The stash saves tracked changes and, with -u, ordinary untracked files. Ignored files need a separate backup or an intentional -a stash. Nested repositories and submodules require their own review.

Fetch, inspect, then reset

For a checked-out local branch that should match origin/main:

git fetch origin
git log --oneline --left-right HEAD...origin/main
git reset --hard origin/main

Only proceed after the fetch succeeds and the target is correct. The hard reset updates the current branch, index, and tracked working-tree contents. It can also remove untracked paths that obstruct writing tracked files, but it does not generally remove every unrelated untracked or ignored file.

If you also want to remove ordinary untracked files and directories:

git clean -nd
# After reviewing the list:
git clean -fd

Ignored files remain unless you request broader cleanup such as -x, which can delete local configuration and build dependencies.

Discard edits while keeping local commits

Reset to the current commit, rather than the remote ref:

git reset --hard HEAD
# Then integrate remote updates using the project's chosen workflow.
git pull --ff-only

Run this only after preserving edits you need. A fast-forward-only pull refuses diverged history; it does not discard local commits to force alignment. Pulling first can fail because the very edits you intend to discard block the update.

FAQ

Can I recover local commits after a reset?

The backup branch is the most direct route. Otherwise inspect git reflog and create a branch at the old commit while it still exists. Reflog retention is configurable, and object pruning affects recovery; there is no guaranteed 30-day recovery window. Unrecorded working-tree edits are a different case.

Can I replace just one file?

After fetching, use git restore --source=origin/main -- path/to/file. This changes the working-tree file without moving your branch or staging the result. The older checkout form also stages it.

Is this suitable for deployment?

Some deployments reset a dedicated checkout, but the process needs an explicit revision and handling for local files. Building and deploying a versioned artifact usually makes the deployed contents easier to reproduce. --ff-only is a pull integration rule, not a general guard against a server's files being edited.

See git-reset, git-clean, git-pull, and git-reflog.

Get a tailored plan

Contact us

Join the community

Scan with WeChat
to join the community

Try Guance

Start online and pay only for what you use.

Get started

Choose a Guance plan

Code hosting