In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the knowledge of "how to display the Git branch name in the Shell prompt". In the operation of the actual case, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
One of the benefits of Git is that it makes branch management of code extremely convenient. Branches only retain differences, do not copy any files, do not need to connect to the network, create quickly, and delete them as soon as they are used. The Git branch has nothing to do with the complexity of the project. No matter how complex your project is, creating a Git branch is always instantaneous. At the same time, because the information of the parent branch is retained, the merging of branches becomes extremely simple.
When you frequently use multiple branches in a project, you can use the git status command to find out which branch you are working under, but it is hard to avoid losing your mind and forgetting which branch you are under, resulting in misoperation.
There is no doubt that it is much more convenient to display the branches in the Shell prompt, and you no longer need to use the git status command frequently.
The principle of implementation is simple: query the name of the Git branch under the current directory and embed it in the PS1 variable. Then, the Git branch name can be easily obtained from the following script:
The code is as follows:
Git branch-- no-color 2 > / dev/null | sed-e'/ ^ [^ *] / d'-e's Band *\ (. *\) / (\ 1) /'
Package the above script into a function, modify the PS1 variable, and embed the function. It goes something like this. But this also means a problem, that is, every shell activity (such as changing directories, or even just hitting enter) will perform a git. The sed command, which starts two processes at a time, is really a little uncomfortable.
Fortunately, you can get the Git branch name in another way. In each Git project, there is a .git directory under which there is a file called HEAD that contains the path information for the current branch:
The code is as follows:
Ref: refs/heads/BRANCH-NAME
We just need to read the 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 may not be correct.
Here is how Aaron Crane is implemented:
The 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 the PROMPT_COMMAND to ensure that Bash calls this function to get the branch name before creating the prompt:
The code is as follows:
PROMPT_COMMAND= "find_git_branch; $PROMPT_COMMAND"
Finally, just redefine the PS1 variable:
The code is as follows:
# Here is bash color codes you can use
Black=$'\ [\ e [1x 30m\]'
Red=$'\ [\ e [1x 31m\]'
Green=$'\ [\ e [1x 32m\]'
Yellow=$'\ [\ e [1x 33m\]'
Blue=$'\ [\ e [1x 34m\]'
Magenta=$'\ [\ e [1x 35m\]'
Cyan=$'\ [\ e [1x 36m\]'
White=$'\ [\ e [1x 37m\]'
Normal=$'\ [\ e [m\]'
PS1= "$white [$magenta\ u$white@$green\ h$white:$cyan\ w$yellow\ $git_branch$white]\ $normal"
You can put the above code in a file such as ~ / .profile or ~ / .bash_profile. My system is defined by Snow Leopard,PS1 in / etc/bashrc, so I modified this file directly.
The final effect is as follows:
UPDATE-2010-06-23:
If you have installed the git-completion.sh subcommand included with Git to automatically complete the script, you can also use the methods provided by the script:
The code is as follows:
Export PS1= "[\ u@\ h\ W"'$(_ _ git_ps1 "(% s)")'"]\ $"
That's all for "how to display the Git branch name in the Shell prompt". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.