简体   繁体   English

Linux bash中git状态的自动显示

[英]Automatic display of git status in Linux bash

I started working with git in a Windows system.我开始在 Windows 系统中使用 git。 I used the Git Shell that comes with Git Desktop.我使用了 Git Desktop 附带的 Git Shell。 This commandline tool always displays the branch you are currently in and a short, colored form of git status (# of untracked files, # of changed files, # of deleted files).此命令行工具始终显示您当前所在的分支以及简短的彩色 git 状态形式(未跟踪文件数、已更改文件数、已删除文件数)。 I found this really convenient.我发现这真的很方便。

Now I changed my system completely to Linux and I did not find anything similar.现在我将我的系统完全更改为 Linux 并且我没有发现任何类似的东西。 Is there a way to teach the Linux bash displaying the branch and status just like the Windows Git Shell does?有没有办法像 Windows Git Shell 一样教 Linux bash 显示分支和状态? I am currently working with the Xubuntu (16.04) Terminal.我目前正在使用 Xubuntu (16.04) 终端。

According to the GIT-SCM book , and assuming you are using bash , you can use the git-prompt.sh script provided by either git or some other package manager in your distro.根据GIT-SCM book ,并假设您使用的是bash ,您可以使用git-prompt.sh脚本,该脚本由您的发行版中的 git 或其他包管理器提供。

. ~/git-prompt.sh
export GIT_PS1_SHOWDIRTYSTATE=1
export PS1='\w$(__git_ps1 " (%s)")\$ '

TL;DR on Ubuntu 20.04.1 TL; Ubuntu 20.04.1 上的 DR

Add this to your ~/.bashrc :将此添加到您的~/.bashrc

source /etc/bash_completion.d/git-prompt
export GIT_PS1_SHOWDIRTYSTATE=1
export PS1='\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]$(__git_ps1 "(%s)")\$ '

Details详情

This answer is for people who wants to enhance the original prompt provided with Ubuntu 20.04.1.这个答案适用于想要增强 Ubuntu 20.04.1 提供的原始提示的人。

The shell is provided by the git-core package shell 由git-core包提供

source /etc/bash_completion.d/git-prompt

export GIT_PS1_SHOWDIRTYSTATE=1 is needed to display when current state is modified.修改当前状态时需要export GIT_PS1_SHOWDIRTYSTATE=1显示。

The initial prompt is:初始提示是:

export PS1='\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '

So you can enhance it that way by adding the git state at the end:所以你可以通过在最后添加 git state 来增强它:

export PS1='\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]$(__git_ps1 "(%s)")\$ '

I know what you mean.我明白你的意思。 In Windows, after you install Git, you will get Git Bash Here .在 Windows 中,安装 Git 后,您将获得Git Bash Here When you launch this, a terminal will come up and the prompt will have color.当您启动它时,会出现一个终端并且提示会有颜色。 [The prompt that I see is: username@machine MINGW32 /C/Working/GitTutorial (master). 【我看到的提示是:username@machine MINGW32 /C/Working/GitTutorial (master)。 username@machine is in green. username@machine 为绿色。 MINGW32 is in purple. MINGW32 为紫色。 Path is in yellow.路径为黄色。 Branch is in blue.]分支是蓝色的。]

Now if your system is Unix/Linux, yes, when you open a terminal, you can make the color to have color.现在,如果您的系统是 Unix/Linux,是的,当您打开终端时,您可以将颜色设置为有颜色。 Just Google and find 'bash prompt', I get https://wiki.archlinux.org/index.php/Bash/Prompt_customization只需谷歌并找到'bash prompt',我得到https://wiki.archlinux.org/index.php/Bash/Prompt_customization

I am sure with a bit more searching, you can make the prompt in Unix/Linux to look exactly what you are asking.我相信通过更多的搜索,您可以使 Unix/Linux 中的提示完全符合您的要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM