Sunday, April 28, 2024
34
rated 0 times [  34] [ 0]  / answers: 1 / hits: 29797  / 3 Years ago, tue, july 13, 2021, 6:05:44

How can I configure the terminal to display the current git branch?



I'd like to see the second line rather than the first:



andy@bob:~/my_projects/project_x$ 
(master)~/my_projects/project_x$


I don't want to have to run git status to see which branch I am currently on anymore!


More From » command-line

 Answers
3

You can add the following code to you .bashrc file:



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) $ "


You can move around these component parts to configure to your tastes, for example to prepend $(parse_git_branch) and not show the user@computer part I used:



PS1="$(parse_git_branch)${debian_chroot:+($debian_chroot)}w$ "


Which displays:



(master)~/my_projects/project_x$ 


See also: What is this PS1 variable doing in .bash_profile file?


[#32941] Tuesday, July 13, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
sconhoney

Total Points: 403
Total Questions: 118
Total Answers: 109

Location: Andorra
Member since Mon, Jan 9, 2023
1 Year ago
sconhoney questions
;