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 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 foldersEvery 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
| Goal | Command | What it discards |
|---|---|---|
| Match the remote exactly | git fetch origin && git reset --hard origin/main | Local commits, staged and unstaged changes to tracked files |
| Same, for whatever branch you are on | git reset --hard @{u} | The same; needs an upstream branch |
| Also delete untracked files | git clean -fd | New files and folders Git does not track |
| Also delete ignored files | git clean -fdx | Ignored files too: .env, node_modules, build output |
| Reset a branch you are not on | git branch -f dev origin/dev | Local commits on that branch |
| Reset one file only | git restore --source=origin/main app.conf | Your changes to that file |
| Keep local commits, add remote ones | git pull --rebase | Nothing; may stop on conflicts |
| Undo a reset | git 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:
$ 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 9090Read 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:
$ 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-mainkeeps a name on your local commits. Delete it withgit branch -D backup-mainonce you are sure.git stash push -ustores uncommitted edits and untracked files. Without-u,notes.txtwould stay behind. Ignored files such as.envare not stashed (that needs-a), which you can see in the clean section.- Later,
git stash popputs the edits back. In one of our runs the same lines had changed upstream, so it stopped withCONFLICT (content): Merge conflict in README.mdand kept the stash entry, as Git says, “in case you need it again”.
git reset --hard origin/main and @{u}
$ 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:
$ 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
$ git restore --source=origin/main app.conf
$ cat app.conf
port=9090git 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:
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
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.gitThe 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:
$ git reset --hard backup-main # or: git reset --hard HEAD@{1}
HEAD is now at 87eeafa Tune workers on serverThat 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:
#!/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 filesIf 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.