Scriptim/bash-prompt-generator

Git branch with __git_ps1

Closed this issue · 6 comments

jxu commented

Feature Description

https://stackoverflow.com/questions/15883416/adding-git-branch-on-the-bash-command-prompt

If __git_ps1 is available (unless your git is from 2014 or before), it's shorter and less bash work than PROMPT_COMMAND='PS1_CMD1=$(git branch --show-current 2>/dev/null)'

Note that by default this will automatically generate a space and parens to produce a string like (master), so it's designed to go at the end of a prompt. It can be customized with a %s format string.

Not a duplicate

  • This is not a duplicate

Want to Implement

  • I want to implement this feature

Have you noticed the Advanced Git Prompt element?

jxu commented

Yes, idk why PROMPT_COMMAND is necessary.

Also Idk if it's necessary to source git-prompt.sh. I never added that and I still had __git_ps1.

Yes, idk why PROMPT_COMMAND is necessary.

PROMPT_COMMAND is just a more fool-proof method of using outputs from commands in the prompt, see e.g. #20. I simply use it for every command, including Advanced Git Prompt. The git-prompt.sh script also mentions the usage of PROMPT_COMMAND for a "slightly faster prompt".

Also Idk if it's necessary to source git-prompt.sh. I never added that and I still had __git_ps1.

The file needs to be sourced in order to make the __git_ps1 function available in your shell. This is also stated in the official usage instructions in the git-prompt.sh script. Your git installation might have done that for you but we cannot assume that's the case for everyone.

jxu commented

Those are very old instructions. It now ships with git.

https://askubuntu.com/a/1044097

I have not tested with WSL or windows git bash.

Those are very old instructions. It now ships with git.

askubuntu.com/a/1044097

This might be the case for Ubuntu, but not necessarily for other distributions – e.g. Arch Linux does not ship git-sh-prompt, it seems. Also, the Stack Exchange answer you linked still mentions that the file needs to be sourced in order to make __git_ps1 work.

jxu commented

Ok, I see it is sourced from git bash completion by default on ubuntu and windows git bash. https://git-scm.com/book/en/v2/Appendix-A:-Git-in-Other-Environments-Git-in-Bash