I'm using Git to sync working directories between my notebook and my desktop computer:
origin
known on both machines as origin
which holds a bare repodesktop
that holds a repo with working copynotebook
that holds a repo with working copyWhen I'm working at home, I'm pushing commits via a (slow) VPN connection to origin
and desktop
. Sometime, it happens that I started a branch on the desktop machine, left my workplace with this branch being checked out and continue work on the notebook. Pretty simple so far.
Things get problematic when I'm going to push changes back to the desktop
machine from the notebook: the Git push error '[remote rejected] master -> master (branch is currently checked out)' problem occurs when the current branch is currently checked out on desktop
.
In order to avoid the error, I'd like to know in advance which branch is currently checked out on desktop
. Is there a way to achieve this?
Yes, of course. I could wait until I'm back at work before pushing and then check out a different branch on desktop. I could also create a temp branch every time I leave my workplace. I could open a RDP session to my desktop machine and check out a different branch. I know that. But that's not what I'm asking for: no workaround.
How could I find out which branch is checked out on a remote machine?
The following command,
git remote show <remote-name>
prints all kinds of information about <remote-name>
, including the branch currently checked out. If you want to extract just the branch name, 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.)
As a bonus, here is an alias for it:
[alias]
remotehead = "!sh -c \"git remote show $1 | sed -n 's/ HEAD branch: //p'\" -"
If you add this alias entry in your ~/.gitconfig
file, you should be able to use it like so
$ git remotehead <remote-name>