I was accidentally working on a branch I shouldn’t have been for a while, so I branched off of it giving it the appropriate name. Now I want to overwrite the branch I shouldn’t have been on to the version from origin (github). Is there an easy way to do this? I tried deleting the branch and then resetting up the tracking branch, but it just gives me the version I was working on again.
Share
If you haven’t pushed to origin yet, you can reset your branch to the upstream branch with:
(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/mybranchExample
That restores the index and working tree, like a
git reset --hardwould.As commented by Brad Herman, a
reset --hardwould remove any new file or reset modified file to HEAD.Actually, to be sure you start from a “clean slate”, a
git clean -f -dafter the reset would ensure a working tree exactly identical to the branch you just reset to.This blog post suggests those aliases (for
masterbranch only, but you can adapt/extend those):