Customize colors for __git_ps1 with GIT_PS1_SHOWCOLORHINTS

后端 未结 2 842
逝去的感伤
逝去的感伤 2021-02-13 19:27

What I have tried

I have updated my prompt to include the branch name using __git_ps1. In addition, I set GIT_PS1_SHOWCOLORHINTS.

Th

2条回答
  •  难免孤独
    2021-02-13 20:04

    I was able to achieve a decent solution by:
    1. Cloning the latest git source, to obtain and install a recent git-prompt.sh (Your distro may already have an up-to-date script)
    2. Removing the check that is stopping the script from inserting color codes in the output string.
    3. Altering my .bashrc to include a call to __git_ps1 with some formatting options to alter my terminal prompt text.

    Commit and documentation, including specific files and edits I made: https://github.com/karlapsite/git/commit/b34d9e8b690ec0b304eb794011938ab49be30204#diff-a43cc261eac6fbcc3578c94c2aa24713R449

    Now, my console has all of the information I wanted: I can open a terminal, and cd into any git repo:
    $ cd ~/Github/git user@hostname:~/Github/git:(master)$ # 'master' is green

    And the when I checkout a hash, and move into a detached head state:
    $ git checkout bca18110 user@hostname:~/Github/git:(bca1811...)$ # the commit hash is red

    I needed to follow this answer: https://stackoverflow.com/a/13997892/4717806 to have bash properly re-interpret the color codes after each command, but my terminal is intact, linewrapping still works, and my prompt is colored the way I wanted!

提交回复
热议问题