简体   繁体   中英

Git: How to revert my local branch to the remote branch state?

I messed my local branch of a project, and i want to revert my local copy to the remote state. How i acomplish that simply goal?

If you are not afraid of losing any local history, you can switch to another branch then delete your local branch, then check the remote version out. For example, if you wanted to revert a branch called "test_feature," you could do this:

$ git checkout master
$ git branch -D test_feature # see note about -D below
$ git checkout test_feature  # should track origin/test_feature

NOTE: -D will force delete the branch, and will suppress warnings about unmerged changes.

This is useful if you have merged a branch you didn't intend to, as the HEAD pointer can change depending on the type of merge.

EDIT : Another method for doing the same thing is to simply type:

git reset --hard origin/test_feature

This will reset the branch you are currently on to the state of the remote (origin in this case) branch test_feature .

@hvgotcodes has a variation of this in his example (he's targeting the HEAD commit)

you can reset with

git reset --hard HEAD

you will lose all your changes if you do this. This might be helpful .

If you want to revert to remote LAST version :

git fetch --all
git reset --hard origin/master

If you want to revert to a specific version :

First, get the string that identify the commit in some date, doing:

git rev-list -n 1 --before="2009-07-27 13:37" origin/master

it prints the commit identifier, take the string (for instance XXXX) and do:

git checkout XXXX

There's simple one liners to do what you're asking, but usually when I feel like I've "messed up a branch" vs. what's on the remote, I use interactive rebasing, which gives me a bit more control. That way I can edit commits if need be, or remove lines if I just don't want the commit. See here: http://book.git-scm.com/4_interactive_rebasing.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM