Reputation: 35730
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
?
Upvotes: 5
Views: 4230
Reputation: 1115
In case you're using the fantastic oh my zsh - it already comes with an alias groh
that does exactly that:
groh='git reset origin/$(git_current_branch) --hard'
Upvotes: 1
Reputation: 31
git rev-parse --abbrev-ref HEAD | xargs -I {} git reset --hard origin/{}
worked for me, it's a bit long though
Upvotes: 3
Reputation: 1078
# simple script to reset hard to the current branch origin
branch=$(git symbolic-ref --short -q HEAD)
echo "Sure to reset hard to origin/$branch ?"
read
git reset --hard origin/$branch
Upvotes: 3
Reputation: 12033
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.
Upvotes: 6
Reputation: 62369
"Whatever you currently have checked out" goes by the name HEAD
. So, git reset --hard HEAD
will revert you to whatever you had checked out before you started making modifications that you have now decided you don't want to keep...
Since the branch you're currently on may or may not have an upstream equivalent, I'm not sure there's a single command to blindly achieve what you're describing. You could write a bit of a shell script that parses the output from git branch
and makes a decision on which remote branch you want to be on, or dies if it sees something it doesn't expect.
Upvotes: 0