lazygit:终端里的 git GUI,告别一长串 git 命令

起因

git 命令多 + 难记。常见操作经常要 4-5 个步骤:

  • 看 diff → stage 部分 → unstage 错的 → commit → push
  • 找某个 commit 看做了啥 → cherry-pick → push
  • rebase 时改某个 commit message
  • 一个文件 history 看每行 blame

老办法:git status + git diff + git add -p(交互 hunk)+ git commit
... 一连串。

GUI 方案(GitKraken / Tower / Sourcetree)功能强但要离开终端 + 收费 +
启动慢。

lazygit 是终端 TUI,5 秒启动 + 键盘操作 + 覆盖 90% git 工作流。

brew install lazygit
# 或
apt install lazygit       # ubuntu 22.04+ ppa

启动

仓库目录里:

lazygit

或者 alias lg='lazygit'

UI 5 个 panel:

┌─Status─────┬─Files──────────────────────────────────┐
│   master   │ M  app/views.py                       │
│            │ ?? new_file.md                        │
├─Branches───┤                                       │
│   master   ├─Diff/Output──────────────────────────┤
│   feature  │ + new line                           │
│            │ - old line                           │
├─Commits────┤                                       │
│ abc Update │                                       │
│ def Fix    │                                       │
└────────────┴──────────────────────────────────────┘

Tab 切 panel,? 看快捷键。

常用操作

stage / commit / push

1. j/k 在 Files panel 选文件
2. space 切换 stage / unstage
3. enter 进 hunk view,space stage 单个 hunk
4. c 写 commit message
5. P push

vs git add -p 交互的痛苦:lazygit 可视化 + 按键即操作。

看 diff

选文件 → 右边 panel 自动显示 diff。
高亮 / 上下滚(PgUp/PgDn) / 同步两窗口(vimdiff like 用 i 触发)。

切换分支

Branches panel:

1. Tab 到 Branches
2. j/k 选分支
3. space checkout
4. n 新建分支
5. d 删除

rebase / cherry-pick

Commits panel:

1. Tab 到 Commits
2. r 选中 commit 改 message
3. s squash 进上一个 commit
4. f fixup(squash + 用上一个 message)
5. e edit commit(修改文件后 continue)
6. d drop commit

interactive rebase 不用记 git rebase -i HEAD~5 + vim 编辑 todo list。
lazygit 里上下选 + 按键。

stash 操作

S      # stash all
g      # 弹 stash 列表
space  # pop

log + blame

y    # show log graph
b    # blame current file

remote 操作

P    # push(可选 force / 不同 remote)
p    # pull
f    # fetch

配置

~/.config/lazygit/config.yml

gui:
  theme:
    lightTheme: false
  showCommandLog: false
  nerdFontsVersion: "3"     # 用 NerdFont 图标

git:
  autoFetch: false          # 默认每分钟 fetch,关掉省网络
  paging:
    colorArg: always
    pager: delta --paging=never   # 用 delta 渲染 diff

keybinding:
  files:
    commitChanges: "c"

customCommands:
  - key: "C"
    description: "Commit conventional"
    command: "git commit -m '{{.Form.Type}}({{.Form.Scope}}): {{.Form.Msg}}'"
    context: "files"
    prompts:
      - type: "menu"
        title: "Type"
        options:
          - { name: "feat", value: "feat" }
          - { name: "fix",  value: "fix" }
          - { name: "chore", value: "chore" }
      - type: "input"
        title: "Scope"
        key: "Scope"
      - type: "input"
        title: "Message"
        key: "Msg"

customCommands 强大:自定义快捷工作流。Shift-C 启动 conventional
commit prompt。

与 git 命令对比

操作 git 命令 lazygit
stage 部分 git add -p + y/n space space space
commit git commit -m "..." c → 输入
切分支 git checkout xxx Tab → 选 → space
rebase 改 message git rebase -i HEAD~3 + edit r
stash pop git stash pop g → space
看历史 git log --oneline 主屏右下自动

熟练后 lazygit 快 2-3x。新人上手时间从 git CLI 几周 → lazygit
几小时。

与 GitUI / tig / gitk 对比

  • GitUI:Rust 写,比 lazygit 启动还快,但功能略少(无 customCommands)
  • tig:老牌,只读 history 浏览强,操作交互弱
  • gitk:Tcl/Tk GUI,老土但 history graph 经典

我用 lazygit 做日常 + tig 看复杂 history graph。

与 IDE 内置 git 工具对比

VS Code / JetBrains 自带 git GUI 也好用。
lazygit 优势:

  • 在终端里(vim / tmux 用户友好)
  • 跨编辑器一致体验
  • 远程 ssh 服务器也能用(GUI 没法用)

我同时用:IDE 看 diff / cherry-pick UI;lazygit 做 rebase / stage。

tmux popup 启动

~/.tmux.conf

bind g display-popup -E -w 90% -h 90% lazygit

任意窗口 prefix+g 弹 lazygit popup → 操作完关掉。完美。

踩过的坑

  1. 大仓库慢:几十万 commit 的 repo(如 chromium)lazygit 启动几秒
    load。logCmd config 可以限制。

  2. submodule 不直观:lazygit 对 submodule 支持有限。
    submodule 切换要 enter 进 sub view。

  3. conflict 解决:merge conflict 时 lazygit 提示要手动编辑文件再回来。
    有简单的 file marker view 但复杂 conflict 还是开编辑器。

  4. commit 模板没用上:git commit.template 配的模板 lazygit
    commit 命令不自动用。要在 lazygit config 里另外配 commitPrefix

  5. 远程 / fetch 不显:拉新分支后 lazygit 没自动刷新。R refresh 一下。

精确评价 共 0 人评价
可复现性
可复现 · 0 不可复现 · 0
文风
文风流畅 · 0 文风晦涩 · 0
立场
支持 · 0 反对 · 0

登录后即可对本帖作出评价。

评论区 0 条 · 所有人可在此交流

登录后参与评论。

还没有评论,来说两句。