ser*_*erg 62 git macos terminal git-branch
我一直在观看Team Treehouse的一些视频,他们在使用Git时有一个非常漂亮的终端.
例如,他们(类似):
mike@treehouseMac: [/Work/test - feature-branch-name] $ git add .
mike@treehouseMac: [/Work/test - feature-branch-name] $ git commit -m "Some feature."
mike@treehouseMac: [/Work/test - feature-branch-name] $ git checkout master
mike@treehouseMac: [/Work/test - master] $ git status
Run Code Online (Sandbox Code Playgroud)
我的终端如何向我展示一些关于我所在分支的有用信息,用颜色来区分我想要的数据位?是否有某种事实上的插件我还没有找到?
我正在使用Mac OSX 10.8
rom*_*iem 84
对于那些在 macOS Catalina 或更高版本(10.15+ 包括 Big Sur 11.0)中寻找如何执行此操作的人,该版本已弃用 bash 而支持 zsh,这是我的 .zshrc 文件:
parse_git_branch() {
git branch 2> /dev/null | sed -n -e 's/^\* \(.*\)/[\1]/p'
}
COLOR_DEF='%f'
COLOR_USR='%F{243}'
COLOR_DIR='%F{197}'
COLOR_GIT='%F{39}'
NEWLINE=$'\n'
setopt PROMPT_SUBST
export PROMPT='${COLOR_USR}%n@%M ${COLOR_DIR}%d ${COLOR_GIT}$(parse_git_branch)${COLOR_DEF}${NEWLINE}%% '
Run Code Online (Sandbox Code Playgroud)
如果您不喜欢我使用的颜色,请将 243/197/39 值替换为此处定义的颜色代码:https ://misc.flogisoft.com/bash/tip_colors_and_formatting
Sto*_*ica 61
这不是一个插件.这是关于shell的快速技巧.
要在bash中进行酷炫的设置,请查看dotfiles这个人的项目:
https://github.com/mathiasbynens/dotfiles
要得到一个花哨的提示,请.bash_prompt在您的~/.bash_profile或中添加~/.bashrc.
要获得与问题完全相同的提示,请更改export PS1末尾的行,.bash_prompt如下所示:
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\]"
Run Code Online (Sandbox Code Playgroud)
我.bash*一个月前最终使用了这个存储库中的所有文件,这对我来说非常有用.
对于Git来说,还有额外的好处.gitconfig.
因为你是一个mac用户,所以还有更多好东西.osx.
6LY*_*TH3 55
简单的方法
~/.bash_profile在您喜欢的编辑器中打开,并将以下内容添加到底部.
提示中的Git分支.
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
export PS1="\u@\h \[\033[32m\]\w - \$(parse_git_branch)\[\033[00m\] $ "
Run Code Online (Sandbox Code Playgroud)
ser*_*erg 28
为了扩展现有的优秀答案,获得一个外观漂亮的终端的一个非常简单的方法是使用开源Dotfiles项目.
https://github.com/mathiasbynens/dotfiles

在OSX和Linux上安装很简单.在终端中运行以下命令.
git clone https://github.com/mathiasbynens/dotfiles.git && cd dotfiles && source bootstrap.sh
Run Code Online (Sandbox Code Playgroud)
这将是:
cd 进入文件夹.小智 12
只需oh-my-zsh按照此链接中的说明安装插件。

它在 macOS 和 Linux 上效果最好。
基本安装
Oh My Zsh 是通过在终端中运行以下命令之一来安装的。您可以通过命令行使用curl或安装它wget。
通过卷曲
sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
Run Code Online (Sandbox Code Playgroud)
通过 wget
sh -c "$(wget https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh -O -)"
Run Code Online (Sandbox Code Playgroud)
Tom*_*ale 11
我的提示包括:
rsync- user@host:pathname复制粘贴善良的风格#
# Set the prompt #
#
# Select git info displayed, see /usr/share/git/completion/git-prompt.sh 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\u@\h$None:"
fi
PreGitPS1+="$Blu\w$None"
else # No colour
# Sets prompt like: ravi@boxy:~/prj/sample_app
unset GIT_PS1_SHOWCOLORHINTS
PreGitPS1="${debian_chroot:+($debian_chroot)}\u@\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\]\u@\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
Run Code Online (Sandbox Code Playgroud)
对于 Mac Catilina 10.15.5 及更高版本:
添加您的 ~/.zshrc 文件
function parse_git_branch() {
git branch 2> /dev/null | sed -n -e 's/^\* \(.*\)/[\1]/p'
}
setopt PROMPT_SUBST
export PROMPT='%F{grey}%n%f %F{cyan}%~%f %F{green}$(parse_git_branch)%f %F{normal}$%f '
Run Code Online (Sandbox Code Playgroud)
在适用于 Mac 的新 Catalina 操作系统中
i) zsh方式。添加以下行.zshrc
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ \1/'
}
COLOR_DEF='%f'
COLOR_DIR='%F{197}'
COLOR_GIT='%F{33}'
setopt PROMPT_SUBST
export PROMPT='${COLOR_DIR}%1d${COLOR_DEF}${COLOR_GIT}$(parse_git_branch)${COLOR_DEF} $ '
Run Code Online (Sandbox Code Playgroud)
ii) 或者使用旧的bash,你需要改变
System Preference -> Users & Groups -> Right click user user
-> Advanced Option -> Login shell -> /bin/bash
Run Code Online (Sandbox Code Playgroud)
如下写入.bash_profile并重启系统
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ \1/'
}
export PS1="\W\[\033[33m\]\$(parse_git_branch)\[\033[00m\] $ "
Run Code Online (Sandbox Code Playgroud)
输出:FolderName BranchName $
在 2019 年,我认为这git branch --show-current是一个比公认的答案更好的命令。
$ git branch --show-current
master
Run Code Online (Sandbox Code Playgroud)
(在 2019 年 6 月的 git 2.22 版本中添加)
它运行得更快,因为它不需要遍历所有分支。同样git branch,在命令提示符中也应该避免,因为如果您有许多本地分支,它会减慢您的提示速度。
把它放在一个函数中以在命令提示符的任何地方使用:
# This function returns '' in all below cases:
# - git not installed or command not found
# - not in a git repo
# - in a git repo but not on a branch (HEAD detached)
get_git_current_branch() {
git branch --show-current 2> /dev/null
}
Run Code Online (Sandbox Code Playgroud)
更多背景:
$ git version
git version 2.23.0
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
81147 次 |
| 最近记录: |