文章 585
评论 5
浏览 226854
Git 操作总是手忙脚乱?这 5 个 alias 让你效率翻倍

Git 操作总是手忙脚乱?这 5 个 alias 让你效率翻倍

一、引言

Git 命令虽然强大,但冗长的参数组合常常让人记不住。你是否每次撤销 commit 都要查资料?每次看日志都要输一长串参数?

今天我要分享 5 个超实用的 Git alias,让你的日常操作效率翻倍。


二、Alias 配置方式

2.1 临时配置(当前仓库)

git config alias.undo 'reset --soft HEAD~1'

2.2 全局配置(所有仓库)

git config --global alias.undo 'reset --soft HEAD~1'

2.3 直接编辑 ~/.gitconfig

cat >> ~/.gitconfig << 'EOF'
[alias]
    undo = reset --soft HEAD~1
    unstage = reset HEAD --
    wip = !git add -A && git commit -m "WIP"
    lg = log --oneline --graph --decorate --all --color
    fixup = !f() { git commit --fixup=$1 && GIT_SEQUENCE_EDITOR=true git rebase -i --autosquash $1~1; }; f
EOF

三、Alias 一:git undo — 撤销最近一次 commit

3.1 配置

git config --global alias.undo 'reset --soft HEAD~1'

3.2 用法

# 撤销最近一次 commit(保留工作区和暂存区)
git undo

# 撤销最近 N 次 commit
git undo HEAD~N

3.3 原理

# git undo 等价于
git reset --soft HEAD~1
参数作用
--soft保留工作区和暂存区的修改
HEAD~1指向当前 commit 的前一个

3.4 ⚠️ 重要警告

仅适用于未推送的 commit

# ✅ 安全:本地未推送的 commit
git undo

# ❌ 危险:已推送到远程的 commit
# 会导致本地和远程历史不一致
git undo  # 不要在这种情况下使用!

# ✅ 正确做法:使用 revert
git revert HEAD

3.5 对比:三种撤销方式

命令工作区暂存区适用场景
git reset --soft HEAD~1保留保留修改 commit message
git reset --mixed HEAD~1保留清空重新调整暂存
git reset --hard HEAD~1清空清空彻底丢弃修改

四、Alias 二:git unstage — 取消暂存

4.1 配置

# 传统方式
git config --global alias.unstage 'reset HEAD --'

# 现代方式(Git 2.23+)
git config --global alias.unstage 'restore --staged'

4.2 用法

# 取消所有暂存
git unstage

# 取消特定文件的暂存
git unstage file.txt

# 取消多个文件
git unstage file1.txt file2.txt

4.3 原理

# 传统方式
git reset HEAD --

# 现代方式(推荐)
git restore --staged

4.4 实战场景

# 不小心把所有文件都暂存了
git add .

# 只想提交部分文件,取消暂存不需要的
git unstage config/

# 检查状态
git status

五、Alias 三:git wip — 快速保存进度

5.1 配置

git config --global alias.wip '!git add -A && git commit -m "WIP"'

5.2 用法

# 快速保存当前所有修改
git wip

# 查看 WIP commit
git log --oneline -3

5.3 原理

# 1. 添加所有修改(包括删除)
git add -A

# 2. 创建 WIP commit
git commit -m "WIP"

5.4 使用场景

场景说明
临时切换分支保存当前进度,切换后再恢复
紧急修复保存当前工作,处理紧急任务
下班前保存当天进度,明天继续

5.5 后续处理

# 恢复 WIP 内容
git undo  # 撤销 WIP commit

# 或者在 PR 合并前 squash
git rebase -i HEAD~3  # 将 WIP commit 合并到前面的 commit

5.6 进阶:带时间戳的 WIP

git config --global alias.wip '!git add -A && git commit -m "WIP $(date +%Y-%m-%d %H:%M:%S)"'

六、Alias 四:git lg — 彩色图形化日志

6.1 配置

git config --global alias.lg 'log --oneline --graph --decorate --all --color'

6.2 用法

# 查看完整日志(包含所有分支)
git lg

# 限制显示最近 N 条
git lg -20

# 查看特定分支
git lg --oneline --graph feature-branch

6.3 输出示例

*   8a3b2c1 (HEAD -> main) Merge pull request #123 from feature/new-api
|\
| * d4e5f67 (feature/new-api) Add error handling
| * 1a2b3c4 Implement new API endpoint
|/
*   9g8h7i6 Merge pull request #122 from fix/bug-123
|\
| * 2d3e4f5 Fix null pointer exception
|/
* 5a6b7c8 Update README

6.4 参数详解

参数作用
--oneline每行显示一个 commit
--graph显示分支拓扑图
--decorate显示分支和标签信息
--all显示所有分支
--color彩色输出

6.5 自定义格式

# 显示更多信息
git config --global alias.lg 'log --oneline --graph --decorate --all --color --format="%C(yellow)%h%C(reset) %C(green)%ad%C(reset) %C(blue)%s%C(reset) %C(red)%d%C(reset)" --date=short'

七、Alias 五:git fixup — 修复历史 commit

7.1 配置

git config --global alias.fixup '!f() { git commit --fixup="$1" && GIT_SEQUENCE_EDITOR=true git rebase -i --autosquash "$1"~1; }; f'

7.2 用法

# 修复指定的 commit
git fixup abc123

# 修复最近一次 commit
git fixup HEAD

7.3 工作原理

# 第一步:创建 fixup commit
git commit --fixup=abc123

# 第二步:自动 squash(跳过编辑器)
GIT_SEQUENCE_EDITOR=true git rebase -i --autosquash abc123~1

7.4 实战场景

# 场景:发现之前的 commit 有 bug

# 1. 查看历史找到要修复的 commit
git lg -5

# 2. 修改代码

# 3. 创建 fixup commit(abc123 是要修复的 commit SHA)
git fixup abc123

# 结果:fixup commit 会被自动合并到 abc123

7.5 手动执行方式

# 如果你不想用 alias,可以分步执行:

# 1. 创建 fixup commit
git add .
git commit --fixup=abc123

# 2. 自动 squash
GIT_SEQUENCE_EDITOR=true git rebase -i --autosquash abc123~1

7.6 ⚠️ 注意事项

  • 仅适用于未推送的 commit
  • 需要 Git 2.7+ 支持
  • --autosquash 会自动将 fixup commit 移动到目标 commit 之后

八、Bonus:更多实用 Alias

# 添加到 ~/.gitconfig

[alias]
    # 查看状态(简洁版)
    s = status -sb
    
    # 查看差异(简洁版)
    d = diff --stat
    
    # 查看详细差异
    dc = diff
    
    # 查看分支列表
    b = branch -a
    
    # 切换分支
    co = checkout
    
    # 创建并切换分支
    cob = checkout -b
    
    # 查看远程分支
    rb = remote -v
    
    # 拉取并重新基址
    pullr = pull --rebase
    
    # 推送到当前分支
    pushc = push origin HEAD
    
    # 撤销工作区修改
    discard = checkout --
    
    # 删除已合并的本地分支
    cleanbranches = !git branch --merged | grep -v '*' | xargs -n 1 git branch -d

九、完整配置文件

# ~/.gitconfig

[user]
    name = Your Name
    email = your@email.com

[core]
    editor = vim
    autocrlf = input
    quotepath = false

[color]
    ui = true

[alias]
    # 基础操作
    s = status -sb
    d = diff --stat
    dc = diff
    b = branch -a
    co = checkout
    cob = checkout -b
    rb = remote -v
    
    # 撤销操作
    undo = reset --soft HEAD~1
    unstage = restore --staged
    discard = checkout --
    
    # 快速保存
    wip = !git add -A && git commit -m "WIP"
    
    # 日志查看
    lg = log --oneline --graph --decorate --all --color
    
    # 修复历史
    fixup = !f() { git commit --fixup="$1" && GIT_SEQUENCE_EDITOR=true git rebase -i --autosquash "$1"~1; }; f
    
    # 推送拉取
    pullr = pull --rebase
    pushc = push origin HEAD
    
    # 清理
    cleanbranches = !git branch --merged | grep -v '*' | xargs -n 1 git branch -d

[pull]
    rebase = true

[push]
    default = current

十、Alias 对比总结

Alias命令用途风险等级
undoreset --soft HEAD~1撤销最近一次 commit⚠️ 已推送 commit 不可用
unstagerestore --staged取消暂存文件✅ 安全
wipadd -A && commit -m "WIP"快速保存进度✅ 安全
lglog --oneline --graph --all图形化查看日志✅ 安全
fixupcommit --fixup + rebase --autosquash修复历史 commit⚠️ 已推送 commit 不可用

十一、使用建议

11.1 渐进式学习

第一阶段:基础操作
    ├── git s(状态)
    ├── git d(差异)
    └── git lg(日志)

第二阶段:撤销操作
    ├── git unstage(取消暂存)
    ├── git undo(撤销 commit)
    └── git discard(丢弃修改)

第三阶段:高级操作
    ├── git wip(保存进度)
    ├── git fixup(修复历史)
    └── git cleanbranches(清理分支)

11.2 团队规范

团队协作注意事项:
✅ 不要对已推送的 commit 使用 undo 或 fixup
✅ WIP commit 不要推送到远程分支
✅ 使用 git pullr(pull --rebase)避免不必要的 merge commit
✅ 在合并 PR 前确保提交历史干净

11.3 自定义扩展

# 根据自己的习惯创建更多 alias

# 示例:统计代码提交数
git config --global alias.contrib 'shortlog -sn'

# 示例:查看最近的 tag
git config --global alias.lt 'describe --tags --always'

十二、总结

这 5 个 Git alias 覆盖了日常开发的大部分场景:

  • git undo:快速撤销错误 commit
  • git unstage:取消暂存不需要的文件
  • git wip:保存临时进度
  • git lg:可视化查看分支历史
  • git fixup:优雅修复历史 commit

核心原则

  • 只在本地使用危险操作(undo、fixup)
  • 团队协作时遵守规范
  • 根据自己的工作流定制 alias

💡 互动话题:你常用哪些 Git alias?有没有自己发明的高效命令?欢迎在评论区分享你的经验!


标题:Git 操作总是手忙脚乱?这 5 个 alias 让你效率翻倍
作者:jiangyi
地址:http://jiangyi.space/articles/2026/07/15/1783847790281.html
公众号:服务端技术精选

服务端开发博客:后端架构、高并发、性能优化与微服务实战教程

取消