# git branch linux **Repository Path**: anxu/git-branch-linux ## Basic Information - **Project Name**: git branch linux - **Description**: linux shell终端, 显示git当前的分支名称 - **Primary Language**: Shell - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2021-01-06 - **Last Updated**: 2021-12-22 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## 介绍 当前用户生效 `~/.bashrc` 文件 全部用户生效 `/etc/bash.bashrc` 文件 打开文件,找到 ```shell if [ "$color_prompt" = yes ]; then PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\W\[\033[00m\]\$ ' else PS1='${debian_chroot:+($debian_chroot)}\u@\h:\W\$ ' fi ``` `[\033[01;32m]`中,01为前景色,32为背景色; `'${debian_chroot:+($debian_chroot)}\u@\h:\W\$ '` ```text 前景色 背景色 30 40 黑色 31 41 红色 32 42 绿色 33 43 黄色 34 44 蓝色 35 45 紫红色 36 46 青蓝色 37 47 白色 ``` ```text \d :代表日期, \H :完整的主机名称 \h :仅取主机的第一个名字 \t :显示时间为24小时格式,如:HH:MM:SS \T :显示时间为12小时格式 \A :显示时间为24小时格式:HH:MM \u :用户名 \v :BASH的版本信息 \w :完整的工作目录名称 \W :列出最后一个目录 \$ :提示字符,如果是root时,提示符为:# ,普通用户则为:$ ``` ## 显示git分支名称 获取分支名称 ```shell git_branch(){ git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/' } ``` 修改,增加分支名称显示,同时换行 `$(git_branch)\n` ```shell if [ "$color_prompt" = yes ]; then PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\W\[\033[00m\]$(git_branch)\n\$ ' else PS1='${debian_chroot:+($debian_chroot)}\u@\h:\W$(git_branch)\n\$ ' fi ``` 最终显示 ``` root@ubuntu:/home/xx (master) $ ``` 修改后,执行`source ~/.bashrc` 生效