How can I find out which branch checked out on the remote machine?

I am using Git to sync working directories between my laptop and my desktop:

  • there is a remote origin

    , known on both machines as origin

    , which contains a bare repo
  • on a laptop, an extra remote call desktop

    that contains a repo with a working copy
  • on the desktop, an additional remote call notebook

    that contains a repo with a working copy

When I work from home I push commit over a (slow) VPN connection with origin

and desktop

. Sometimes it happens that I started a branch on my desktop computer, left my workplace, and that branch was checked out and continued working on my notebook. Pretty simple.

Things get problematic when I'm going to push changes back to the machine desktop

from the notebook: Git push error '[remote rejected] master -> master (branch is currently checked out)' The problem occurs when the current branch is currently checked out to desktop

.

To avoid the error, I would like to know ahead of time which branch is currently checked out for desktop

. Is there a way to achieve this?


Yes of course. I could wait for me to get back to work before pushing and then checkout another branch on the desktop. I could also create a temporary branch every time I leave my workplace. I could open an RDP session on my desktop and check out another branch. I know it. But that's not what I'm asking: no workaround.

+3


source to share


2 answers


How can I find out which branch checked out on the remote machine?

Next command

git remote show <remote-name>

      

prints all kinds of information about <remote-name>

, including the selected branch. If you only want to extract the name of the branch, you can run the following:

git remote show <remote-name> | sed -n 's/  HEAD branch: //p'

      

(At least this command works with Git 2.1.3.)



Alias

As a bonus, there is an alias here:

[alias]
    remotehead = "!sh -c \"git remote show $1 | sed -n 's/  HEAD branch: //p'\" -"

      

If you add this alias entry to your ~/.gitconfig

file, you can use it like this

$ git remotehead <remote-name>

      

+4


source


You can use git rev-parse

to resolve a symbolic link desktop/HEAD

.



% git rev-parse --symbolic-full-name desktop/HEAD
refs/remotes/desktop/master

      

+2


source







All Articles