3 ms·
current git branch is missing (helps me to avoid working on the wrong branch). (tried to do it with colors, but things got too messy). I never managed to work w
by MichaelMoser123 5y ago
current git branch is missing (helps me to avoid working on the wrong branch). (tried to do it with colors, but things got too messy). I never managed to work with colors in bash propmts; they look different on each environment.
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
PS1="[\u@\h \W\$(parse_git_branch)]\$ "
- sirn 5y agoYou might want to use `git rev-parse --abbrev-ref HEAD` or `git name-rev --name-only HEAD` instead of `git branch`. Plumbing commands (such as rev-parse or name-ref) are generally more interface-stable to use in a script than porcelain commands (such as git branch) which are designed for interactive use where output may change depending on user environment and configuration.
- MichaelMoser123 5y agomakes sense. thanks!
- NegativeLatency 5y agoI’ve heard this advice given before and initially followed it but ‘git branch’ and parsing the output works in a few cases (I don’t recall what they are at this time) that less hackey approaches don’t.
- sirn 5y agoI think that might be `git symbolic-ref` which doesn't work in detached HEAD state (e.g. when checking out a commit) whereas `git rev-parse --abbrev-ref` will print "HEAD". On the other hand, `git name-rev` will try to find the nearest named ref (including tags) and how far HEAD is from that named ref (e.g. master~1). Internally, symbolic-ref resolve the ref in the same way as git branch, just without handling detached HEAD. __git_ps1 use a combination of rev-parse, symbolic-ref and git describe to provide a more interesting output for displaying state of the working directory. To have the similar output for detached HEAD as git-status (e.g. "HEAD detached at fa0c4e1") then probably `git rev-parse --abbrev-rev` and `git rev-parse --short` should be used together. Unfortunately, while rev-parse can print multiple output in a single command, abbrev-rev and short cannot be used together. In such case, parsing `git status` might indeed be more suitable as it spawn only one executable instead of two. Some porcelain commands do have --porcelain flag to ensure the stability of the interface, though (e.g. `git status --porcelain=v1`).
- paulmac_ie 5y agoThis is my prompt that shows my git branch, AWS_PROFILE etc: if [ "$color_prompt" = yes ]; then PS1='\[\033[0;32m\]\[\033[0m\033[0;32m\]\u\ [\033[0;36m\] @ \[\033[0;36m\]\h \w\ [\033[0;32m\]$(__git_ps1)\n\[\033[0;32m\]└─\ [\033[0m\033[0;32m\] \$\[\033[0m\033[0;32m\] AWS:${AWS_PROFILE} ▶\[\033[0m\] ' else PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ ' fi
- ryenus 5y agoNowadays there's the `--show-current` option: git branch --show-current See https://git-scm.com/docs/git-branch https://git-scm.com/docs/git-branch
- res0nat0r 5y agoThis is the PS1 I've been using for a long time: export PS1="\n\$(kube_ps1)\n\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[0;31m\]\$(__git_ps1)\n\$\[\033[00m\] " Note it needs the git-prompt.sh script (usually comes with the bash-completion package), and the kube-ps1 script: https://github.com/jonmosco/kube-ps1 https://github.com/jonmosco/kube-ps1 It will show you the current kubernetes profile and namespace, user@host, current directory and git branch if you're in a git repo.
- dllthomas 5y agoI recently added a count of stashes (in red) following the branch name, and I've been liking it.