Setting your branch to exactly match the remote branch can be done in two steps:

git fetch origin
git reset --hard origin/master

If you want to save your current branch's state before doing this (just in case), you can do:

git commit -a -m "Saving my work, just in case"
git branch my-saved-work

Now your work is saved on the branch "my-saved-work" in case you decide you want it back (or want to look at it later or diff it against your updated branch).

Note: the first example assumes that the remote repo's name is origin and that the branch named master in the remote repo matches the currently checked-out branch in your local repo, since that is in line with the example given in the question. If you are trying to reset to the default branch in a more recent repository, it is likely that it will be main.

BTW, this situation that you're in looks an awful lot like a common case where a push has been done into the currently checked out branch of a non-bare repository. Did you recently push into your local repo? If not, then no worries -- something else must have caused these files to unexpectedly end up modified. Otherwise, you should be aware that it's not recommended to push into a non-bare repository (and not into the currently checked-out branch, in particular).

Answer from Dan Moulding on Stack Overflow
Top answer
1 of 16
10340

Setting your branch to exactly match the remote branch can be done in two steps:

git fetch origin
git reset --hard origin/master

If you want to save your current branch's state before doing this (just in case), you can do:

git commit -a -m "Saving my work, just in case"
git branch my-saved-work

Now your work is saved on the branch "my-saved-work" in case you decide you want it back (or want to look at it later or diff it against your updated branch).

Note: the first example assumes that the remote repo's name is origin and that the branch named master in the remote repo matches the currently checked-out branch in your local repo, since that is in line with the example given in the question. If you are trying to reset to the default branch in a more recent repository, it is likely that it will be main.

BTW, this situation that you're in looks an awful lot like a common case where a push has been done into the currently checked out branch of a non-bare repository. Did you recently push into your local repo? If not, then no worries -- something else must have caused these files to unexpectedly end up modified. Otherwise, you should be aware that it's not recommended to push into a non-bare repository (and not into the currently checked-out branch, in particular).

2 of 16
748

First, use git reset to reset to the previously fetched HEAD of the corresponding upstream branch:

git reset --hard @{u}

The advantage of specifying @{u} or its verbose form @{upstream} is that the name of the remote repo and branch don't have to be explicitly specified. On Windows or with PowerShell, specify "@{u}" (with double quotes).

Next, as needed, use git clean to remove untracked files, optionally also with -x:

git clean -df

Finally, as needed, get the latest changes:

git pull
🌐
freeCodeCamp
freecodecamp.org › news › git-reset-origin-how-to-reset-a-local-branch-to-remote-tracking-branch
Git Reset Origin – How to Reset a Local Branch to Remote Tracking Branch
June 22, 2022 - Typically, there will be a local remote-tracking branch with the same name as the remote one that you want to reset to, such as main. Use the following command to checkout the local remote main branch: ... If you are using a different name for this branch, replace main with the name you are using. To fetch the remote repository, and the latest state and version of the code in the remote repository, enter the following command: ... origin is an alias created by Git and specifies the remote URL of the remote repository.
Discussions

How do I change the URI (URL) for a remote Git repository? - Stack Overflow
I have a repo (origin) on a USB key that I cloned on my hard drive (local). I moved "origin" to a NAS and successfully tested cloning it from here. I would like to know if I can change th... More on stackoverflow.com
🌐 stackoverflow.com
git - Resetting remote to a certain commit - Stack Overflow
Assuming that your branch is called master both here and remotely, and that your remote is called origin you could do: git reset --hard git push -f origin master More on stackoverflow.com
🌐 stackoverflow.com
[Git] How to 'git reset' a remote branch?
On July 1st, a change to Reddit's API pricing will come into effect. Several developers of commercial third-party apps have announced that this change will compel them to shut down their apps. At least one accessibility-focused non-commercial third party app will continue to be available free of charge. If you want to express your strong disagreement with the API pricing change or with Reddit's response to the backlash, you may want to consider the following options: Limiting your involvement with Reddit, or Temporarily refraining from using Reddit Cancelling your subscription of Reddit Premium as a way to voice your protest. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/learnprogramming
13
5
February 12, 2024
Need to reset git branch to origin version - Stack Overflow
C:\Users\vonc\git\git>git switch -C master origin/master Reset branch 'master' Branch 'master' set up to track remote branch 'master' from 'origin'. Your branch is up to date with 'origin/master'. More on stackoverflow.com
🌐 stackoverflow.com
🌐
Koda School
kodaschool.com › blog › changing-the-remote-origin-in-git
Changing the Remote Origin in Git
Navigate to the directory of your local Git repository using the cd command. ... Before you change the remote origin, check the existing remote setup with git remote -v. This command lists the remote connections you have to other repositories.
🌐
freeCodeCamp
freecodecamp.org › news › git-reset-to-remote-head-how-to-reset-a-remote-branch-to-origin
Git Reset to Remote Head – How to Reset a Remote Branch to Origin
September 10, 2024 - git commit -a -m "Branch backup" git branch branch-backup · Now run the command below to reset your remote branch to origin.
🌐
KodeKloud
kodekloud.com › blog › change-remote-origin-in-git
How to Change Remote Origin in Git
November 5, 2025 - Non-Destructive Change: Updating the remote origin does not affect your codebase, branches, or commit history - only the destination changes. Quick Verification: Use git remote -v before and after updating to confirm your repository now points ...
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › git › how-to-reset-a-git-branch-to-a-remote-repository
How to Reset a Git Branch to a Remote Repository? - GeeksforGeeks
May 22, 2024 - Force push the changes to the remote repository (if necessary): git push --force origin develop · Data Loss: The git reset --hard command will discard all local changes, including uncommitted changes and commits that are not in the remote branch.
🌐
Sentry
sentry.io › sentry answers › git › reset a local branch to remote state in git
Reset a local branch to remote state in Git | Sentry
How do I reset the state of a branch ... this. First, download all remote branches with git fetch: git fetch origin # <-- name of remote, change if not origin...
🌐
iO Flood
ioflood.com › blog › git-reset-local-branch-to-remote
How To Git Reset Local Branch To Remote | Helpful Git Tips
July 9, 2024 - To execute a local Git reset to remote, use the git reset origin command. This command discards any local commits not present on the remote branch, aligning your local branch with the remote.
Top answer
1 of 11
1989

Assuming that your branch is called master both here and remotely, and that your remote is called origin you could do:

 git reset --hard <commit-hash>
 git push -f origin master

However, you should avoid doing this if anyone else is working with your remote repository and has pulled your changes. In that case, it would be better to revert the commits that you don't want, then push as normal.

Update: you've explained below that other people have pulled the changes that you've pushed, so it's better to create a new commit that reverts all of those changes. There's a nice explanation of your options for doing this in this answer from Jakub Narębski. Which one is most convenient depends on how many commits you want to revert, and which method makes most sense to you.

Since from your question it's clear that you have already used git reset --hard to reset your master branch, you may need to start by using git reset --hard ORIG_HEAD to move your branch back to where it was before. (As always with git reset --hard, make sure that git status is clean, that you're on the right branch and that you're aware of git reflog as a tool to recover apparently lost commits.) You should also check that ORIG_HEAD points to the right commit, with git show ORIG_HEAD.

Troubleshooting:

If you get a message like "! [remote rejected] a60f7d85 -> master (pre-receive hook declined)"

then you have to allow branch history rewriting for the specific branch. In BitBucket for example it said "Rewriting branch history is not allowed". There is a checkbox named Allow rewriting branch history which you have to check.

2 of 11
259

Most other answers – including the accepted one – will result in unnecessary loss of local state.

Local changes are not inherently required to change a remote. You may need to apply local corrections in addition to resetting a remote, but that's a lower priority if your problem is simply that you need to quickly put the remote back to how it was before your push, or you pushed the wrong thing. This method (like any other forced push) has the potential to ruin your remote if you choose the wrong commit, but even then you can usually find the correct one and try again.

You must have the desired commit somewhere in your local repo that the remote should match.

  1. Do not do any local resetting, checking out, or branch switching.

  2. Use git log to find the commit you want to the remote to be at. Use git log -p to see changes, or git log --graph --all --oneline --decorate to see a compact tree.

    If you cannot find the commit, try checking git reflog which will show the state history of your repo. You may be able to recover the commit from its hash, e.g.:

    git branch <name for branch of rescued commit> <hash of rescued commit>
    
  3. Copy the commit's hash, tag, or (if it's the tip) its branch name.

  4. Run a command like:

    git push --force <remote> <commit-ish>:<the remote branch>
    

    e.g.

    git push --force origin 606fdfaa33af1844c86f4267a136d4666e576cdc:main
    

    or

    git push --force staging v2.4.0b2:releases
    

If the forced push fails, it's likely disabled by the remote. This may be worked around by temporarily changing one or both of receive.denyNonFastForwards and receive.denyDeletes. If your remote is hosted on a service without shell access, it probably has settings you can change to allow forced pushes.


I use a convenient alias (git go, mnemonic "graph oneline") for viewing history, which can be added like so:

git config --global alias.go 'log --graph --oneline --all --decorate'
🌐
Reddit
reddit.com › r/learnprogramming › [git] how to 'git reset' a remote branch?
r/learnprogramming on Reddit: [Git] How to 'git reset' a remote branch?
February 12, 2024 -

Say I have a local branch and I make change A, then push it so both the local and remote has change A. I then realize I don't want change A as there was an error in it. I can use git reset to reset my local branch to the commit before change A, but then if I do that and try to push, it doesn't work. This makes sense because technically the local branch is behind the remote branch. And a solution to this is to just make a "revert change A" commit, but I don't even want my commit history to show these two commits since they cancel each other out

Is there a way to do "git reset" a remote branch?

Top answer
1 of 5
1438

If you haven't pushed to origin yet, you can reset your branch to the upstream branch with:

git checkout mybranch
git reset --hard origin/mybranch

(Make sure that you reference your latest commit in a separate branch, like you mention in your question)

Note that just after the reset, mybranch@{1} refers to the old commit, before reset.

But if you had already pushed, see "Create git branch, and revert original to upstream state" for other options.


With Git 2.23 (August 2019), that would be one command: git switch.
Namely: git switch -C mybranch origin/mybranch

Example

C:\Users\vonc\git\git>git switch -C master origin/master
Reset branch 'master'
Branch 'master' set up to track remote branch 'master' from 'origin'.
Your branch is up to date with 'origin/master'.

That restores the index and working tree, like a git reset --hard would.


As commented by Brad Herman, a reset --hard would remove any new file or reset modified file to HEAD.

Actually, to be sure you start from a "clean slate", a git clean -f -d after the reset would ensure a working tree exactly identical to the branch you just reset to.


This blog post suggests those aliases (for master branch only, but you can adapt/extend those):

[alias]
   resetorigin = !git fetch origin && git reset --hard origin/master && git clean -f -d
   resetupstream = !git fetch upstream && git reset --hard upstream/master && git clean -f -d

Then you can type:

git resetupstream

or

git resetorigin
2 of 5
157

There is a slightly easier way to do this:

git reset --hard @{u}

@{u} is a shortcut for whatever your tracking branch is, so if you're on master and that tracks origin/master, @{u} points to origin/master.

The advantage of using this is that you don't have to remember (or type out) the full name of your tracking branch. You can also make an alias:

git-reset-origin="git reset --hard @{u}"

which will work regardless of the branch you're currently on.

🌐
Graphite
graphite.com › guides › git-hard-reset-remote
Git hard reset to remote - Graphite
To perform a hard reset to a remote branch, you'll typically follow these steps in your terminal: Fetch the latest changes from the remote repository: ... This command updates your remote-tracking branches, the pointers that connect a local copy of a branch to its remote counterpoint, under refs/remotes/origin/. git fetch will fetch all of the changes that you do not have yet from the remote repository without merging those changes into your local branch.
🌐
Tim Mousk
timmousk.com › blog › git-reset-to-remote
How To Reset To Remote In Git? – Tim Mouskhelichvili
March 12, 2023 - bashgit reset --hard origin/main · 4. Clean up the local files and directories using the git clean command. bashgit clean -fdx · Note: This example uses the main branch, but the process works with any other branch.
🌐
30 Seconds of Code
30secondsofcode.org › home › git › repository › reset master to match remote
Git - Reset your local master branch to match remote - 30 seconds of code
March 31, 2024 - # Syntax # git fetch origin # git checkout master # git reset --hard origin/master git fetch origin git checkout master git reset --hard origin/master # Local `master` branch is now up to date with remote `master`
🌐
PhoenixNAP
phoenixnap.com › home › kb › devops and development › how to git reset to remote
How to Git Reset to Remote {3 Simple Methods}
February 4, 2026 - This step ensures you get the latest updates stored on the remote repository. Use the following syntax: git fetch [remote_name] Replace [remote_name] with the name of your remote repository. For example: git fetch origin ·
🌐
iO Flood
ioflood.com › blog › git-reset-local-branch-to-remote-repository
Git Reset Local Branch to Remote Repository
November 26, 2023 - To do so, first fetch the updates from the remote repo with git fetch origin. Then, reset your local branch to the state of the remote branch using git reset --hard origin/[branch_name]. This allows you to sync with changes made by others without merging conflicts.
🌐
Mike Street
mikestreety.co.uk › blog › reset-a-git-repository-back-to-origin
Reset a git repository back to origin - Mike Street - Lead Developer and CTO
February 22, 2023 - We ran through the following commands which got the local repository nice and clean and back to it's original state. ... Replace main with your primary branch name below. git branch | grep --invert-match "main" | xargs git branch --delete · Clean any remote branches your repository thinks it knows about that no longer exist