Ich habe mir einige der Team Treehouse-Videos angesehen und sie haben ein sehr schön aussehendes Terminal, wenn sie mit Git arbeiten.
Zum Beispiel haben sie (etwas ähnliches):
[email protected]: [/Work/test - feature-branch-name] $ git add .
[email protected]: [/Work/test - feature-branch-name] $ git commit -m "Some feature."
[email protected]: [/Work/test - feature-branch-name] $ git checkout master
[email protected]: [/Work/test - master] $ git status
Wie kann mir mein Terminal nützliche Informationen über den Zweig anzeigen, in dem ich bin, mit Farben, um die gewünschten Daten zu unterscheiden? Gibt es ein De-facto-Plugin, das ich noch nicht gefunden habe?
Ich verwende Mac OSX 10.8
Es geht nicht um ein Plugin. Es geht um schnelle Tricks in der Shell.
Um ein cooles Setup in bash zu erhalten, schaut euch das dotfiles
-Projekt dieses Mannes an:
https://github.com/mathiasbynens/dotfiles
Um eine tolle Aufforderung zu erhalten, fügen Sie den .bash_Prompt
in Ihren ~/.bash_profile
oder ~/.bashrc
ein.
Um dieselbe Aufforderung wie in Ihrer Frage zu erhalten, ändern Sie die Zeile export PS1
am Ende von .bash_Prompt
wie folgt:
export PS1="\[${BOLD}${Magenta}\]\u\[$WHITE\]@\[$ORANGE\]\h\[$WHITE\]: [\[$GREEN\]\w\[$WHITE\]\$([[ -n \$(git branch 2> /dev/null) ]] && echo \" - \")\[$PURPLE\]\$(parse_git_branch)\[$WHITE\]] \$ \[$RESET\]"
Ich habe am Ende vor einem Monat alle .bash*
-Dateien aus diesem Repository verwendet, und das war wirklich nützlich für mich.
Für Git gibt es zusätzliche Goodies in .gitconfig
.
Und da Sie ein Mac-Benutzer sind, gibt es in .osx
noch mehr Goodies.
Einfacher Weg
Öffnen Sie ~/.bash_profile
in Ihrem bevorzugten Editor und fügen Sie den folgenden Inhalt unten hinzu.
Git-Zweig in Prompt.
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
export PS1="\[email protected]\h \[\033[32m\]\w - \$(parse_git_branch)\[\033[00m\] $ "
Um die vorhandenen großartigen Antworten zu erweitern, können Sie mit dem Open-Source-Projekt Dotfiles ein einfach aussehendes Terminal erstellen.
https://github.com/mathiasbynens/dotfiles
Die Installation ist unter OSX und Linux denkbar einfach. Führen Sie den folgenden Befehl in Terminal aus.
git clone https://github.com/mathiasbynens/dotfiles.git && cd dotfiles && source bootstrap.sh
Dies wird zu:
cd
in den Ordner.Meine Ansage beinhaltet:
rsync
- style [email protected]:pathname
für das Kopieren und EinfügenBeispiel: Fügen Sie dazu Ihrem
~/.bashrc
Folgendes hinzu:
#
# Set the Prompt #
#
# Select git info displayed, see /usr/lib/git-core/git-sh-Prompt for more
export GIT_PS1_SHOWDIRTYSTATE=1 # '*'=unstaged, '+'=staged
export GIT_PS1_SHOWSTASHSTATE=1 # '$'=stashed
export GIT_PS1_SHOWUNTRACKEDFILES=1 # '%'=untracked
export GIT_PS1_SHOWUPSTREAM="verbose" # 'u='=no difference, 'u+1'=ahead by 1 commit
export GIT_PS1_STATESEPARATOR='' # No space between branch and index status
export GIT_PS1_DESCRIBE_STYLE="describe" # detached HEAD style:
# contains relative to newer annotated tag (v1.6.3.2~35)
# branch relative to newer tag or branch (master~4)
# describe relative to older annotated tag (v1.6.3.1-13-gdd42c2f)
# default exactly eatching tag
# Check if we support colours
__colour_enabled() {
local -i colors=$(tput colors 2>/dev/null)
[[ $? -eq 0 ]] && [[ $colors -gt 2 ]]
}
unset __colourise_Prompt && __colour_enabled && __colourise_Prompt=1
__set_bash_Prompt()
{
local exit="$?" # Save the exit status of the last command
# PS1 is made from $PreGitPS1 + <git-status> + $PostGitPS1
local PreGitPS1="${debian_chroot:+($debian_chroot)}"
local PostGitPS1=""
if [[ $__colourise_Prompt ]]; then
export GIT_PS1_SHOWCOLORHINTS=1
# Wrap the colour codes between \[ and \], so that
# bash counts the correct number of characters for line wrapping:
local Red='\[\e[0;31m\]'; local BRed='\[\e[1;31m\]'
local Gre='\[\e[0;32m\]'; local BGre='\[\e[1;32m\]'
local Yel='\[\e[0;33m\]'; local BYel='\[\e[1;33m\]'
local Blu='\[\e[0;34m\]'; local BBlu='\[\e[1;34m\]'
local Mag='\[\e[0;35m\]'; local BMag='\[\e[1;35m\]'
local Cya='\[\e[0;36m\]'; local BCya='\[\e[1;36m\]'
local Whi='\[\e[0;37m\]'; local BWhi='\[\e[1;37m\]'
local None='\[\e[0m\]' # Return to default colour
# No username and bright colour if root
if [[ ${EUID} == 0 ]]; then
PreGitPS1+="$BRed\h "
else
PreGitPS1+="$Red\[email protected]\h$None:"
fi
PreGitPS1+="$Blu\w$None"
else # No colour
# Sets Prompt like: [email protected]:~/prj/sample_app
unset GIT_PS1_SHOWCOLORHINTS
PreGitPS1="${debian_chroot:+($debian_chroot)}\[email protected]\h:\w"
fi
# Now build the part after git's status
# Highlight non-standard exit codes
if [[ $exit != 0 ]]; then
PostGitPS1="$Red[$exit]"
fi
# Change colour of Prompt if root
if [[ ${EUID} == 0 ]]; then
PostGitPS1+="$BRed"'\$ '"$None"
else
PostGitPS1+="$Mag"'\$ '"$None"
fi
# Set PS1 from $PreGitPS1 + <git-status> + $PostGitPS1
__git_ps1 "$PreGitPS1" "$PostGitPS1" '(%s)'
# echo '$PS1='"$PS1" # debug
# defaut Linux Mint 17.2 user Prompt:
# PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\[email protected]\h\[\033[01;34m\] \w\[\033[00m\] $(__git_ps1 "(%s)") \$ '
}
# This tells bash to reinterpret PS1 after every command, which we
# need because __git_ps1 will return different text and colors
Prompt_COMMAND=__set_bash_Prompt
Das auf Ihrem System installierte git-Paket enthält bash-Dateien, die Sie beim Erstellen einer informativen Aufforderung unterstützen. Um Farben zu erstellen, müssen Sie Terminal-Escape-Sequenzen in Ihre Eingabeaufforderung einfügen. Die letzte Komponente besteht darin, Ihre Eingabeaufforderung zu aktualisieren, nachdem jeder Befehl ausgeführt wurde, indem die integrierte Variable Prompt_COMMAND verwendet wird.
Bearbeiten Sie Ihre ~/.bashrc so, dass sie Folgendes enthält, und Sie sollten die Aufforderung in Ihrer Frage erhalten, modulo einige Farbunterschiede.
#
# Git provides a bash file to create an informative Prompt. This is its standard
# location on Linux. On Mac, you should be able to find it under your Git
# installation. If you are unable to find the file, I have a copy of it on my GitHub.
#
# https://github.com/chadversary/home/blob/42cf697ba69d4d474ca74297cdf94186430f1384/.config/kiwi-profile/40-git-Prompt.sh
#
source /usr/share/git/completion/git-Prompt.sh
#
# Next, we need to define some terminal escape sequences for colors. For a fuller
# list of colors, and an example how to use them, see my bash color file on my GitHub
# and my coniguration for colored man pages.
#
# https://github.com/chadversary/home/blob/42cf697ba69d4d474ca74297cdf94186430f1384/.config/kiwi-profile/10-colors.sh
# https://github.com/chadversary/home/blob/42cf697ba69d4d474ca74297cdf94186430f1384/.config/kiwi-profile/40-less.sh
#
color_start='\e['
color_end='m'
color_reset='\e[0m'
color_bg_blue='44'
#
# To get a fancy git Prompt, it's not sufficient to set PS1. Instead, we set Prompt_COMMAND,
# a built in Bash variable that gets evaluated before each render of the Prompt.
#
export Prompt_COMMAND="PS1=\"\${color_start}\${color_bg_blue}\${color_end}\[email protected]\h [\w\$(__git_ps1 \" - %s\")]\${color_reset}\n\$ \""
#
# If you find that the working directory that appears in the Prompt is ofter too long,
# then trim it.
#
export Prompt_DIRTRIM=3
Es gibt viele PS1-Generatoren, aber ezprompt hat den git-Status (2. Registerkarte 'Statuselemente').
Aufgrund der Antwort von 6LYTH3 habe ich beschlossen, meine eigenen zu veröffentlichen, da einige Verbesserungen nützlich sein könnten:
Einfache Lösung
Öffnen Sie ~/.bash_profile
und fügen Sie den folgenden Inhalt hinzu
# \[\e[0m\] resets the color to default color
reset_color='\[\e[0m\]'
# \[\033[33m\] sets the color to yellow
path_color='\[\033[33m\]'
# \e[0;32m\ sets the color to green
git_clean_color='\[\e[0;32m\]'
# \e[0;31m\ sets the color to red
git_dirty_color='\[\e[0;31m\]'
# determines if the git branch you are on is clean or dirty
git_Prompt ()
{
# Is this a git directory?
if ! git rev-parse --git-dir > /dev/null 2>&1; then
return 0
fi
# Grab working branch name
git_branch=$(git branch 2>/dev/null| sed -n '/^\*/s/^\* //p')
# Clean or dirty branch
if git diff --quiet 2>/dev/null >&2; then
git_color="${git_clean_color}"
else
git_color="${git_dirty_color}"
fi
echo " [$git_color$git_branch${reset_color}]"
}
export PS1="${path_color}\w\[\e[0m\]$(git_Prompt)\n"
Das sollte:
1) Prompt the path you're in, in color: path_color.
2) Tell you which branch are you.
3) Color the name of the branch based on the status of the branch with git_clean_color
for a clean work directory and git_dirty_color for a dirty one.
4) The brackets should stay in the default color you established in your computer.
5) Puts the Prompt in the next line for readability.
Sie können die Farben mit dieser Liste anpassen
Ausgefeilte Lösung
Eine andere Möglichkeit ist, Git Bash Prompt zu verwenden und mit this zu installieren. Ich habe die Option über Homebrew unter Mac OS X verwendet.
git_Prompt_list_themes
um die Themes zu sehen, aber ich mochte keines davon.
git_Prompt_color_samples
um die verfügbaren Farben zu sehen.
git_Prompt_make_custom_theme [<Name of base theme>]
Zum Erstellen eines neuen benutzerdefinierten Designs sollte eine .git-Prompt-colors.sh-Datei erstellt werden.
subl ~/.git-Prompt-colors.sh
um git-Prompt-colors.sh zu öffnen und anzupassen:
Die Datei .git-Prompt-colors.sh sollte mit meiner Anpassung so aussehen
override_git_Prompt_colors() {
GIT_Prompt_THEME_NAME="Custom"
# Clean or dirty branch
if git diff --quiet 2>/dev/null >&2; then
GIT_Prompt_BRANCH="${Green}"
else
GIT_Prompt_BRANCH="${Red}"
fi
}
reload_git_Prompt_colors "Custom"
Hoffe das hilft, einen schönen Tag!
Installieren Sie einfach die oh-my-zsh
-Plugins wie in dieser Link beschrieben.
Es funktioniert am besten unter MacOS und Linux.
Grundinstallation
Oh My Zsh wird installiert, indem Sie einen der folgenden Befehle in Ihrem Terminal ausführen. Sie können dies über die Befehlszeile mit curl
oder wget
installieren.
via curl
sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
via wget
sh -c "$(wget https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh -O -)"