[PATCH] Add git-branches-script

Subsystems: the rest

DORMANTno replies

3 messages, 3 authors, 2016-06-15 · open the first message on its own page

[PATCH] Add git-branches-script

From: Amos Waterland <hidden>
Date: 2016-06-15 22:42:04

For people whose workflow involves switching back and forth between a
lot of branches, it can be really helpful to be able to quickly tell
which branch you are on and which ones are available.  This patch
introduces a small script that when invoked as `git branches' prints a
list of available branches with a star in front of the one you are on:

 $ cd linux-2.6/
 $ git checkout -b ppc64-cleanups
 $ git checkout -b ppc64-new-devel
 $ git checkout -b ppc64-all-merge
 $ git branches
   master
 * ppc64-all-merge
   ppc64-cleanups
   ppc64-new-devel

Signed-off-by: Amos Waterland <redacted>

---

 git-branches-script |    8 ++++++++
 1 files changed, 8 insertions(+), 0 deletions(-)
 create mode 100755 git-branches-script

12ab86f36137c4ffd1fb9b878479b9befe4cf2d4
diff --git a/git-branches-script b/git-branches-script
new file mode 100755
--- /dev/null
+++ b/git-branches-script
@@ -0,0 +1,8 @@
+#!/bin/sh
+
+. git-sh-setup-script || die "Not a git archive"
+
+current=$(basename $(readlink $GIT_DIR/HEAD))
+
+cd $GIT_DIR/refs/heads &&
+ls | sed -e "s/^/  /" -e "s/  $current/* $current/"

Re: [PATCH] Add git-branches-script

From: Kalle Valo <hidden>
Date: 2016-06-15 22:42:04

Amos Waterland [off-list ref] writes:
For people whose workflow involves switching back and forth between a
lot of branches, it can be really helpful to be able to quickly tell
which branch you are on and which ones are available.  This patch
introduces a small script that when invoked as `git branches' prints a
list of available branches with a star in front of the one you are on:

 $ cd linux-2.6/
 $ git checkout -b ppc64-cleanups
 $ git checkout -b ppc64-new-devel
 $ git checkout -b ppc64-all-merge
 $ git branches
   master
 * ppc64-all-merge
   ppc64-cleanups
   ppc64-new-devel
I would prefer to using some git command compared to 'ls -l
.git/refs/heads/'. In my opinion there is a need for this command and
it should be included.

But my immediate concern was that there is already 'git branch'
command and 'git branches' is too similar. I think they should be
merged. I have a patch coming up.

-- 
Kalle Valo

Re: [PATCH] Add git-branches-script

From: Miguel Bazdresch <hidden>
Date: 2016-06-15 22:42:04

* Amos Waterland [off-list ref] [2005-08-16 00:48]:
For people whose workflow involves switching back and forth between a
lot of branches, it can be really helpful to be able to quickly tell
which branch you are on and which ones are available.  This patch
introduces a small script that when invoked as `git branches' prints a
list of available branches with a star in front of the one you are on:

 $ cd linux-2.6/
 $ git checkout -b ppc64-cleanups
 $ git checkout -b ppc64-new-devel
 $ git checkout -b ppc64-all-merge
 $ git branches
   master
 * ppc64-all-merge
   ppc64-cleanups
   ppc64-new-devel
Some might find it useful to put Amos' script in a bash function and
then put the current branch in the prompt. What I did was to put this
function in my .bashrc:

gitbranch () {
	. git-sh-setup-script &&
	branch=$(basename $(readlink $GIT_DIR/HEAD)) &&
	echo -n "" $branch ""
}

Then I modified my bash prompt:

export PS1='\[\033[1;31m\]\j$(gitbranch)[\w]\$ \[\033[0m\]'
                            ^^^^^^^^^^^

Now, if my current dir is not a git repo, I have my regular prompt. As
soon as I cd into a git repo, I get the current branch in the prompt, as
follows:

0[~]$ cd gitrepo
0 master [~/gitrepo]$ git checkout branch1
0 branch1 [~/gitrepo]$ cd ..
0[~]$

-- 
Miguel Bazdresch
http://thewizardstower.org/
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help