自定义 Git Bash shell
省略的行
由于需要保持向后兼容性,Bash 的新功能很少默认启用。必须配置 MinGW64(适用于 Windows x64 的极简 GNU)才能充分利用这些功能。只需进行一些简单的调整,即可在无需使用外部工具的情况下,开箱即用地增强 Bash 的功能,从而提高您的工作效率。我假设您已安装 Windows 版 Git 以及可选的 Windows 终端。
自定义启动文件
Git bash shell 被调用为一个交互式登录 shell,用于仅进行 Git 身份验证访问。Bash 使用一些启动文件来为用户配置 shell 环境。它将按给定的顺序执行 、 和 等文件~/.bash_profile
。~/.bash_login
第~/.profile
一个可读的文件将被执行。
Bash 配置文件
1. 创建一个~/.bash_profile
文件。该文件.bash_profile
包含用于设置 shell 环境变量的命令。~/.bash_profile
可以使用 代替 ,~/.profile
但只能由 Bash 读取。由于 shell 是交互式的,因此该~/.bashrc
文件无需执行任何操作。更多信息请参阅Bash 手册。将以下所有内容复制到您自己的文件中。
# Source the ~/.bashrc file if it exists
if [ -f ~/.bashrc ]
then
. ~/.bashrc
fi
Bash shell脚本
2. 创建一个~/.bashrc
文件。该.bashrc
文件包含 Bash Shell 特有的命令。它是存放别名和 Bash 相关函数的最佳位置。将以下所有内容复制到你自己的文件中。
# Git aliases
alias gs='git status -sb'
alias gcc='git checkout'
alias gcm='git checkout master'
alias gaa='git add --all'
alias gc='git commit -m $2'
alias push='git push'
alias gpo='git push origin'
alias pull='git pull'
alias clone='git clone'
alias stash='git stash'
alias pop='git stash pop'
alias ga='git add'
alias gb='git branch'
alias gl="git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
alias gm='git merge'
# Bash aliases
alias .='cd .'
alias ..='cd ..'
alias ...='cd ../../'
alias ....='cd ../../../'
alias .....='cd ../../../../'
alias bashclear='echo "" > ~/.bash_history'
alias cls='clear'
alias ls='ls -F --color=auto --show-control-chars'
alias ll='ls -l'
alias ll.='ls -la'
alias lls='ls -la --sort=size'
alias llt='ls -la --sort=time'
alias rm='rm -iv'
alias work='cd /c/repos'
# Bash shell settings
# Typing a directory name just by itself will automatically change into that directory.
shopt -s autocd
# Automatically fix directory name typos when changing directory.
shopt -s cdspell
# Automatically expand directory globs and fix directory name typos whilst completing.
# Note, this works in conjuction with the cdspell option listed above.
shopt -s direxpand dirspell
# Enable the ** globstar recursive pattern in file and directory expansions.
# For example, ls **/*.txt will list all text files in the current directory hierarchy.
shopt -s globstar
# Ignore lines which begin with a <space> and match previous entries.
# Erase duplicate entries in history file.
HISTCONTROL=ignoreboth:erasedups
# Ignore saving short- and other listed commands to the history file.
HISTIGNORE=?:??:history
# The maximum number of lines in the history file.
HISTFILESIZE=99999
# The number of entries to save in the history file.
HISTSIZE=99999
# Set Bash to save each command to history, right after it has been executed.
PROMPT_COMMAND='history -a'
# Save multi-line commands in one history entry.
shopt -s cmdhist
# Append commands to the history file, instead of overwriting it.
# History substitution are not immediately passed to the shell parser.
shopt -s histappend histverify
Git Bash 提示符
Git Bash 提示符由名为 的 shell 脚本设置git-prompt.sh
,该脚本可在目录中找到c/Program\ Files/Git/etc/profile.d
。请注意,在第 8-10 行,~/.config/git/git-prompt.sh
如果存在自定义文件,则会执行 source 命令。我认为这是覆盖默认设置的推荐方法。
# lines omitted
if test -f ~/.config/git/git-prompt.sh
then
. ~/.config/git/git-prompt.sh
else
# lines omitted
fi
# lines omitted
3. 创建一个~/.config/git/git-prompt.sh
文件。该git-prompt.sh
文件包含用于设置 Git Bash 终端标题和 Bash 提示符字符串的命令。将以下所有内容复制到您自己的文件中。
# Custom prompt settings
PROMPT_DIRTRIM=4 # Shorten deep paths in the prompt
PS1='\[\033]0;Git | Bash v\v | \W\007\]' # set window title
PS1="$PS1"'\n' # new line
PS1="$PS1"'\[\033[30;45m\] [\A] ' # black text, magenta, 24h time
PS1="$PS1"'\[\033[30;42m\] \u ' # black text, green, user
#PS1="$PS1"'\[\033[30;42m\]@\h ' # black text, green, @host
PS1="$PS1"'\[\033[30;43m\] \w ' # black text, yellow, working director
if test -z "$WINELOADERNOEXEC"
then
GIT_EXEC_PATH="$(git --exec-path 2>/dev/null)"
COMPLETION_PATH="${GIT_EXEC_PATH%/libexec/git-core}"
COMPLETION_PATH="${COMPLETION_PATH%/lib/git-core}"
COMPLETION_PATH="$COMPLETION_PATH/share/git/completion"
if test -f "$COMPLETION_PATH/git-prompt.sh"
then
. "$COMPLETION_PATH/git-completion.bash"
. "$COMPLETION_PATH/git-prompt.sh"
PS1="$PS1"'\[\033[97;46m\]' # white text, cyan
PS1="$PS1"'`__git_ps1`' # bash function
fi
fi
PS1="$PS1"'\[\033[0m\]' # change color
PS1="$PS1"'\n' # new line
PS1="$PS1"'$ ' # prompt: always $
# Git status options
# Shows * or + for unstaged and staged changes, respectively.
export GIT_PS1_SHOWSTASHSTATE=true
# shows $ if there are any stashes.
export GIT_PS1_SHOWDIRTYSTATE=true
# Shows % if there are any untracked files.
export GIT_PS1_SHOWUNTRACKEDFILES=true
# shows <, >, <>, or = when your branch is behind, ahead, diverged from,
# or in sync with the upstream branch, respectively.
export GIT_PS1_SHOWUPSTREAM="auto"
用户配置
4. 创建一个~/.inputrc
文件。此文件包含使用内置 GNU Readline 库配置命令历史记录、目录显示和键盘绑定的命令。有关更多信息,请参阅Bash 手册和Readline 文档。
# Disable beeps & bells, and do not display control characters.
set bell-style none
set echo-control-characters off
# The TAB key cycles forward through the completion choices.
# Press an arrow key, such as right-arrow, to choose a selection.
TAB: menu-complete
# The Shift-TAB key cycles backward through the completion choices.
# Like TAB, press an arrow key, such as right-arrow, to choose a selection.
"\e[Z": menu-complete-backward
# The first TAB key press will display a list that match the given prefix.
# The next TAB key press will start cycling through the available choices.
set menu-complete-display-prefix on
# Display completion matches upon the first press of the TAB key.
#set show-all-if-ambiguous on
#Enable colors when completing filenames and directories.
set colored-stats on
# Completion matches of multiple items highlight the matching prefix in color.
set colored-completion-prefix on
# Ignore case when completing.
set completion-ignore-case on
# Treat hypens and underscores as equivalent when completing.
set completion-map-case on
# Append the / character to the end of symlinked directories when completing.
set mark-symlinked-directories on
# Enable incremental history navigation with the UP and DOWN arrow keys.
# This will use the already typed text as a required prefix.
"\e[A": history-search-backward
"\e[B": history-search-forward
Readline 库还提供了几个有用的快捷方式。
键盘快捷键 | 描述 |
---|---|
Control-A | 转到行首。 |
Control-E | 转到行尾。 |
Alt-B | 回退一个字。 |
Alt-F | 继续前进。 |
Alt-Backspace | 向后删除一个单词。 |
Alt-D | 向前删除一个单词。 |
Control-R | 回顾历史记录。 |
Control-R | 回顾历史。 |
Control-Shift-R | 循环历史。 |
Alt-。 | 附加前一个命令的参数。 |
最终结果
5. 自定义完成后,重新启动终端或在 Windows 终端中打开一个新的 Git Bash 选项卡。您的 Git Bash 应该看起来类似于下面的结果。
Bash 实用程序
Git for Windows 还包含一些 Bash 实用程序,如果您不想使用默认行为,可以使用它们来增强您的 Git 体验。这些实用程序需要在每个独立的开发环境中进行配置。有关更多信息,请参阅Pro Git Book 。
git -completion和git-prompt脚本位于以下目录中。将它们/c/Program\ Files/Git/mingw64/share/git/completion/
复制到您的主目录。git-completion.bash
git-prompt.sh
要使用脚本,请将以下行附加到~/.bashrc
文件。
# Enable tab completion for Git commands
source ~/.git-completion.bash
# Change bash prompt to display current Git branch and status
source ~/.git-prompt.sh