Appearance
常用别名配置
Git 命令通常较长,通过配置别名可以大幅提升效率。比如用 git lg 代替 git log --oneline --graph --all --decorate。
设置命令别名
基本语法
bash
git config --global alias.<别名> '<命令>'别名会被添加到 ~/.gitconfig 的 [alias] 段中。
示例
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 st # 等价于 git status
git co main # 等价于 git checkout main执行 Shell 命令
别名以 ! 开头时,会作为 shell 命令执行(而非 git 子命令):
bash
# 执行 shell 命令
git config --global alias.ls '!ls -la'
# 定义复杂脚本函数
git config --global alias.cleanup '!git branch --merged | grep -v "\*\|main\|master\|develop" | xargs git branch -d'推荐别名集合
以下是经过实践检验的高效别名配置,直接添加到 ~/.gitconfig 的 [alias] 段:
ini
[alias]
# 基础命令简化
st = status
s = status -s # 简短状态
co = checkout
sw = switch
br = branch
ci = commit
cp = cherry-pick
rb = rebase
mg = merge
# 日志类
lg = log --oneline --graph --all --decorate
ll = log --oneline -10 # 查看最近 10 条
last = log -1 HEAD --stat # 查看最近一次提交
who = shortlog -n -s --no-merges # 贡献者统计
# 差异比较
d = diff
dc = diff --cached # 查看暂存区差异
ds = diff --stat # 统计摘要
# 暂存操作
a = add
aa = add --all
ap = add -p # 交互式暂存
# 提交相关
amend = commit --amend --no-edit # 快速追加到上一个提交
fix = commit --fixup # 创建 fixup 提交
# 分支操作
new = checkout -b # 创建并切换分支
del = branch -d # 删除分支
Del = branch -D # 强制删除分支
brs = branch -a -vv # 查看所有分支详情
# 远程操作
psh = push
psh-f = push --force-with-lease # 安全的强制推送
pl = pull
plr = pull --rebase # 拉取并变基
fe = fetch --all --prune # 拉取所有并清理
# 撤销操作
undo = reset --soft HEAD~1 # 撤销最近提交(保留改动)
unstage = restore --staged # 取消暂存(新语法)
discard = restore # 丢弃工作区改动
# Stash
sss = stash # 储藏
ssl = stash list # 查看储藏列表
ssp = stash pop # 恢复并删除储藏
ssa = stash apply # 恢复储藏(不删除)
# 查找类
find = log --all --full-history -- # 查找文件历史
grep-log = log --all --oneline --grep # 在提交信息中搜索
# 工具类
aliases = config --get-regexp alias # 查看所有别名
edit-config = config --global --edit # 编辑全局配置
root = rev-parse --show-toplevel # 显示仓库根目录
ignored = ls-files --others --ignored --exclude-standard # 查看被忽略文件
# 清理类
cleanup = "!git branch --merged | grep -v '\\*\\|main\\|master\\|develop' | xargs -r git branch -d"
trim = "!git remote prune origin && git gc --prune=now"实用别名详解
漂亮的日志格式
bash
# 自定义格式的美观日志
git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
# 效果:
# * a1b2c3d - (HEAD -> main) feat: 添加登录功能 (2 hours ago) <Zhang San>
# * e4f5g6h - fix: 修复样式问题 (1 day ago) <Li Si>安全的强制推送
bash
git config --global alias.pushf "push --force-with-lease"
# 使用
git pushf # 比 git push -f 更安全,防止覆盖他人推送快速查看当前状态摘要
bash
git config --global alias.info "!echo '=== Branch ===' && git branch -v && echo '=== Status ===' && git status -s"清理已合并的本地分支
bash
git config --global alias.cleanup "!git branch --merged main | grep -v 'main\\|\\*' | xargs -r git branch -d && echo 'Cleaned up merged branches'"撤销最后 N 次提交
bash
# 使用参数(需要 shell 函数)
git config --global alias.undo-n "!f() { git reset --soft HEAD~\${1:-1}; }; f"
# 使用
git undo-n # 撤销最近 1 次提交
git undo-n 3 # 撤销最近 3 次提交自定义 Git 子命令
除了别名,还可以创建完全独立的 Git 子命令脚本。
创建方法
将可执行脚本命名为 git-<command>,放在 PATH 路径下,即可用 git <command> 调用:
bash
# 创建脚本文件
cat > /usr/local/bin/git-branch-clean << 'EOF'
#!/bin/bash
# 删除所有已合并到当前分支的本地分支
git branch --merged | grep -v "\*\|main\|master\|develop" | xargs -r git branch -d
echo "Cleaned up merged branches"
EOF
chmod +x /usr/local/bin/git-branch-clean
# 使用
git branch-clean实用子命令示例
git-recent:查看最近切换过的分支
bash
cat > /usr/local/bin/git-recent << 'EOF'
#!/bin/bash
# 显示最近使用的 n 个分支
n=${1:-10}
git reflog | grep checkout | head -$n | sed 's/.*checkout: moving from .* to //'
EOF
chmod +x /usr/local/bin/git-recentgit-summary:仓库摘要信息
bash
cat > /usr/local/bin/git-summary << 'EOF'
#!/bin/bash
echo "=== Repository Summary ==="
echo "Branch: $(git branch --show-current)"
echo "Commits: $(git rev-list --count HEAD)"
echo "Contributors: $(git shortlog -sn | wc -l)"
echo "Files: $(git ls-files | wc -l)"
echo "Last commit: $(git log -1 --format='%ar by %an')"
EOF
chmod +x /usr/local/bin/git-summary查看和管理别名
bash
# 查看所有已设置的别名
git config --get-regexp alias
# 或使用 aliases 别名(如果已设置)
git aliases
# 删除某个别名
git config --global --unset alias.st
# 直接编辑配置文件管理别名
git config --global --edit别名的局限性
- 别名不能引用另一个别名(不支持别名链)
- Shell 命令别名(
!开头)在 Windows 上可能有兼容性问题 - 带参数的复杂操作建议写成 shell 函数或脚本
总结
配置好别名后,你的 Git 工作流会更加流畅。以下是最高频使用的别名推荐:
| 别名 | 对应命令 | 用途 |
|---|---|---|
st | status | 查看状态 |
lg | log --oneline --graph --all | 美观日志 |
co | checkout | 切换 |
ci | commit | 提交 |
dc | diff --cached | 暂存区差异 |
amend | commit --amend --no-edit | 追加到上次提交 |
undo | reset --soft HEAD~1 | 撤销上次提交 |
pushf | push --force-with-lease | 安全强制推送 |