3

I'm using Git to sync working directories between my notebook and my desktop computer:

  • there's a remote origin known on both machines as origin which holds a bare repo
  • on the notebook, theres an additional remote called desktop that holds a repo with working copy
  • on the desktop, theres an additional remote called notebook that holds a repo with working copy

When 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.

Community
  • 1
  • 1
eckes
  • 64,417
  • 29
  • 168
  • 201
  • 1
    Why are you pushing to _both_ origin and desktop? I'd say the correct pattern would be to push only to the bare git and _pull_ that data into your netbook repository. You'd also be pushing less data over the slow VPN line. Are you running an old version of Git? IIRC recent version don't allow pushes into non-bare gits _at all_. – Magnus Bäck Dec 15 '14 at 08:55

2 Answers2

5

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.)

Alias

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>
jub0bs
  • 60,866
  • 25
  • 183
  • 186
2

You can use git rev-parse to resolve the symbolic reference desktop/HEAD.

% git rev-parse --symbolic-full-name desktop/HEAD
refs/remotes/desktop/master
chepner
  • 497,756
  • 71
  • 530
  • 681
  • This doesn't update the remote before printing, right? – eckes Dec 15 '14 at 19:21
  • 1
    Right; it essentially just checks the content of the local file `.git/refs/remotes/desktop/HEAD`. You would have to do a `git fetch` first to ensure that this was (more) up to date. – chepner Dec 15 '14 at 19:37
  • Sadly, in a new repo with one branch pushed to it, this may not have a 'HEAD' set locally, even after fetching. But the [remote show](https://stackoverflow.com/a/27480892/5363308) answer above will return the correct result. – Paul F. Wood Aug 16 '19 at 10:06