Reputation:
I'm trying to make a small tool (maybe it already exist) to help working with repositories and branches from the terminal.
Basically the idea is to have the name of the branch in the prompt (similar to virtual environments, which also show the current environment in the prompt).
The following command just extracts the name of the branch I'm currently in.
git branch | grep "*" | cut -c3-
Including this in my .bashrc file to modify PS1
works, however only changes the prompt when the console is open.
I want to update the prompt everytime I change the folder cd my_folder
, cd ..
so if I go to another repository, it updates the branch.
So I created an alias for cd
, which now appends this small script after executing the cd
.
However if I use git
to change the branch since I'm not changing the folder the prompt is not inmediately updated.
I can also create an alias for git but I have the impression I'm changing too much things and maybe this is something that already exists.
To avoid reinventing the wheel, does this already exist? Is it a good idea to use it?
Thanks
Upvotes: 0
Views: 4778
Reputation: 163
Add these code lines in your ~/.bashrc or .bash_profile file
cd
command and press the enter in terminal.la
and see, does .bashrc or .bash_profile exist.sudo nano .bashrc
(here you can use
either .bashrc or .bash_profile). I am using .bashrc file hereNow your nano editor will prompt, go to the bottom of your nano editor by pressing down-arrow key and paste the following code lines at the end of your file.
# to add the git branch names
function parse_git_branch () {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
RED="\[\033[01;31m\]"
YELLOW="\[\033[01;33m\]"
GREEN="\[\033[01;32m\]"
BLUE="\[\033[01;34m\]"
NO_COLOR="\[\033[00m\]"
# without host
PS1="$GREEN\u$NO_COLOR:$BLUE\w$YELLOW\$(parse_git_branch)$NO_COLOR\$ "
# with host
# PS1="$GREEN\u@\h$NO_COLOR:$BLUE\w$YELLOW\$(parse_git_branch)$NO_COLOR\$ "
Now press Ctrl + s to save the file and press Ctrl + x to exit from the nano editor.
Now you are done.
now check your git repository it will be shown the branch name with colourful names.
References
https://gist.github.com/danielalvarenga/2df8cabbd6f3041c2378
Upvotes: 1
Reputation: 1961
Try this
=> Clone the a repository as following
git clone --depth=1 https://github.com/Bash-it/bash-it.git ~/.bash_it
=> Install it
~/.bash_it/install.sh
=> reopen the termial
Upvotes: -1
Reputation: 464
First is to check whether you are in a Git repository and then extract with the symbolic-ref command:
function gitbranch
{
if git rev-parse --git-dir &>/dev/null; then
printf "%s" "[$( git symbolic-ref HEAD --short )]"
fi
}
export PS1="\u@\h \[\033[32m\]\w\[\033[33m\]\$(gitbranch)\[\033[00m\] $ "
Add it to your .bashrc file.
Upvotes: 0
Reputation: 159
Add this code in ~/.bashrc. After adding don't forget to use source command.
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
export PS1="\u@\h \[\033[32m\]\w\[\033[33m\]\$(parse_git_branch)\[\033[00m\] $ "
Hope this may help.
Upvotes: 1