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
Answer from VonC on Stack Overflow
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
Reverting a branch to a clean state before applying new changes. A hard reset changes the state of your repository to a specific commit. Here’s how it works in detail: Resetting the HEAD: The HEAD in Git is a reference to the current commit you're working on.
🌐
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?

🌐
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
There are two commands you can use to achieve this. First, download all remote branches with git fetch: git fetch origin # <-- name of remote, change if not origin · Second, use git reset to sync the local branch with the remote branch:
🌐
Git
git-scm.com › docs › git-reset
Git - git-reset Documentation
When you specify files or directories or pass --patch, git reset updates the staged version of the specified files. ... Set the current branch head (HEAD) to point at <commit>. Depending on <mode>, also update the working directory and/or index to match the contents of <commit>. <commit> defaults ...
🌐
Gitbybit
gitbybit.com › gitopedia › git-commands › git-reset
Gitopedia: git reset — move a branch pointer
To this day, veteran developers still use git reset for these tasks. However, with the introduction of the git restore command in Git 2.23.0, it is now recommended to use git restore for managing the working tree and staging area, while git reset should primarily be used for moving the HEAD and current branch pointer.
🌐
Medium
medium.com › @elton-martins › to-reset-a-git-branch-to-match-the-master-main-branch-6692c28a36fc
To reset a Git branch to match the master/main branch | by Elton Martins | Medium
March 15, 2024 - 1. Make sure you are on the branch you want to reset: ... This git reset - -hard command will discard all local changes in your branch and set the branch to the same commit as the remote master branch.
Find elsewhere
🌐
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.
Top answer
1 of 2
7

HEAD is where your workspace is currently in the tree of git commits; detached means that it doesn't correspond to a branch. To fix this, you should create a new branch with git checkout -b branch (replacing branch with the name you want to give your new branch).

If you want to drop the commits following the one you reset to, you can delete the master branch and re-create it:

git branch -D master
git checkout -b master

If you're working on a repository which is pushed elsewhere you'll need to do more work to fix things up, possibly forcing a push (and telling every one else to re-clone their workspace). If you have shared state, you should really create a revert commit; take a look at git revert (starting from master and reverting all the commits starting with the one following c70e611).

2 of 2
3

HEAD detached at c70e611

This is because when you did the git reset --hard, you were not on any branch at that time. You had a detached HEAD, and that detached head got moved with the git reset --hard command, along with a rewrite of your working tree to that state.

If you want some branch foo to be c70611, then:

git checkout foo
git reset --hard c70611

If this is considered to be a good state to push to foo's upstream then just git push <remote-name> foo.

There is a more direct way to force foo to c70611 without checking it out to be the current branch. Namely, you can rewrite what foo points to using the git update-ref command.

Before doing any of the above, I would pause and try to see how I had ended up in a detached state without noticing. Perhaps it was an unfinished rebase or whatever. Step one is to review the last few entries in the git reflog to help jog your memory.

🌐
DEV Community
dev.to › cicube › how-to-reset-your-local-git-branch-5273
How to Reset Your Local Git Branch - DEV Community
November 7, 2024 - You may have done some experimental changes which just don't fit in, or you resolve a merge conflict and end up with a messy state. For such situations, the command git reset --hard could be a savior.
🌐
Medium
medium.com › @vishalbarvaliya › resetting-your-git-branch-to-a-previous-commit-a-complete-guide-96cc314a172e
Resetting Your Git Branch to a Previous Commit: A Complete Guide | by Vishal Barvaliya | Medium
September 8, 2024 - Your branch has accumulated multiple unnecessary commits and you want to clean it up. You’re dealing with a merge conflict and need to revert to a clean state. The `git reset --hard` command is the most direct and forceful way to reset your branch.
🌐
GitLab
forum.gitlab.com › how to use gitlab
Reset feature branch to master - How to Use GitLab - GitLab Forum
September 22, 2022 - Hello, I would like to reset my feature branch to master. I usually do “git reset --hard master” when my feature branch is checked out. That work, but then I’m unable to push it to gitlab origin, because it tell me th…
🌐
Quora
quora.com › How-do-you-reset-a-broken-Git-branch-to-master
How to reset a broken Git branch to master - Quora
Answer: Define “broken”? If you just want to throw away all the work you can simply run “git reset --hard master” and it’s already done. If “broken” means that git repository is totally messed up and “git fsck” reports errors, it’s probably better to clone the whole repository ...
🌐
QEMU
qemu.org › download
Download QEMU - QEMU
reset to 0 with every major increment, otherwise incremented by 1 for each release from git master · micro · always 0 for releases from git master, incremented by 1 for each stable branch release · The implication of this is that changes in major version number do not have any bearing on the scope of changes included in the release.
🌐
GitHub
github.com › mattpocock › skills › blob › main › skills › misc › git-guardrails-claude-code › SKILL.md
skills/skills/misc/git-guardrails-claude-code/SKILL.md at main · mattpocock/skills
April 28, 2026 - Set up Claude Code hooks to block dangerous git commands (push, reset --hard, clean, branch -D, etc.) before they execute.
Author   mattpocock
🌐
42mate
blog.42mate.com › git-reset-a-branch-to-the-last-thing-in-the-remote-repository
Git : Reset a Branch to the last thing in the remote repository
July 20, 2016 - The commands for that are ... The trick is the fetch at the beginning, that pulls all the latest data from the remote and will not try to merge into the brach. When we do the git reset –hard from origin/mybranch will use this remote data pulled previously to merge into our local branch.
🌐
Gitbooks
sillevl.gitbooks.io › git › content › advanced › reset-checkout-revert
Reset, Checkout, and Revert · Git
When you don’t include a file path as a parameter, they operate on whole commits. That’s what we’ll be exploring in this section. Note that git revert has no file-level counterpart. On the commit-level, resetting is a way to move the tip of a branch to a different commit.
🌐
Atlassian Support
support.atlassian.com › sourcetree › kb › reset-branch-to-a-commit
Reset Branch to a Commit | Sourcetree | Atlassian Support
April 15, 2025 - 2. Then, right click on a specific commit, and select "Reset current branch to this commit". Similar to below: ... To understand further on the different types of reset (soft, mixed, hard), view this page: https://git-scm.com/docs/git-reset
🌐
Lovable
docs.lovable.dev › git › sync your lovable project with github
Sync your Lovable project with GitHub - Lovable Documentation
3 days ago - After connecting a project to a GitHub repository, you see the following in the project’s GitHub settings: ... Option to Disconnect (available from the dropdown) Lovable only edits and syncs one branch at a time. By default, this is the repository’s default branch (usually main).