Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

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?

like image 427
machineghost Avatar asked Dec 15 '22 18:12

machineghost


1 Answers

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 HEAD@{upstream} should work to do what you want. But I haven't tested it.

like image 197
Andy Ross Avatar answered Jan 29 '23 21:01

Andy Ross