-1

I have followed the steps given in the accepted answer in this question - Prepend current git branch in terminal. But even after adding following code to .bashrc and restarting the laptop, I do not see the branch name in terminal. Am I missing something? Do I have to somehow specify in the code as to whats the name of the repo root folder?

parse_git_branch() {
 git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
PS1="${debian_chroot:+($debian_chroot)}\u@\h:\w\$(parse_git_branch) $"
Prim
  • 97

1 Answers1

0

git branch will report only if the current working directory is the repository you want to track.

For instance:

$> pwd
/home/xieerqi
$> git branch
fatal: Not a git repository (or any of the parent directories): .git
$> cd sergrep
$> git branch
* master

You want to add a cd call to that function that will navigate to that directory. Better yet, put the brackets around that command, so that the command is executed in subshell, so your current working directory is not affected. For me, the function can be written as so:

parse_git_branch(){
  # navigate in sub shell to my git repository
  # and execute git branch
  ( cd /home/xieerqi/sergrep; git branch 2> /dev/null | \
   sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/' )
}

And here's how that works in action:

DIR:/xieerqi|14:24|skolodya@ubuntu:
$ source ~/.mkshrc                                                             
DIR:/xieerqi|14:24|skolodya@ubuntu:
$ PS1="$(parse_git_branch)$PS1"                                                
(master)DIR:/xieerqi|14:24|skolodya@ubuntu:
$ echo HELLO ASKUBUNTU
HELLO ASKUBUNTU
(master)DIR:/xieerqi|14:24|skolodya@ubuntu:
$ 
(master)DIR:/xieerqi|14:24|skolodya@ubuntu:
$ typeset -f parse_git_branch
parse_git_branch() {
    ( cd /home/xieerqi/sergrep 
      git branch 2>/dev/null | sed -e "/^[^*]/d" -e "s/* \\(.*\\)/(\\1)/" ) 
}