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 asorigin
, 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.
source to share
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>
source to share