How do I git reset - bad origin / any branch I checked?

I am trying to write a git deployment script, but the script must update multiple servers, and they are not all in the same git branch. Ideally, a script should only need one command, resulting in "git reset --hard origin / whateverBranchThisServerIsOn".

Now I understand what I can do:

git reset --hard origin/foo 

to reset my git for remote branch foo. However, what I would like to do is reset for the remote branch, but not โ€œfooโ€ specifically, just โ€œremote from any branch that the machine is currently checking (i.e. which branch appears when you do git branch โ€ .

I tried:

 git reset --hard origin 

and

 git reset --hard origin/HEAD 

and

 git reset --hard HEAD 

but they all either check other branches, or (in the latter case) do not receive remote commits. Of course, is there a way to say git reset --hard origin/CURRENT_BRANCH ?

+4
source share
3 answers

The idea of โ€‹โ€‹the "branch I checked" is a little distorted. There is no 1: 1 ratio between branches. Your branch has no "origin" at all. But it may 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 like indicating that HEAD@ {upstream} should work in order to do what you want. But I did not test it.

+5
source
 # 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 
+3
source

"No matter what you checked now," goes by the name of HEAD . So git reset --hard HEAD will bring you back to what you checked before proceeding with the changes that you now decide you donโ€™t want to continue ...

Since the branch you are currently in may or may not have an upstream equivalent, Iโ€™m not sure that there is one command to blindly achieve what you are describing. You can write a little shell script that parses the output from git branch and decides on which remote branch you want to be included on, or dies if it sees something that it does not expect.

0
source

Source: https://habr.com/ru/post/1437473/


All Articles