threads / discuss / 23049

git branch --current?

Subject: git branch --current?

## tl;dr

6 messages between Mar 17, 2010 and Mar 18, 2010.

replies: 5people: 3as markdown or json

Nguyen Thai Ngoc Duy· Mar 17, 2010, 14:43 UTC · lore

I use "git branch" when I need to know the current branch. But I have many branches, looking for the star is "time consuming". I'd rather have something like pwd, just print the current branch name and that's all. A bit more information about current branch wouldn't hurt, as long as the branch name is printed at a fixed location.

What command do you use to know what branch you are on?
-- 
Duy
Jay Soffian· Mar 17, 2010, 14:48 UTC · re: Nguyen Thai Ngoc Duy · lore

Re: git branch --current?

On Wed, Mar 17, 2010 at 10:43 AM, Nguyen Thai Ngoc Duy <pclouds@gmail.com> wrote:

Show 7 quoted lines
> I use "git branch" when I need to know the current branch. But I have
> many branches, looking for the star is "time consuming". I'd rather
> have something like pwd, just print the current branch name and that's
> all. A bit more information about current branch wouldn't hurt, as
> long as the branch name is printed at a fixed location.
>
> What command do you use to know what branch you are on?

Doesn't everyone who's anyone put it in their prompt with the bash completion script? :-)

Well, if not, how about a simple alias:
pwb = rev-parse --abbrev-ref HEAD
?
j.
Nguyen Thai Ngoc Duy· Mar 17, 2010, 15:04 UTC · re: Jay Soffian · lore

Re: git branch --current?

On 3/17/10, Jay Soffian <jaysoffian@gmail.com> wrote:
Show 13 quoted lines
> On Wed, Mar 17, 2010 at 10:43 AM, Nguyen Thai Ngoc Duy
>  <pclouds@gmail.com> wrote:
>  > I use "git branch" when I need to know the current branch. But I have
>  > many branches, looking for the star is "time consuming". I'd rather
>  > have something like pwd, just print the current branch name and that's
>  > all. A bit more information about current branch wouldn't hurt, as
>  > long as the branch name is printed at a fixed location.
>  >
>  > What command do you use to know what branch you are on?
>
>
> Doesn't everyone who's anyone put it in their prompt with the bash
>  completion script? :-)

Me obviously :-) But bash is not the one true shell. I need to use csh sometimes.

>  Well, if not, how about a simple alias:
>
>  pwb = rev-parse --abbrev-ref HEAD

Hmm.. I have alias.pwb = symbolic-ref HEAD. Both solutions are not as good as "git branch", say detached HEAD case.

-- 
Duy
Jay Soffian· Mar 17, 2010, 15:14 UTC · re: Nguyen Thai Ngoc Duy · lore

Re: git branch --current?

On Wed, Mar 17, 2010 at 11:04 AM, Nguyen Thai Ngoc Duy <pclouds@gmail.com> wrote:

> Hmm.. I have alias.pwb = symbolic-ref HEAD. Both solutions are not as
> good as "git branch", say detached HEAD case.

How about naming something like this git-pwb and copying it to somewhere in PATH:

#!/bin/sh . /opt/git/share/git-core/git-completion.bash echo $(__git_ps1)

Perhaps you are inspired to contribute git-completion.csh?
:-)
j.
David Aguilar· Mar 17, 2010, 19:06 UTC · re: Jay Soffian · lore

Re: git branch --current?

Hi,
On Wed, Mar 17, 2010 at 11:14:44AM -0400, Jay Soffian wrote:
Show 18 quoted lines
> On Wed, Mar 17, 2010 at 11:04 AM, Nguyen Thai Ngoc Duy
> <pclouds@gmail.com> wrote:
> > Hmm.. I have alias.pwb = symbolic-ref HEAD. Both solutions are not as
> > good as "git branch", say detached HEAD case.
> 
> How about naming something like this git-pwb and copying it to
> somewhere in PATH:
> 
> #!/bin/sh
> . /opt/git/share/git-core/git-completion.bash
> echo $(__git_ps1)
> 
> 
> Perhaps you are inspired to contribute git-completion.csh?
> 
> :-)
> 
> j.

You caught my attention. I don't personally like csh. In fact, I'm actively working on a way to get us off of csh here at $dayjob. Hence, I usually shy away from anything that encourages csh usage.

That said, I'm a practical person. We've written a git-completion.tcsh here so I figured I'd share.

Caveats -- it sucks! I'm pretty sure that this is due to either a) csh's poor completion facilities, or b) my inability to use it to its best ability. The thing that really kills me is that I can't figure out how to get both path completion and --option completion at the same time. For a while I had 'echo *' in there to handle that but that breaks down once you start completing down a subdirectory (the "/" breaks it). Hence, we always use the '--' separator when specifying pathspecs.

What I learned from this exercise? Bash completion is effin amazing! (typed from a bash terminal)

I'm sure there's plenty of ways to improve this. Aside from throwing it in contrib/ and hoping others find it useful enough to improve it, I've probably hit my tcsh completion-foo limit.

-- >8 -- git-completion.tcsh -- >8 --

# Source this script in tcsh to setup shell completions # for git. Completions are activated by typing <tab> or Control-D # in the shell after entering a partial command. # # Usage: # source git-completion.tcsh (e.g. in ~/.cshrc) # # Supported completions: # git <tab> (lists git commands) # git help <tab> (lists git commands) # git branch <tab> (lists branch names) # git checkout <tab> (lists branch names) # git log <tab> (lists the most commonly used flags) # git remote <tab> (lists git remote commands) # git remote add|prune|rm|show|update <tab> # (lists git remote names) # In addition to entering <tab> where shown, you can enter it after # typing part of the word, e.g. git branch bug<tab> to auto-complete # branches starting with "bug". # # Author: David Adler, David Aguilar

if ( -x /usr/bin/git) then
    # Git is installed so define tcsh completions for it.
    # List of known git subcommands
    # This is a hard-coded list to avoid calling 'git help' at startup.
    set __git_cmd_names = (add bisect blame branch checkout clone commit config \
                           diff difftool fetch grep gui init log merge mv pull push \
                           rebase reset rm show shortlog stash status tag)
    alias __git_aliases 'git config --get-regex "alias.*" | sed -n "s,alias\.\([^ ]*\).*,\1,p"'
    alias __git_branches 'git for-each-ref --format="%(refname)" refs/heads refs/remotes | sed -e s,refs/remotes/,, | sed -e s,refs/heads/,,'
    alias __git_origin_branches 'git for-each-ref --format="%(refname)" refs/remotes/origin | grep -v HEAD | sed -e s,refs/remotes/origin/,,'
    # Define the completions (see the tcsh man page).
    complete git \
             'p/1/`__git_aliases | xargs echo $__git_cmd_names`/' \
             "n/help/($__git_cmd_names)/" \
             'n/branch/`__git_branches | xargs echo -m -d`/' \
             'n/config/(--global --get-regexp --list)/' \
             'n/diff/`__git_branches | xargs echo --check --staged --stat -- *`/' \
             'n/difftool/`__git_branches | xargs echo --no-prompt --staged -- *`/' \
             'n/fetch/`git remote`/' \
             'n/merge/`__git_branches`/' \
             'n/log/`__git_branches | xargs echo -- --name-only --name-status --reverse --committer= --no-color --relative --ignore-space-change --ignore-space-at-eol --format=medium --format=full --format=fuller`/' \
             'n/stash/(apply list save pop clear)/' \
             'n/push/`git remote`/' \
             'N/push/`__git_origin_branches`/' \
             'n/pull/`git remote | xargs echo --rebase`/' \
             'n/--rebase/`git remote`/' \
             'N/--rebase/`__git_origin_branches`/' \
             'N/pull/`__git_origin_branches`/' \
             'n/rebase/`__git_branches | xargs echo --continue --abort --onto --skip --interactive`/' \
             'N/rebase/`__git_branches`/' \
             'n/remote/(show add rm prune update)/' \
             'N/remote/`git remote`/' \
             'n/checkout/`__git_branches | xargs echo -b --`/' \
             'N/-b/`__git_branches`/'
endif
-- 
		David
Nguyen Thai Ngoc Duy· Mar 18, 2010, 02:00 UTC · re: David Aguilar · lore

Re: git branch --current?

On Thu, Mar 18, 2010 at 2:06 AM, David Aguilar <davvid@gmail.com> wrote:
Show 17 quoted lines
> You caught my attention.  I don't personally like csh.  In fact,
> I'm actively working on a way to get us off of csh here at
> $dayjob.  Hence, I usually shy away from anything that
> encourages csh usage.
>
> That said, I'm a practical person.  We've written a
> git-completion.tcsh here so I figured I'd share.
>
> Caveats -- it sucks!  I'm pretty sure that this is due
> to either a) csh's poor completion facilities, or b) my
> inability to use it to its best ability.  The thing that really
> kills me is that I can't figure out how to get both path
> completion and --option completion at the same time.
> For a while I had 'echo *' in there to handle that but
> that breaks down once you start completing down a subdirectory
> (the "/" breaks it).  Hence, we always use the '--' separator
> when specifying pathspecs.
But it works, from a user point of view.
Show 6 quoted lines
> What I learned from this exercise?
> Bash completion is effin amazing!  (typed from a bash terminal)
>
> I'm sure there's plenty of ways to improve this.  Aside from
> throwing it in contrib/ and hoping others find it useful enough
> to improve it, I've probably hit my tcsh completion-foo limit.
I'll put it in my ~/.tcsh and see how it goes. Thanks.
-- 
Duy

← back to recent threads