Need to reset git branch to origin version

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.


If you don't have pushed yet to origin, 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.


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

Assuming this is what happened:

# on branch master
vi buggy.py                 # you edit file
git add buggy.py            # stage file
git commit -m "Fix the bug" # commit
vi tests.py                 # edit another file but do not commit yet

Then you realise you make changes on the wrong branch.

git checkout -b mybranch    # you create the correct branch and switch to it

But master still points to your commit. You want it to point where it pointed before.

Solution

The easiest way is:

git branch --force master origin/master

Another way is:

git checkout master
git reset --soft origin/master
git checkout mybranch

Note that using reset --hard will cause your uncommitted changes to be lost ( tests.py in my example).


I have a private repo on a server and regularly rebase/force-push to it, which makes it necessary to reset the local branch on my other computer often. I therefore created the following alias "catchup", which allows doing this for the current branch. Unlike the other answer there is no hardcoded branch name in this alias.

Hold on tight.

[alias]
  catchup = "!f(){ echo -n "reset 33[0;33m$(git symbolic-ref -q --short HEAD)33[0m to 33[0;33m$(git for-each-ref --format='%(upstream:short)' $(git symbolic-ref -q HEAD))33[0m? (Y/n) "; read -r ans; if [ "$ans" = "y" -o "$ans" = "Y" -o -z "$ans" ]; then git reset --hard $(git for-each-ref --format='%(upstream:short)' $(git symbolic-ref -q HEAD)); else echo "catchup aborted"; fi }; f"

Properly formatted (won't work with the newlines in .gitconfig) it looks like this:

"
!f(){
  echo -n "reset 33[0;33m$(git symbolic-ref -q --short HEAD)33[0m to 33[0;33m$(git for-each-ref --format='%(upstream:short)' $(git symbolic-ref -q HEAD))33[0m? (Y/n) ";
  read -r ans;
  if [ "$ans" = "y" -o "$ans" = "Y" -o -z "$ans" ]; then
    git reset --hard $(git for-each-ref --format='%(upstream:short)' $(git symbolic-ref -q HEAD));
  else
    echo "catchup aborted";
  fi
}; f
"
  • The 33[0;33m and 33[0m is for emphasizing the current branch and upstream with color.
  • $(git symbolic-ref -q --short HEAD) is the current branch name
  • $(git for-each-ref --format='%(upstream:short)' $(git symbolic-ref -q HEAD)) is the upstream of the current branch.
  • Since reset is a potentially dangerous call (especially with the --hard option, you will lose any uncommitted changes), it first tells you what it's about to do. For example if you're on branch dev-container with remote called qcpp/dev-container and you enter git catchup , you'll be prompted:

    reset dev-container to qcpp/dev-container? (Y/n)

    If you then type y or just hit return, it will perform the reset. If you enter anything else the reset will not be carried out.

    If you want to be super safe and programmatically prevent losing unstaged/uncommitted changes, you can pimp the above alias further with according checks for diff-index.

    The obligatory word of warning: If you are working on a public repository other people have based work on, and you need this alias, you are doing it wrong™.

    链接地址: http://www.djcxy.com/p/26244.html

    上一篇: 用远程分支覆盖我的本地分支

    下一篇: 需要将git分支重置为原始版本