Method to display git branch name in shell prompt

Time:2022-5-6

One of the benefits of Git is that it makes the branch management of code an extremely convenient thing. Branches only keep differences, do not need to copy any files, do not need to connect to the network, create quickly, and delete when used up. Git branch has nothing to do with the complexity of the project. No matter how complex your project is, creating git branch is always an instant. At the same time, because the information of parent class branches is retained, the merging of branches becomes extremely simple.

When multiple branches are frequently used in a project, you can use the GIT status command to query which branch you are working under. However, it is inevitable that when you are dizzy, you forget which branch you are under, so there are cups such as misoperation.

So it’s undoubtedly a lot easier to display the branches in the shell prompt. There’s no need to use the GIT status command frequently anymore

The implementation principle is very simple, which is to query the GIT branch name under the current directory, and then embed it into the PS1 variable. Then, the GIT branch name can be easily obtained through the following script:

Copy codeThe code is as follows:
git branch –no-color 2> /dev/null | sed -e ‘/^[^*]/d’ -e ‘s/* \(.*\)/(\1)/’

Encapsulate the above script into the function, modify the PS1 variable and embed the function… Generally. But this also means a problem, that is, every time the shell activity (such as switching directories, or even just typing enter) will execute the GIT… Sed command, so that two processes are started every time. It’s really a little uncomfortable.

Fortunately, you can use another way to get the GIT branch name. There is one in each git project Git directory. Under this directory is a file called head, which contains the path information of the current branch:

Copy codeThe code is as follows:
ref: refs/heads/BRANCH-NAME

We just need to read this file and match it with the corresponding path to know the correct branch name. Don’t simply split the last branch-name from the head content, because it doesn’t have to be correct.

The following is how Aaron crane is implemented:

Copy codeThe code is as follows:
## Parses out the branch name from .git/HEAD:
find_git_branch () {
    local dir=. head
    until [ “$dir” -ef / ]; do
        if [ -f “$dir/.git/HEAD” ]; then
            head=$(< “$dir/.git/HEAD”)
            if [[ $head = ref:\ refs/heads/* ]]; then
                git_branch=” → ${head#*/*/}”
            elif [[ $head != ” ]]; then
                git_branch=” → (detached)”
            else
                git_branch=” → (unknow)”
            fi
            return
        fi
        dir=”../$dir”
    done
    git_branch=”
}

Next, add this function to prompt_ In command, make sure bash calls this function to get the branch name before creating prompt:

Copy codeThe code is as follows:
PROMPT_COMMAND=”find_git_branch; $PROMPT_COMMAND”

Finally, just redefine the PS1 variable:

Copy codeThe code is as follows:
# Here is bash color codes you can use
  black=$’\[\e[1;30m\]’
    red=$’\[\e[1;31m\]’
  green=$’\[\e[1;32m\]’
 yellow=$’\[\e[1;33m\]’
   blue=$’\[\e[1;34m\]’
magenta=$’\[\e[1;35m\]’
   cyan=$’\[\e[1;36m\]’
  white=$’\[\e[1;37m\]’
 normal=$’\[\e[m\]’
 
PS1=”$white[$magenta\[email protected]$green\h$white:$cyan\w$yellow\$git_branch$white]\$ $normal”

You can put the above code in ~ / Profile or ~ / bash_ Profile and other files. My system is snow leopard, and PS1 is defined in / etc / bashrc, so I directly modify this file.

The final effect is as follows:

UPDATE – 2010/06/23:

If you installed git completion. Com, which comes with GIT The SH subcommand automatically completes the script, or you can use the methods provided by the script:

Copy codeThe code is as follows:
export PS1=”[\[email protected]\h \W”‘$(__git_ps1 ” (%s)”)'”]\$ “

For Ubuntu system, please refer to: / etc / bash_ completion. d/git

Recommended Today

Module 1 basic knowledge of go language – source code file of database

Have you ever written small commands (or microprograms) in go? When you are writing “Hello, world”, a source file is enough. Although this gadget is useless, it can give you a little sense of achievement at most. If you are not satisfied with this, don’t worry and follow suit. I’m sure you can also write […]