[GTALUG] git questions

Giles Orr gilesorr at gmail.com
Fri Mar 6 15:29:44 UTC 2015


I've started using git fairly heavily (although not necessarily very
well).  I have several repos: a couple are code, also my ~/.vim/
folder and I'm thinking about adding ~/bin/ .  If I remember to push
from a particular machine before leaving it, I'll never have to deal
with merging as I'm the only user.  While most likely my discomfort
with merging will be overcome by practice as I understand git does it
well (I use SVN at work: merging is shudder-inducing), I'm guessing
keeping everything up to date is still preferable.  These questions
are mostly about incorporating information about the repo into the
Bash prompt: I was impressed recently by the way zsh appears to handle
it, with a sequence of tiny icons in the lower right corner of the
terminal indicating relative status.  I didn't talk to the zsh user
long, so I don't know if that's built-in, a plugin, or something he
did himself.

I'm starting from some code I got from nitrous.io, lovely in its conciseness:

parse_git_branch () {
  git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/*
\(.*\)/\1$(parse_git_dirty)/"
}
parse_git_dirty () {
  git diff --no-ext-diff --quiet --exit-code &>/dev/null || echo "!"
}

Put \$(parse_git_branch) into your prompt and it tells you what branch
you're on, and if there are any unstaged changes (and goes away if
you're not in a repo).  I'd prefer it did uncommitted rather than
unstaged, haven't tried to fix that yet.  But I want to update it as
it doesn't deal with origin at all.

So, the questions:

What is the easiest and most concise way to determine if your local is
behind origin master?  I've found that "git remote show origin" will
show this information, but I'm not sure if it's the "best" way to find
out, and I'm also concerned that running that every time your prompt
comes up would slow things down as it makes a remote call(?) to get an
answer - when you might not even have a network connection, or worse,
a very slow connection.

"git status" usually says "Your branch is up-to-date with
'origin/master'" (or "ahead"), but occasionally - even though origin
is configured properly - this line doesn't appear.  Is there a way to
convince it to always show this line?

Unfortunately, "git status" doesn't seem to ever notice if you're
"behind" origin, thus the need for "git remote show origin".  Any fix
for that?

The thought was to have output for the prompt that looked like this:

(everything synced): "master-"
(uncommitted local changes): "master!-"
(behind origin): "master^"
(ahead of origin): "masterv"
(ahead and behind, with local changes): "master!^v"

You get the general idea.

I'm aware git is capable of immense complexity (branches, detached
head, multiple remotes, different remotes for push and pull, etc.)
that aren't addressed here.  If I tried to tackle all of that at once
(especially given I don't understand most of it) my head would explode
and nothing would get done.  So I'm sticking with my simple use case
until I have to deal with the more complex stuff.

Any thoughts welcome.

-- 
Giles
http://www.gilesorr.com/
gilesorr at gmail.com


More information about the talk mailing list