Ich verwende diese Anleitung , um den Zweignamen in gnome terminal (Ubuntu 15.10) anzuzeigen, wenn ich in einem Git-Repository arbeite. Basierend auf dem obigen habe ich jetzt das Folgende in meiner ~/.bashrc-Datei:
# uncomment for a colored Prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the Prompt
#force_color_Prompt=yes
...
# Add git branch if its present to PS1
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
if [ "$color_Prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\[email protected]\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[01;31m\]$(parse_git_branch)\[\033[00m\]\$ '
else
PS1='${debian_chroot:+($debian_chroot)}\[email protected]\h:\w$(parse_git_branch)\$ '
fi
unset color_Prompt force_color_Prompt
Als Ergebnis bekomme ich jetzt:
so funktioniert es . Aber warum wurde die Farbe meines Benutzers @ Host entfernt? Und ich würde auch erwarten, dass der Filialname farbig sein sollte. Vorher sah es so aus:
UPDATE: Ich habe jetzt stattdessen diese Anleitung ausprobiert:
https://coderwall.com/p/fasnya/add-git-branch-name-to-bash-Prompt
dies zu .bashrc hinzufügen:
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
export PS1="\[email protected]\h \[\033[32m\]\w\[\033[33m\]\$(parse_git_branch)\[\033[00m\] $ "
und das funktioniert:
Hinweis in .bashrc Ich habe auch diese (Standard):
# uncomment for a colored Prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the Prompt
#force_color_Prompt=yes
Ich habe noch keinen Grund gefunden, warum dieses Snippet das richtige Ergebnis liefert und die andere Version nicht. Irgendwelche Eingaben dazu?
Hier ist die Version von .bashrc, für die das alte Snippet aktiviert ist und die nicht funktioniert:
Dieser Ausschnitt:
_# Add git branch if its present to PS1
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
if [ "$color_Prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\[email protected]\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[01;31m\]$(parse_git_branch)\[\033[00m\]\$ '
else
PS1='${debian_chroot:+($debian_chroot)}\[email protected]\h:\w$(parse_git_branch)\$ '
fi
_
Soll die Standard-Prompt-Definition ersetzen :
_if [ "$color_Prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\[email protected]\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
PS1='${debian_chroot:+($debian_chroot)}\[email protected]\h:\w\$ '
fi
_
Was endet mit:
_unset color_Prompt force_color_Prompt
_
Der von Ihnen gepostete _.bashrc
_ zeigt an, dass Sie ihn nach der Standard-Eingabeaufforderungsdefinition und _unset color_Prompt force_color_Prompt
_ (Zeile # 64) hinzufügen.
Ersetzen Sie die Standard-Prompt-Definition durch das Snippet oder lassen Sie _~/.bashrc
_ unverändert und kommentieren Sie die Standard-Prompt-Definition zusammen mit _unset color_Prompt force_color_Prompt
_ in Zeile 64:
So könnte ein Teil Ihres .bashrc aussehen
_parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
if [ "$color_Prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\[email protected]\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[01;31m\] $(parse_git_branch)\[\033[00m\]\$ '
else
PS1='${debian_chroot:+($debian_chroot)}\[email protected]\h:\w$(parse_git_branch)\$ '
fi
# THE SIX LINES BELOW are the default Prompt and the unset (which were in the original .bashrc)
#if [ "$color_Prompt" = yes ]; then
# PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\[email protected]\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
#else
# PS1='${debian_chroot:+($debian_chroot)}\[email protected]\h:\w\$ '
#fi
#unset color_Prompt force_color_Prompt
_
Fügen Sie diese Zeilen in Ihre ~/.bashrc-Datei ein
# Show git branch name
force_color_Prompt=yes
color_Prompt=yes
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
if [ "$color_Prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\[email protected]\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[01;31m\]$(parse_git_branch)\[\033[00m\]\$ '
else
PS1='${debian_chroot:+($debian_chroot)}\[email protected]\h:\w$(parse_git_branch)\$ '
fi
unset color_Prompt force_color_Prompt
Laden Sie die .bashrc-Datei mit dem folgenden Befehl neu:
$ source ~/.bashrc
Im Moment bin ich diesem Beispiel gefolgt https://Gist.github.com/eliotsykes/47516b877f5a4f7cd52f und habe es bisher gemocht, obwohl ich vorhabe, es weiter anzupassen.
Im Terminal
mkdir ~/.bash
Kopieren Sie die Rohdatei
git-Prompt.sh
von git contrib in das Verzeichnis~/.bash
: https://github.com/git/git/blob/master/contrib/completion/git-Prompt. shFügen Sie in
~/.bashrc
oder~/.bash_profile
(wählen Sie die Datei aus, in der Sie normalerweise die Bash-Anpassungen/Einstellungen vornehmen) die folgenden Zeilen hinzu:source ~/.bash/git-Prompt.sh # Show git branch name at command Prompt export GIT_PS1_SHOWCOLORHINTS=true # Option for git-Prompt.sh to show branch name in color # Terminal Prompt: # Include git branch, use Prompt_COMMAND (not PS1) to get color output (see git-Prompt.sh for more) export Prompt_COMMAND='__git_ps1 "\w" "\n\\\$ "' # Git branch (relies on git-Prompt.sh)
Solange Sie sich in einem Git-Repo befinden, sollte Ihre Bash-Eingabeaufforderung jetzt den aktuellen Git-Zweig in Farbe anzeigen, um anzuzeigen, ob nicht festgeschriebene Änderungen vorgenommen wurden.
Gehe zum Home-Ordner
klicken Ctrl+h um versteckte Dateien anzuzeigen.
Öffne die Datei .bashrc
und füge am Ende die folgende ein:
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
export PS1="\[email protected]\h \[\033[32m\]\w\[\033[33m\]\$(parse_git_branch)\[\033[00m\] $ "
Wenn Sie Ihr Terminal geöffnet haben, schließen Sie es und öffnen Sie es erneut. Genießen!!
Mein Problem war, dass ich die Option nicht aktiviert hatte
Befehl als Login-Shell ausführen in
Terminal → Bearbeiten → Profil Einstellungen → Befehl
ersetzen
parse_git_branch
mit
parse_git_branch 2>/dev/null
in deiner PS1-Definition und lebe glücklich bis ans Ende.