Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Customize your Git command line prompt

2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

1, let the BASH command line display the current branch line and the submission status and highlight it with different colors

Effect picture:

After entering the working directory of git, the name of the current branch line is displayed

If there is a version that has been tracked but not temporarily saved, use the red * prompt

If you have tracked uncommitted versions, use the * prompt

The prompt can print the name of the current branch after switching to another branch

Switching to a non-Git working directory does not affect other operations.

The principle of customization: the main thing is to modify the PS1 variable (if you don't know what PS1 is, please know it first).

Modify the bashrc, either globally or for the current user

Function get_git_branch_now {git branch-- no-color 2 > / dev/null | awk'/\ * / {printf "\ 033 [34m [>" $2 "]\ 033 [0m"} 'return 0} function get_git_status_now {git status 2 > / dev/null | grep-Q "Unmerged paths" & & echo-e'\ 033 [41 37m * 033 [0m'& & return 0 # merge conflict status git status 2 > / dev/null | grep-qE "Changes not staged for commit | Changed but not updated" & & echo-e'\ 033 [31m *\ 033 [0m'& & return 0 # non-temporary status git status 2 > / dev/null | grep-Q "Changes to be committed" & & echo-e'\ 033 [33m *\ 033 [0m '& & return 0 # unsubmitted status} ["$PS1" = "\\ s -\\ v\ $] & & PS1="\ [\ e [36m\] [\ u\ [\ e [0m\]\ [\ e [5m\] @\ [\ e [32m\]\ w\ [\ e [36m\]]\ [\ e [0m\]\ $(get_git_branch_now)\ $ (get_git_status_now)\\ $"

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

Update on January 23, 2016, fixed the problem of abnormal operation under os x.

2, make the daily output of Git more friendly, and customize the log format

The default output of git is single color, which is not only not beautiful enough, but also not easy to read. In fact, git itself supports displaying its output in multiple colors, and you can turn on multi-color output by running the following command on the command line to change the settings of git:

Git config-global color.status autogit config-global color.diff autogit config-global color.branch autogit config-global color.interactive auto

After executing the above command, the output of git's status, diff and branch commands are all colored. See the example in the following figure.

Custom log format

After completing the above steps, the output of the git log command is somewhat colored, but still boring (see figure below).

Never mind, the powerful git provides the ability to customize the log format, try entering the following command:

Git log-graph-pretty=format:'%Cred%h%Creset -% C (yellow)% d%Creset% s% Cgreen (% cr)% C (bold blue)% Creset'-- abbrev-commit

You will see output similar to the following:

How's it going? not bad, huh? However, it is not realistic to output such a long list of commands every time you look at log. Let's solve this problem through the command alias of git. 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 blue)% Creset'"

The above command will create a command alias lg, and each time you use the command git lg, you enter the same long list of commands. Now, if you want to see beautiful multi-color output, use git lg, if you want to see normal log output, use git log, the two do not interfere with each other.

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

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

The custom log output format can be achieved by replacing the contents in the single quotation marks after format with the format you want. % h,% ar, etc. Here are some git predefined placeholders. The complete list is as follows:

Git version control management

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report