script to show git branch in bash no longer works on ubuntu 18.04
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgO9GURib1T8z7lCwjOGLQaGtrueEthgQ8LO42ZX8cOfTqDK4jvDDpKkLFwf2J49kYCMNW7d4ABih_XCb_2UXdq5fPJDkoyg7-8g_YfRUot-XnaXkNYycsNp7lA5_TW9td0FFpLQ2APzKcZ/s1600/1.jpg)
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYQ0N5W1qAOxLP7t7iOM6O6AzbZnkXUy16s7P_CWfOb5UbTQY_aDsc727chyphenhyphen5W4IppVNernMMQeaUFTB_rFzAd95_CDt-tnwN-nBx6JyUp2duGjPaL5-VgNO41AVsA_vu30EJcipdDG409/s400/Clash+Royale+CLAN+TAG%2523URR8PPP.png)
up vote
3
down vote
favorite
In my 16.04 installation I put this in my ~/.bashrc
file:
#Show git branch in commandline
parse_git_branch()
git branch 2> /dev/null
export PS1="$debian_chroot:+($debian_chroot)[33[01;32m]u:[33[01;34m]w[33[00m]$(parse_git_branch)[33[00m] $ "
It showed a prompt like this:
user:~/myrepo (master) $
But when I do the same on 18.04 the prompt looks like this instead:
user:~/myrepo $
How do I get it to work in 18.04?
$ printf "%qn" "$PS1" "$PROMPT_COMMAND" "$0" "$SHELL"
\[\033[01;32m\]\u:\[\033[01;34m\]\w\[\033[00m\]$(parse_git_branch)\[\033[00m\] $
''
bash
/bin/bash
IâÂÂm using sed (GNU sed) 4.4
and GNU bash, version 4.4.19(1)-release (x86_64-pc-linux-gnu)
. This is a "fresh" installation, so there is nothing additional in .bashrc
.
bash git bashrc prompt
add a comment |Â
up vote
3
down vote
favorite
In my 16.04 installation I put this in my ~/.bashrc
file:
#Show git branch in commandline
parse_git_branch()
git branch 2> /dev/null
export PS1="$debian_chroot:+($debian_chroot)[33[01;32m]u:[33[01;34m]w[33[00m]$(parse_git_branch)[33[00m] $ "
It showed a prompt like this:
user:~/myrepo (master) $
But when I do the same on 18.04 the prompt looks like this instead:
user:~/myrepo $
How do I get it to work in 18.04?
$ printf "%qn" "$PS1" "$PROMPT_COMMAND" "$0" "$SHELL"
\[\033[01;32m\]\u:\[\033[01;34m\]\w\[\033[00m\]$(parse_git_branch)\[\033[00m\] $
''
bash
/bin/bash
IâÂÂm using sed (GNU sed) 4.4
and GNU bash, version 4.4.19(1)-release (x86_64-pc-linux-gnu)
. This is a "fresh" installation, so there is nothing additional in .bashrc
.
bash git bashrc prompt
@PerlDuck Please add one or even two answers!!!
â dessert
Jun 5 at 11:22
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
In my 16.04 installation I put this in my ~/.bashrc
file:
#Show git branch in commandline
parse_git_branch()
git branch 2> /dev/null
export PS1="$debian_chroot:+($debian_chroot)[33[01;32m]u:[33[01;34m]w[33[00m]$(parse_git_branch)[33[00m] $ "
It showed a prompt like this:
user:~/myrepo (master) $
But when I do the same on 18.04 the prompt looks like this instead:
user:~/myrepo $
How do I get it to work in 18.04?
$ printf "%qn" "$PS1" "$PROMPT_COMMAND" "$0" "$SHELL"
\[\033[01;32m\]\u:\[\033[01;34m\]\w\[\033[00m\]$(parse_git_branch)\[\033[00m\] $
''
bash
/bin/bash
IâÂÂm using sed (GNU sed) 4.4
and GNU bash, version 4.4.19(1)-release (x86_64-pc-linux-gnu)
. This is a "fresh" installation, so there is nothing additional in .bashrc
.
bash git bashrc prompt
In my 16.04 installation I put this in my ~/.bashrc
file:
#Show git branch in commandline
parse_git_branch()
git branch 2> /dev/null
export PS1="$debian_chroot:+($debian_chroot)[33[01;32m]u:[33[01;34m]w[33[00m]$(parse_git_branch)[33[00m] $ "
It showed a prompt like this:
user:~/myrepo (master) $
But when I do the same on 18.04 the prompt looks like this instead:
user:~/myrepo $
How do I get it to work in 18.04?
$ printf "%qn" "$PS1" "$PROMPT_COMMAND" "$0" "$SHELL"
\[\033[01;32m\]\u:\[\033[01;34m\]\w\[\033[00m\]$(parse_git_branch)\[\033[00m\] $
''
bash
/bin/bash
IâÂÂm using sed (GNU sed) 4.4
and GNU bash, version 4.4.19(1)-release (x86_64-pc-linux-gnu)
. This is a "fresh" installation, so there is nothing additional in .bashrc
.
bash git bashrc prompt
edited Jun 5 at 15:41
asked Jun 4 at 20:14
Felix Rosén
164111
164111
@PerlDuck Please add one or even two answers!!!
â dessert
Jun 5 at 11:22
add a comment |Â
@PerlDuck Please add one or even two answers!!!
â dessert
Jun 5 at 11:22
@PerlDuck Please add one or even two answers!!!
â dessert
Jun 5 at 11:22
@PerlDuck Please add one or even two answers!!!
â dessert
Jun 5 at 11:22
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
4
down vote
Although you already solved the problem, I'd like to introduce the solution already shipped with git:
Together with git the file /usr/lib/git-core/git-sh-prompt
gets installed. Years ago it was in the contrib directory but meanwhile it made it into the official paths. The file isn't meant to be executed (in fact, it doesn't have x
flags) but rather to be sourced from the .bashrc
. It defines some functions to handle a pretty prompt, the most prominent being __git_ps1
.
You can add the following lines to your .bashrc
:
source /usr/lib/git-core/git-sh-prompt
export PS1="$debian_chroot:+($debian_chroot)[33[01;32m]u:[33[01;34m]w[33[00m]$(__git_ps1)[33[00m] $ "
The top of the file /usr/lib/git-core/git-sh-prompt
gives some description of what else can be done. My version says 1) Copy this file to somewhere (e.g. ~/.git-prompt.sh).
but this is not necessary (the instruction is obsolete).
You may also set some environment variables to alter the function's behaviour. I, for instance, use
export GIT_PS1_SHOWDIRTYSTATE=yes
The result then looks like:
# clean
pduck:~/oss/rsyslog (master) $
or
# dirty
pduck:~/oss/rsyslog (master *) $
or
# something added to index
pduck:~/oss/rsyslog (master +) $
or
# in a completely fresh repo
pduck:~/oss/xxx (master #) $
Btw: The "proper" way to get the current branch is not to parse git branch
's output as that may change with new git versions.git branch
is a so-called porcelain command which means its output is nice and pretty but not guaranteed to stay the same with different versions. The git guys thus recommend plumbing tools for scripting. With plumbing the current branch can be determined with
if [ "$(git rev-parse --is-inside-work-tree 2>/dev/null)" = "true" ]; then
current_branch=$(git symbolic-ref --quiet --short HEAD || git rev-parse --short HEAD)
else
current_branch=""
fi
The first assignment works if HEAD is a symbolic reference (that is: a branch or tag is checked out). If you checked out something that has no label (e.g. git checkout HEAD^^
), then it fails and we use git rev-parse --short HEAD
instead to show the SHA1. The if
checks whether we are in a git working directory at all (because else the commands don't make any sense).
add a comment |Â
up vote
2
down vote
accepted
The solution was quite simple.
I set an upstream, staged a commit and pushed it. After that it worked.
I thought that the script somehow used the .git
folder that is generated with git init
. Since it works on branches that does not have an upstream.
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
Although you already solved the problem, I'd like to introduce the solution already shipped with git:
Together with git the file /usr/lib/git-core/git-sh-prompt
gets installed. Years ago it was in the contrib directory but meanwhile it made it into the official paths. The file isn't meant to be executed (in fact, it doesn't have x
flags) but rather to be sourced from the .bashrc
. It defines some functions to handle a pretty prompt, the most prominent being __git_ps1
.
You can add the following lines to your .bashrc
:
source /usr/lib/git-core/git-sh-prompt
export PS1="$debian_chroot:+($debian_chroot)[33[01;32m]u:[33[01;34m]w[33[00m]$(__git_ps1)[33[00m] $ "
The top of the file /usr/lib/git-core/git-sh-prompt
gives some description of what else can be done. My version says 1) Copy this file to somewhere (e.g. ~/.git-prompt.sh).
but this is not necessary (the instruction is obsolete).
You may also set some environment variables to alter the function's behaviour. I, for instance, use
export GIT_PS1_SHOWDIRTYSTATE=yes
The result then looks like:
# clean
pduck:~/oss/rsyslog (master) $
or
# dirty
pduck:~/oss/rsyslog (master *) $
or
# something added to index
pduck:~/oss/rsyslog (master +) $
or
# in a completely fresh repo
pduck:~/oss/xxx (master #) $
Btw: The "proper" way to get the current branch is not to parse git branch
's output as that may change with new git versions.git branch
is a so-called porcelain command which means its output is nice and pretty but not guaranteed to stay the same with different versions. The git guys thus recommend plumbing tools for scripting. With plumbing the current branch can be determined with
if [ "$(git rev-parse --is-inside-work-tree 2>/dev/null)" = "true" ]; then
current_branch=$(git symbolic-ref --quiet --short HEAD || git rev-parse --short HEAD)
else
current_branch=""
fi
The first assignment works if HEAD is a symbolic reference (that is: a branch or tag is checked out). If you checked out something that has no label (e.g. git checkout HEAD^^
), then it fails and we use git rev-parse --short HEAD
instead to show the SHA1. The if
checks whether we are in a git working directory at all (because else the commands don't make any sense).
add a comment |Â
up vote
4
down vote
Although you already solved the problem, I'd like to introduce the solution already shipped with git:
Together with git the file /usr/lib/git-core/git-sh-prompt
gets installed. Years ago it was in the contrib directory but meanwhile it made it into the official paths. The file isn't meant to be executed (in fact, it doesn't have x
flags) but rather to be sourced from the .bashrc
. It defines some functions to handle a pretty prompt, the most prominent being __git_ps1
.
You can add the following lines to your .bashrc
:
source /usr/lib/git-core/git-sh-prompt
export PS1="$debian_chroot:+($debian_chroot)[33[01;32m]u:[33[01;34m]w[33[00m]$(__git_ps1)[33[00m] $ "
The top of the file /usr/lib/git-core/git-sh-prompt
gives some description of what else can be done. My version says 1) Copy this file to somewhere (e.g. ~/.git-prompt.sh).
but this is not necessary (the instruction is obsolete).
You may also set some environment variables to alter the function's behaviour. I, for instance, use
export GIT_PS1_SHOWDIRTYSTATE=yes
The result then looks like:
# clean
pduck:~/oss/rsyslog (master) $
or
# dirty
pduck:~/oss/rsyslog (master *) $
or
# something added to index
pduck:~/oss/rsyslog (master +) $
or
# in a completely fresh repo
pduck:~/oss/xxx (master #) $
Btw: The "proper" way to get the current branch is not to parse git branch
's output as that may change with new git versions.git branch
is a so-called porcelain command which means its output is nice and pretty but not guaranteed to stay the same with different versions. The git guys thus recommend plumbing tools for scripting. With plumbing the current branch can be determined with
if [ "$(git rev-parse --is-inside-work-tree 2>/dev/null)" = "true" ]; then
current_branch=$(git symbolic-ref --quiet --short HEAD || git rev-parse --short HEAD)
else
current_branch=""
fi
The first assignment works if HEAD is a symbolic reference (that is: a branch or tag is checked out). If you checked out something that has no label (e.g. git checkout HEAD^^
), then it fails and we use git rev-parse --short HEAD
instead to show the SHA1. The if
checks whether we are in a git working directory at all (because else the commands don't make any sense).
add a comment |Â
up vote
4
down vote
up vote
4
down vote
Although you already solved the problem, I'd like to introduce the solution already shipped with git:
Together with git the file /usr/lib/git-core/git-sh-prompt
gets installed. Years ago it was in the contrib directory but meanwhile it made it into the official paths. The file isn't meant to be executed (in fact, it doesn't have x
flags) but rather to be sourced from the .bashrc
. It defines some functions to handle a pretty prompt, the most prominent being __git_ps1
.
You can add the following lines to your .bashrc
:
source /usr/lib/git-core/git-sh-prompt
export PS1="$debian_chroot:+($debian_chroot)[33[01;32m]u:[33[01;34m]w[33[00m]$(__git_ps1)[33[00m] $ "
The top of the file /usr/lib/git-core/git-sh-prompt
gives some description of what else can be done. My version says 1) Copy this file to somewhere (e.g. ~/.git-prompt.sh).
but this is not necessary (the instruction is obsolete).
You may also set some environment variables to alter the function's behaviour. I, for instance, use
export GIT_PS1_SHOWDIRTYSTATE=yes
The result then looks like:
# clean
pduck:~/oss/rsyslog (master) $
or
# dirty
pduck:~/oss/rsyslog (master *) $
or
# something added to index
pduck:~/oss/rsyslog (master +) $
or
# in a completely fresh repo
pduck:~/oss/xxx (master #) $
Btw: The "proper" way to get the current branch is not to parse git branch
's output as that may change with new git versions.git branch
is a so-called porcelain command which means its output is nice and pretty but not guaranteed to stay the same with different versions. The git guys thus recommend plumbing tools for scripting. With plumbing the current branch can be determined with
if [ "$(git rev-parse --is-inside-work-tree 2>/dev/null)" = "true" ]; then
current_branch=$(git symbolic-ref --quiet --short HEAD || git rev-parse --short HEAD)
else
current_branch=""
fi
The first assignment works if HEAD is a symbolic reference (that is: a branch or tag is checked out). If you checked out something that has no label (e.g. git checkout HEAD^^
), then it fails and we use git rev-parse --short HEAD
instead to show the SHA1. The if
checks whether we are in a git working directory at all (because else the commands don't make any sense).
Although you already solved the problem, I'd like to introduce the solution already shipped with git:
Together with git the file /usr/lib/git-core/git-sh-prompt
gets installed. Years ago it was in the contrib directory but meanwhile it made it into the official paths. The file isn't meant to be executed (in fact, it doesn't have x
flags) but rather to be sourced from the .bashrc
. It defines some functions to handle a pretty prompt, the most prominent being __git_ps1
.
You can add the following lines to your .bashrc
:
source /usr/lib/git-core/git-sh-prompt
export PS1="$debian_chroot:+($debian_chroot)[33[01;32m]u:[33[01;34m]w[33[00m]$(__git_ps1)[33[00m] $ "
The top of the file /usr/lib/git-core/git-sh-prompt
gives some description of what else can be done. My version says 1) Copy this file to somewhere (e.g. ~/.git-prompt.sh).
but this is not necessary (the instruction is obsolete).
You may also set some environment variables to alter the function's behaviour. I, for instance, use
export GIT_PS1_SHOWDIRTYSTATE=yes
The result then looks like:
# clean
pduck:~/oss/rsyslog (master) $
or
# dirty
pduck:~/oss/rsyslog (master *) $
or
# something added to index
pduck:~/oss/rsyslog (master +) $
or
# in a completely fresh repo
pduck:~/oss/xxx (master #) $
Btw: The "proper" way to get the current branch is not to parse git branch
's output as that may change with new git versions.git branch
is a so-called porcelain command which means its output is nice and pretty but not guaranteed to stay the same with different versions. The git guys thus recommend plumbing tools for scripting. With plumbing the current branch can be determined with
if [ "$(git rev-parse --is-inside-work-tree 2>/dev/null)" = "true" ]; then
current_branch=$(git symbolic-ref --quiet --short HEAD || git rev-parse --short HEAD)
else
current_branch=""
fi
The first assignment works if HEAD is a symbolic reference (that is: a branch or tag is checked out). If you checked out something that has no label (e.g. git checkout HEAD^^
), then it fails and we use git rev-parse --short HEAD
instead to show the SHA1. The if
checks whether we are in a git working directory at all (because else the commands don't make any sense).
edited Jun 6 at 10:39
answered Jun 6 at 10:04
![](https://i.stack.imgur.com/kCmqs.png?s=32&g=1)
![](https://i.stack.imgur.com/kCmqs.png?s=32&g=1)
PerlDuck
3,62911030
3,62911030
add a comment |Â
add a comment |Â
up vote
2
down vote
accepted
The solution was quite simple.
I set an upstream, staged a commit and pushed it. After that it worked.
I thought that the script somehow used the .git
folder that is generated with git init
. Since it works on branches that does not have an upstream.
add a comment |Â
up vote
2
down vote
accepted
The solution was quite simple.
I set an upstream, staged a commit and pushed it. After that it worked.
I thought that the script somehow used the .git
folder that is generated with git init
. Since it works on branches that does not have an upstream.
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
The solution was quite simple.
I set an upstream, staged a commit and pushed it. After that it worked.
I thought that the script somehow used the .git
folder that is generated with git init
. Since it works on branches that does not have an upstream.
The solution was quite simple.
I set an upstream, staged a commit and pushed it. After that it worked.
I thought that the script somehow used the .git
folder that is generated with git init
. Since it works on branches that does not have an upstream.
answered Jun 5 at 15:44
Felix Rosén
164111
164111
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1043610%2fscript-to-show-git-branch-in-bash-no-longer-works-on-ubuntu-18-04%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
@PerlDuck Please add one or even two answers!!!
â dessert
Jun 5 at 11:22