Guides

Git reset local branch to remote: git fetch, reset --hard and how to undo it

Reset a local Git branch to match the remote: git fetch and git reset --hard origin/main, clean untracked files, keep a backup and undo with reflog.

Published ~7 min read
On this page

Short answer: run git fetch origin, then git reset --hard origin/main (use your branch name). Your local branch, staging area and tracked files then match the remote exactly; add git clean -fd to delete untracked files as well. Local commits and uncommitted edits are discarded, so save anything you need first.

Git · any OS
git fetch origin
git branch backup-main            # optional safety net: keeps your local commits
git reset --hard origin/main      # or: git reset --hard @{u}
git clean -fd                     # optional: delete untracked files and folders

Every command and output below was run with Git 2.39.5 (Debian 12) on a throwaway pair: a bare repository as the “remote” and two clones, one of which pushed two commits while the other made a local commit and left edits lying around. Commit hashes are from that run; long scratch paths are shortened.

Cheat sheet: reset a branch to the remote

GoalCommandWhat it discards
Match the remote exactlygit fetch origin && git reset --hard origin/mainLocal commits, staged and unstaged changes to tracked files
Same, for whatever branch you are ongit reset --hard @{u}The same; needs an upstream branch
Also delete untracked filesgit clean -fdNew files and folders Git does not track
Also delete ignored filesgit clean -fdxIgnored files too: .env, node_modules, build output
Reset a branch you are not ongit branch -f dev origin/devLocal commits on that branch
Reset one file onlygit restore --source=origin/main app.confYour changes to that file
Keep local commits, add remote onesgit pull --rebaseNothing; may stop on conflicts
Undo a resetgit reset --hard HEAD@{1}Restores the commit you were on, not uncommitted edits

See what a reset would throw away

A reset only knows the remote as of your last fetch: origin/main is a local copy that git fetch updates. Fetch first, then compare:

Output · after git fetch origin
$ git status -sb
## main...origin/main [ahead 1, behind 2]
 M README.md
?? notes.txt
$ git log --oneline origin/main..main      # only local: will be dropped
87eeafa Tune workers on server
$ git log --oneline main..origin/main      # only on the remote: will arrive
cf86bff Document start script
bc8cf68 Move app to port 9090

Read it like this: one local commit and the edit to README.md will be lost, notes.txt is untracked and survives a reset, and two commits come in from the remote. git diff --stat sums up the uncommitted edits. If the first git log prints nothing and git status is clean, you lose nothing and git pull would do the same job.

Save your work before you reset

Two commands cost nothing and make the reset reversible:

Output
$ git branch backup-main
$ git stash push -u -m "before reset"
Saved working directory and index state On main: before reset
$ git stash show -u --stat
 README.md | 1 +
 notes.txt | 1 +
 2 files changed, 2 insertions(+)
  • git branch backup-main keeps a name on your local commits. Delete it with git branch -D backup-main once you are sure.
  • git stash push -u stores uncommitted edits and untracked files. Without -u, notes.txt would stay behind. Ignored files such as .env are not stashed (that needs -a), which you can see in the clean section.
  • Later, git stash pop puts the edits back. In one of our runs the same lines had changed upstream, so it stopped with CONFLICT (content): Merge conflict in README.md and kept the stash entry, as Git says, “in case you need it again”.

git reset --hard origin/main and @{u}

Output
$ git reset --hard @{u}
HEAD is now at cf86bff Document start script
$ git status -sb
## main...origin/main

@{u} (short for @{upstream}) means the remote branch this branch tracks, so the same line works on main, master or develop without typing the name. On a branch that was never pushed or set up to track, it fails with fatal: no upstream configured for branch 'feature'; name the remote branch instead, or set one with git branch -u origin/feature. Branch setup and upstreams are covered in how to create a branch in Git.

To reset a branch you have not checked out, move it without switching: git branch -f dev origin/dev. Git refuses that for the current branch with fatal: cannot force update the branch 'main', followed by the folder where it is checked out, and for that branch reset is the tool. If the remote branch was renamed, fetch with --prune first; renaming a Git branch shows how other clones follow a rename.

Delete untracked files: git clean -fd and -x

reset --hard leaves untracked files alone. git clean removes them, and -n shows what it would do without deleting:

Output
$ git clean -n -d
Would remove notes.txt
Would remove tmpdir/
$ git clean -fd
Removing notes.txt
Removing tmpdir/
$ git clean -n -d -x
Would remove .env
Would remove app.log

-f is required to delete anything, -d includes untracked folders, and -x also deletes files matched by .gitignore. In this repository those were .env and app.log: on a server that is the configuration and the data you most likely want to keep, so run -x only after a dry run. To remove only ignored files, such as build output, use -X.

Reset a single file to the remote version

Output
$ git restore --source=origin/main app.conf
$ cat app.conf
port=9090

git restore exists since Git 2.23. On older versions, or from habit, git checkout origin/main app.conf does the same and also stages the file, while git restore without --staged changes only the working tree. Only that path changes; the branch and every other file stay as they are.

When git pull --rebase is the better choice

Reset is for throwing local work away. If your local commits should stay on top of the remote ones, rebase instead. A plain git pull on diverged branches with no pull.rebase setting stops and asks:

Output · git pull, branches diverged (abridged)
hint: You have divergent branches and need to specify how to reconcile them.
hint: You can do so by running one of the following commands sometime before
hint: your next pull:
hint:
hint:   git config pull.rebase false  # merge
hint:   git config pull.rebase true   # rebase
hint:   git config pull.ff only       # fast-forward only
...
fatal: Need to specify how to reconcile divergent branches.

git pull --rebase replays your commits after the remote ones. In our test both sides had changed app.conf, so it stopped with CONFLICT (content): Merge conflict in app.conf. git rebase --abort went straight back to the state before the pull. On a server checkout that only ever receives code, git config pull.ff only makes git pull refuse anything but a clean fast forward, so local edits never get merged in by accident.

Undo a hard reset with git reflog

Output · git reflog -4
cf86bff HEAD@{0}: reset: moving to @{u}
87eeafa HEAD@{1}: reset: moving to HEAD
87eeafa HEAD@{2}: commit: Tune workers on server
9293041 HEAD@{3}: clone: from .../app.git

The reflog lists where HEAD has been. The second line comes from git stash, which resets to HEAD internally. To go back, reset to the entry before the reset, or to the backup branch:

Output
$ git reset --hard backup-main      # or: git reset --hard HEAD@{1}
HEAD is now at 87eeafa Tune workers on server

That restores commits only. Uncommitted edits that were never stashed and files deleted by git clean are not in the reflog and are gone, which is why the backup step comes first.

Resetting a deploy checkout on a server

On a VPS that deploys from Git, a checkout drifts when someone edits a file in place. A deploy script that always lands on the remote state looks like this:

deploy.sh
#!/bin/bash
set -e
cd /var/www/app
git fetch --prune origin
git reset --hard origin/main
git clean -fd          # no -x: keeps .env, uploads and other ignored files

If git fetch fails there with Permission denied (publickey), the deploy key is the problem, not the reset; see Permission denied (publickey). If git itself is missing on a fresh server, git: command not found has the install line.

Frequently asked

How do I reset my local branch to match the remote?

Run git fetch origin, then git reset --hard origin/main with your branch name, or git reset --hard @{u} for the upstream of the current branch. Your branch, index and tracked files then match the remote. Untracked files stay until you also run git clean -fd.

Does git reset --hard delete untracked files?

No. It resets tracked files, the index and the branch, but files Git does not track stay where they are. git clean -n -d lists them, git clean -fd deletes them, and git clean -fdx also deletes ignored files such as .env, so dry-run that one first.

Can I undo git reset --hard?

Yes, for commits. git reflog lists where HEAD has been, and git reset --hard HEAD@{1} goes back to the commit before the reset. Uncommitted edits that were not stashed and files removed by git clean are not in the reflog and cannot be recovered that way.

Should I use git pull --rebase or git reset --hard?

Use git reset --hard when your local commits and edits should be thrown away and the branch must equal the remote. Use git pull --rebase when your local commits should be kept and replayed on top of the remote ones; it may stop on conflicts, and git rebase --abort returns to where you started.

Related articles

All guides