How to display Git branch names in the shell prompt _linux Shell

Source: Internet
Author: User

One of the benefits of Git is that the branch management of the code becomes an extremely convenient thing, the branch only retains the difference, does not have to copy any files, does not connect the network, quickly creates, uses is deleted. The GIT branch has nothing to do with the complexity of the project, no matter how complicated your project is, creating a git branch is always an instant thing. At the same time, because the information of the parent class branch is preserved, the merging of branches becomes very simple.

When more than one branch is used frequently in a project, you can use the git status command to query which branch you are currently working under, but it is inevitable that when your brain is faint, you forget which branch you are under, and thus have a cup of misoperation.

Then it is no doubt convenient to display the branches in the Shell prompt, no longer need to use the git status command frequently ...

The implementation principle is simple, basically querying the name of the Git branch under the current directory and embedding it in the PS1 variable. Then, the Git branch name can be easily obtained by using the following script:

Copy Code code as follows:

Git branch--no-color 2>/dev/null | Sed-e '/^[^*]/d '-e ' s/* \ (. *\)/(\1)/'

Encapsulate the above script into a function, modify the PS1 variable, embed the function ... This is generally the case. But it also means that every shell activity (such as switching directories, or even just knocking down a carriage return) performs a git ... sed command, so it's a bit of a bummer to start 2 processes at a time.

Fortunately, you can get the GIT branch name in another way, and in each Git project, there is a. git directory, which has a file called Head, which contains the path information for the current branch:

Copy Code code as follows:

Ref:refs/heads/branch-name

All we have to do is read the file and then match the path to each other to know the correct branch name. Don't simply split the last branch-name from the head content, because it's not necessarily correct.

Here's how Aaron Crane is implemented:

Copy Code code as follows:

# # Parses out of the branch name from. Git/head:
Find_git_branch () {
Local dir=. Head
Until ["$dir"-ef/]; Todo
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_command to ensure that Bash calls this function to get the branch name before creating PROMPT:
Copy Code code as follows:

Prompt_command= "Find_git_branch; $PROMPT _command "

Finally, just redefine the PS1 variable:

Copy Code code as follows:

# Here's bash color codes 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 \u$white@ $green \h$white: $cyan \w$yellow\ $git _branch$white]\$ $normal"

The above code you can put in ~/.profile or ~/.bash_profile file, my system is Snow Leopard,ps1 defined in/ETC/BASHRC, so I directly modify this file.

The final effect is as follows:

UPDATE–2010/06/23:

If you install the git-completion.sh script that comes with Git, you can also use the method provided by the script:

Copy Code code as follows:

Export ps1= "[\u@\h \w" ' $ (__git_ps1 "(%s)") ']\$ '

Ubuntu system, please refer to:/etc/bash_completion.d/git

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.