Personalize your Git command-line prompt

Source: Internet
Author: User
Tags using git

1, let the bash command line display the current branch and show the submission status and use different color highlighting to distinguish


650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/7A/07/wKiom1ahAZSStKCoAABFTGH-2c4417.png "title=" 1.png " alt= "Wkiom1ahazsstkcoaabftgh-2c4417.png"/>

After entering the working directory of Git, display the name of the current branch

If there is a version that has been tracked without staging, use the red * hint

If you have a tracked uncommitted version, use the * * * prompt

Switch to a different branch after the prompt can print the current branch name

After switching to a non-git working directory, no other actions are affected.


The principle of customization: The main thing is to modify the PS1 variable (if you do not know what PS1 is, then please understand first).

Modify BASHRC Whether it is global or the current user's

Function get_git_branch_now  {        git branch  --no-color 2> /dev/null | awk  '/*/{print ' [> "$"] "} '}function get_git _status_now {        git status 2> /dev/null  | grep  "Unmerged paths"  > /dev/null  2>&1  & & echo -e  ' \e[41;37m*\e[0m '  &&  return 0              #合并冲突状态          git status 2> /dev/null | grep  "changed but not updated"   > /dev/null  2>&1  && echo -e  ' \e[31m*\e[0m '   &&  return 0       #未暂存状态          git status 2> /dev/null | grep  "changes to be committed"  >  /dev/null  2>&1  && echo -e  ' \e[33m*\e[0m '  & &  return 0       #未提交状态}  [  "$PS 1"  =   "\\s-\\v\\\$ "  ] && ps1= "\[\e[36m\][\u\[\e[0m\]\[\e[5m\]@\[\e[0m\]\h \[ \e[32m\]\w\[\e[36m\]]\[\e[0m\]\[\e[34m\]\$ (Get_git_branch_now) \[\e[0m\]\$ (get_git_status_now) \\$  "

This will show the effect of the picture, of course, my PS1 here is a bit complicated, if you can read the last two variable references, then naturally understand.


2, make Git's daily output more friendly, and custom log format

The default output of Git is a single color, not only beautiful, but also not easy to read. In fact, git natively supports displaying its output in a variety of colors, simply by running the following command on the command line to modify the Git settings to turn on the multi-color output:

git config--global color.status autogit config--global color.diff autogit config--global color.branch autogit config-- Global Color.interactive Auto

After executing these commands, the output of Git's status, diff, and branch commands are all colored. See example.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/7A/06/wKioL1ahB6DhwvEoAAAhIkdlXlI514.png "title=" 2.png " alt= "Wkiol1ahb6dhwveoaaahikdlxli514.png"/>

Custom log Format

After completing the above steps, git log the output of the command appears dull (see), although it has a point color.

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/7A/07/wKiom1ahB6ridxvAAABFxDEwheU771.png "title=" 3.png " alt= "Wkiom1ahb6ridxvaaabfxdewheu771.png"/>

It doesn't matter, the powerful git provides the ability to customize the log format, trying to enter the following command:

git log--graph--pretty=format: '%cred%h%creset-%c (yellow)%d%creset%s%cgreen (%CR)%c (bold blue) <%an>%creset '- -abbrev-commit

You will see a similar output:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/7A/06/wKioL1ahCC3T140pAABp2zElHF4005.png "title=" 1.png " alt= "Wkiol1ahcc3t140paabp2zelhf4005.png"/>

How's that, huh? However, it is not realistic to print such a long list of commands every time you view log. Let's solve this problem by using Git's command alias. Enter the following command:

git config--global alias.lg "log--graph--pretty=format: '%cred%h%creset-%c (yellow)%d%creset%s%cgreen (%CR)%c (bold bl UE) <%an>%creset ' "

The above command creates a command aliaslgEvery time you use the commandgit lgIt is equivalent to entering the long list of commands just now. Now, if you want to see beautiful, multi-color output, usegit lg, if you want to see the normal log output, use thegit log, they do not interfere with each other.

If you want log to output some specific information, you can adjust --pretty the value of the parameter yourself, for example, the following command will show only commit hash, commit time, submitter name:

git log--pretty=format: '%h%ar%an '

You can implement the custom log output format by replacing the contents of the single quotation mark with the format you want. Here %h , and %ar so on are some git pre-defined placeholders, the complete list is as follows:

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/7A/06/wKioL1ahCObyJkwUAAFdjHS_0yQ047.png "title=" 2.png " alt= "Wkiol1ahcobyjkwuaafdjhs_0yq047.png"/>


Git Version control management

This article from "Professor elder brother" blog, reprint please contact the author!

Personalize your Git command-line prompt

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.