Appearance
编辑器与别名
概述
Git 默认使用系统默认编辑器(通常是 vi/vim),但你可以配置自己喜欢的编辑器。同时,Git 别名功能可以让你为常用命令创建简短的自定义名称,大幅提高工作效率。
合理配置编辑器和别名是提升 Git 使用体验的重要一步,本章将详细介绍如何配置编辑器、创建和管理别名,以及一些实用的配置技巧。
配置默认编辑器
常用编辑器配置
bash
# VS Code(推荐)
git config --global core.editor "code --wait"
# Vim
git config --global core.editor "vim"
# Neovim
git config --global core.editor "nvim"
# Nano
git config --global core.editor "nano"
# Emacs
git config --global core.editor "emacs"
# Sublime Text
git config --global core.editor "subl -n -w"
# Atom
git config --global core.editor "atom --wait"
# JetBrains IDE 系列
# PhpStorm
git config --global core.editor "phpstorm --wait"
# WebStorm
git config --global core.editor "webstorm --wait"
# IntelliJ IDEA
git config --global core.editor "idea --wait"
# PyCharm
git config --global core.editor "pycharm --wait"编辑器参数说明
bash
# VS Code 参数说明
code --wait # 等待文件关闭后继续
code --new-window # 在新窗口打开
code --reuse-window # 复用现有窗口
# Sublime Text 参数说明
subl -n # 新窗口
subl -w # 等待
# Atom 参数说明
atom --wait # 等待文件关闭
# JetBrains 参数说明
phpstorm --wait # 等待文件关闭
phpstorm --line 10 # 打开到指定行验证编辑器配置
bash
# 查看当前编辑器配置
git config --global core.editor
# 测试编辑器
git commit --amend
# 会打开配置的编辑器
# 或使用
git config --global -e
# 打开全局配置文件进行编辑编辑器特定配置
VS Code 配置
bash
# 基本配置
git config --global core.editor "code --wait"
# 使用特定 VS Code 配置
git config --global core.editor "code --wait --user-data-dir ~/.vscode-git"
# 使用 VS Code Insiders
git config --global core.editor "code-insiders --wait"Vim/Neovim 配置
bash
# Vim 基本配置
git config --global core.editor "vim"
# Neovim 配置
git config --global core.editor "nvim"
# 使用特定 Vim 配置
git config --global core.editor "vim -u ~/.vimrc.git"
# 使用 Vim 的特定模式
git config --global core.editor "vim -f" # 前台运行Nano 配置
bash
# Nano 基本配置
git config --global core.editor "nano"
# Nano 带软换行
git config --global core.editor "nano -w"
# Nano 启用鼠标支持
git config --global core.editor "nano -m"配置 Git 别名
基本别名语法
bash
# 语法格式
git config --global alias.<别名> <命令>
# 示例
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit常用别名推荐
基础命令别名
bash
# 状态查看
git config --global alias.st status
git config --global alias.ss "status -s" # 简洁模式
# 分支操作
git config --global alias.br branch
git config --global alias.bv "branch -vv" # 显示详细信息
git config --global alias.ba "branch -a" # 显示所有分支
git config --global alias.bd "branch -d" # 删除分支
git config --global alias.bD "branch -D" # 强制删除分支
# 检出操作
git config --global alias.co checkout
git config --global alias.cb "checkout -b" # 创建并切换分支
git config --global alias.cm "checkout main" # 切换到主分支
# 提交操作
git config --global alias.ci commit
git config --global alias.cm "commit -m" # 带消息提交
git config --global alias.ca "commit --amend" # 修改提交
git config --global alias.cane "commit --amend --no-edit" # 修改提交但不改消息
# 添加操作
git config --global alias.a "add"
git config --global alias.aa "add --all"
git config --global alias.au "add -u" # 只添加已跟踪文件
git config --global alias.ap "add -p" # 交互式添加日志查看别名
bash
# 基本日志
git config --global alias.lg log
git config --global alias.lg1 "log -1" # 查看最后一次提交
git config --global alias.lg5 "log -5" # 查看最后 5 次提交
# 单行日志
git config --global alias.lol "log --oneline"
git config --global alias.lola "log --oneline --all"
# 图形化日志
git config --global alias.lg "log --oneline --graph --all"
git config --global alias.lgg "log --graph --oneline --decorate --all"
# 美化日志
git config --global alias.lgp "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
# 详细日志
git config --global alias.ll "log --pretty=format:'%C(yellow)%h%Creset %s %C(cyan)(%cr)%Creset' --abbrev-commit"差异查看别名
bash
# 基本差异
git config --global alias.d diff
git config --global alias.ds "diff --staged" # 查看暂存区差异
git config --global alias.dw "diff --word-diff" # 单词级别差异
git config --global alias.dc "diff --check" # 检查空白错误
# 使用差异工具
git config --global alias.dt "difftool"
# 查看文件差异统计
git config --global alias.dstat "diff --stat"远程操作别名
bash
# 远程仓库
git config --global alias.remotev "remote -v"
# 拉取推送
git config --global alias.pl pull
git config --global alias.ps push
git config --global alias.pf "push --force-with-lease" # 安全强制推送
# 获取更新
git config --global alias.fe fetch
git config --global alias.fea "fetch --all"
git config --global alias.fep "fetch --prune" # 获取并清理远程分支引用撤销操作别名
bash
# 撤销操作
git config --global alias.undo "reset --soft HEAD~1" # 撤销提交,保留修改
git config --global alias.undo-hard "reset --hard HEAD~1" # 撤销提交,丢弃修改
git config --global alias.unstage "reset HEAD --" # 取消暂存
git config --global alias.uncommit "reset --soft HEAD^" # 撤销提交
# 恢复文件
git config --global alias.restore "checkout --"
git config --global alias.restore-staged "restore --staged"合并变基别名
bash
# 合并操作
git config --global alias.m merge
git config --global alias.mn "merge --no-ff" # 禁用快进合并
git config --global alias.ma "merge --abort" # 中止合并
# 变基操作
git config --global alias.rb rebase
git config --global alias.rbi "rebase -i" # 交互式变基
git config --global alias.rba "rebase --abort" # 中止变基
git config --global alias.rbc "rebase --continue" # 继续变基标签操作别名
bash
# 标签操作
git config --global alias.t tag
git config --global alias.ta "tag -a" # 创建附注标签
git config --global alias.tl "tag -l" # 列出标签
git config --global alias.td "tag -d" # 删除标签存储操作别名
bash
# 存储操作
git config --global alias.s stash
git config --global alias.sa "stash apply" # 应用存储
git config --global alias.sc "stash clear" # 清空存储
git config --global alias.sl "stash list" # 列出存储
git config --global alias.sp "stash pop" # 弹出存储
git config --global alias.ss "stash save" # 保存存储复杂命令别名
带参数的别名
bash
# 注意:别名不能直接带参数,需要使用 shell 函数或脚本
# 方法 1:使用 ! 执行 shell 命令
git config --global alias.last 'log -1 HEAD'
git config --global alias.new '!git log $1@{1}..$1@{0} "$@"'
# 方法 2:使用 shell 函数
git config --global alias.cm '!f() { git commit -m "$1"; }; f'
git config --global alias.publish '!git push -u origin $(git branch --show-current)'
# 方法 3:使用外部脚本
git config --global alias.mylog '!git log --oneline --graph --all'实用复杂别名
bash
# 显示最近修改的文件
git config --global alias.last-modified '!git log -1 --name-only --pretty=format:'
# 显示未合并的分支
git config --global alias.unmerged '!git branch --no-merged main'
# 删除已合并的分支
git config --global alias.clean-merged '!git branch --merged main | grep -v "^\*\|main\|develop" | xargs -n 1 git branch -d'
# 显示贡献者统计
git config --global alias.contributors 'shortlog -sn'
# 显示文件历史
git config --global alias.file-log '!git log --follow -p --'
# 显示仓库统计
git config --global alias.stats '!git log --author="$1" --pretty=tformat: --numstat | awk "{ add += $1; subs += $2; loc += $1 - $2 } END { printf \"added lines: %s, removed lines: %s, total lines: %s\n\", add, subs, loc }" -'
# 快速提交
git config --global alias.quick '!f() { git add -A && git commit -m "$1" && git push; }; f'
# 创建并切换分支
git config --global alias.nb '!git checkout -b'
# 查看某个文件的历史版本
git config --global alias.show-file '!git show $1:$2'别名管理
查看所有别名
bash
# 查看所有别名
git config --global --get-regexp alias
# 格式化输出
git config --global --get-regexp alias | sed 's/alias\.//g' | sort
# 创建查看别名的别名
git config --global alias.aliases 'config --get-regexp alias'删除别名
bash
# 删除单个别名
git config --global --unset alias.st
# 删除所有别名(谨慎使用)
git config --global --unset-all alias备份别名
bash
# 导出别名
git config --global --get-regexp alias > ~/git-aliases-backup.txt
# 从备份恢复
while read line; do
key=$(echo $line | awk '{print $1}')
value=$(echo $line | cut -d' ' -f2-)
git config --global "$key" "$value"
done < ~/git-aliases-backup.txt配置文件详解
全局配置文件示例
ini
# ~/.gitconfig
[user]
name = Your Name
email = your.email@example.com
[core]
editor = code --wait
autocrlf = input
ignorecase = false
[alias]
st = status
co = checkout
br = branch
ci = commit
lg = log --oneline --graph --all
lgp = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
undo = reset --soft HEAD~1
unstage = reset HEAD --
last = log -1 HEAD
cm = commit -m
cam = commit -am
ll = log --pretty=format:'%C(yellow)%h%Creset %s %C(cyan)(%cr)%Creset' --abbrev-commit
d = diff
ds = diff --staged
dc = diff --check
publish = !git push -u origin $(git branch --show-current)
clean-merged = !git branch --merged main | grep -v "^\\*\\|main\\|develop" | xargs -n 1 git branch -d
[color]
ui = auto
[init]
defaultBranch = main
[credential]
helper = osxkeychain
[push]
default = simple
autoSetupRemote = true
[pull]
rebase = false直接编辑配置文件
bash
# 打开全局配置文件
git config --global --edit
# 或使用编辑器直接打开
code ~/.gitconfig
vim ~/.gitconfig
nano ~/.gitconfig配置文件位置
bash
# 系统配置
/etc/gitconfig
# 全局配置
~/.gitconfig
# 或
~/.config/git/config
# 本地配置
.git/config
# 查看配置文件路径
git config --list --show-origin高级配置技巧
使用 include 引入配置
bash
# 在 ~/.gitconfig 中添加
# 引入公共配置
[include]
path = ~/.gitconfig.common
# 引入工作配置(条件)
[includeIf "gitdir:~/work/"]
path = ~/.gitconfig.work
# 引入个人配置(条件)
[includeIf "gitdir:~/personal/"]
path = ~/.gitconfig.personal配置文件模板
ini
# ~/.gitconfig.common - 公共配置
[core]
editor = code --wait
autocrlf = input
[color]
ui = auto
[alias]
st = status
co = checkout
br = branch
ci = commit
lg = log --oneline --graph --all
[init]
defaultBranch = mainini
# ~/.gitconfig.work - 工作配置
[user]
name = Work Name
email = work@company.com
[commit]
gpgsign = trueini
# ~/.gitconfig.personal - 个人配置
[user]
name = Personal Name
email = personal@example.com使用 Git 钩子自动配置
bash
# 创建模板目录
mkdir -p ~/.git-templates/hooks
# 配置模板目录
git config --global init.templatedir ~/.git-templates
# 创建 post-checkout 钩子
cat > ~/.git-templates/hooks/post-checkout << 'EOF'
#!/bin/bash
# 根据仓库路径自动配置用户信息
if [[ $(pwd) == *"/work/"* ]]; then
git config user.name "Work Name"
git config user.email "work@company.com"
elif [[ $(pwd) == *"/personal/"* ]]; then
git config user.name "Personal Name"
git config user.email "personal@example.com"
fi
EOF
chmod +x ~/.git-templates/hooks/post-checkout实用别名集合
完整推荐配置
bash
# 一键配置所有推荐别名
# 基础命令
git config --global alias.st status
git config --global alias.ss "status -s"
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.cm "commit -m"
git config --global alias.cam "commit -am"
# 日志
git config --global alias.lg "log --oneline --graph --all"
git config --global alias.lgp "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
git config --global alias.ll "log --pretty=format:'%C(yellow)%h%Creset %s %C(cyan)(%cr)%Creset' --abbrev-commit"
git config --global alias.last "log -1 HEAD"
# 差异
git config --global alias.d diff
git config --global alias.ds "diff --staged"
git config --global alias.dt difftool
# 分支
git config --global alias.ba "branch -a"
git config --global alias.bv "branch -vv"
git config --global alias.bd "branch -d"
git config --global alias.nb "checkout -b"
# 撤销
git config --global alias.undo "reset --soft HEAD~1"
git config --global alias.unstage "reset HEAD --"
git config --global alias.undo-hard "reset --hard HEAD~1"
# 远程
git config --global alias.pl pull
git config --global alias.ps push
git config --global alias.pf "push --force-with-lease"
git config --global alias.fe fetch
git config --global alias.fep "fetch --prune"
# 存储
git config --global alias.sa "stash apply"
git config --global alias.sl "stash list"
git config --global alias.sp "stash pop"
# 复杂命令
git config --global alias.publish '!git push -u origin $(git branch --show-current)'
git config --global alias.clean-merged '!git branch --merged main | grep -v "^\\*\\|main\\|develop" | xargs -n 1 git branch -d'
git config --global alias.contributors 'shortlog -sn'
git config --global alias.aliases 'config --get-regexp alias'快速配置脚本
bash
#!/bin/bash
# setup-git-aliases.sh
echo "配置 Git 别名..."
# 基础命令
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
# 日志
git config --global alias.lg "log --oneline --graph --all"
# 撤销
git config --global alias.undo "reset --soft HEAD~1"
git config --global alias.unstage "reset HEAD --"
echo "Git 别名配置完成!"
echo ""
echo "常用别名:"
git config --global --get-regexp alias | sed 's/alias\.//g' | sort总结
必配编辑器
bash
# VS Code(推荐)
git config --global core.editor "code --wait"必配别名
bash
# 基础命令
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
# 日志
git config --global alias.lg "log --oneline --graph --all"
# 撤销
git config --global alias.undo "reset --soft HEAD~1"
git config --global alias.unstage "reset HEAD --"配置最佳实践
- 选择熟悉的编辑器:提高提交信息编写效率
- 创建有意义的别名:简短且易于记忆
- 使用复杂别名自动化:减少重复操作
- 备份配置文件:便于在新环境快速配置
- 团队共享别名:统一团队工作流程
下一步
配置完成后,建议继续学习:
