How do I git reset –hard origin/whatever branch I have checked out?

Issue

I’m trying to write a git deployment script, but the script has to update several servers and they aren’t all on the same Git branch. Ideally the script should need just one command, to the effect of “git reset –hard origin/whateverBranchThisServerIsOn”.

Now, I understand that I can do:

git reset --hard origin/foo

to reset my git environment to the remote foo branch. However, what I’d like to do is reset to a remote branch, but not “foo” specifically, just “the remote of whatever branch the machine currently has checked out (ie. what branch shows up when you do git branch“.

I’ve tried:

git reset --hard origin

and:

git reset --hard origin/HEAD

and:

git reset --hard HEAD

but they all either checkout other branches, or (in the last case) don’t get the remote commits. Surely there is some way to say git reset --hard origin/CURRENT_BRANCH?

Solution

The idea of “the branch I have checked out” is a little malformed. There is no 1:1 relationship between branches like that. Your branch may not have an “origin” at all. But it can have a remote tracking branch, which may (or may not) have the same name as the one you are on. Reading man gitrevisions, it looks specifying [email protected]{upstream} should work to do what you want. But I haven’t tested it.

Answered By – Andy Ross

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published