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):
Answer from VonC on Stack Overflow[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 -dThen you can type:
git resetupstreamor
git resetorigin
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 -dThen you can type:
git resetupstreamor
git resetorigin
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.
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?
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).
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.