lazygit:终端里把 git 用得比 IDE 快的 TUI

起因

我每天 git status / git add -p / git commit -m / git push /
git log --oneline / git diff HEAD 重复几十次。打字成本不算高但
每次都要看一眼输出再决定下一步,rhythm 容易卡。IDE 的 git 面板鼠标
操作又比键盘慢。

lazygit 是 Go 写的 git TUI:一屏看到 status / branches / log /
stash / commits / files,全键盘操作。

安装

# Debian / Ubuntu (PPA)
LAZYGIT_VERSION=$(curl -s "https://api.github.com/repos/jesseduffield/lazygit/releases/latest" \
  | grep -oE '"tag_name":\s*"v[^"]+"' | sed 's/.*"v\([^"]*\)"/\1/')
curl -Lo lazygit.tar.gz \
  "https://github.com/jesseduffield/lazygit/releases/latest/download/lazygit_${LAZYGIT_VERSION}_Linux_x86_64.tar.gz"
tar xf lazygit.tar.gz lazygit
sudo install lazygit /usr/local/bin
rm lazygit.tar.gz lazygit

# macOS
brew install lazygit

# Windows
scoop install lazygit

在任意 git 仓库里跑:

lazygit
# 或者起个 alias
echo 'alias lg=lazygit' >> ~/.bashrc

关键键位

主屏分 5 个 panel:files / branches / commits / stash / status。
Tab / [ ] 切 panel。

Files panel

  • <space> stage / unstage(按整文件)
  • <enter> 进入文件 → 选行 / hunk → <space> 部分 stage
  • d discard(清除改动)
  • c commit(弹起编辑器写 message)
  • C commit without verifying (--no-verify)
  • a stage all
  • i add to .gitignore

Branches panel

  • <space> checkout
  • n 新建分支
  • d 删除
  • r rebase 选中分支到当前
  • f fast-forward 拉这条分支(不切过去)
  • M merge 进当前分支

Commits panel

  • <space> checkout 这个 commit
  • c cherry-pick
  • r reword commit message
  • e interactive edit (相当于 rebase -i + edit)
  • f fixup 上一条
  • s squash 进上一条
  • d drop
  • <enter> 看 commit diff,再选文件 / hunk 看具体改动

Stash panel

  • s stash 当前改动
  • <space> apply
  • g pop(apply + drop)
  • d drop

Status panel (左上)

  • , switch to recent repos
  • 看 ahead/behind 远端的数量

一些日常操作的"光速" gesture

局部 stage(hunk-level)

进 files → enter 进文件 → ↓↑ 选 hunk → <space> stage 这块 → q 退出 → c commit

git add -p 流畅 5x。

Interactive rebase 一气呵成

commits panel → e(edit)→ 选要 reword 的 → r → 改 message →
选要 fixup 的 → f → m menu → 'continue rebase'

不用记 git rebase -i HEAD~5 的所有 squash/reword/edit/drop 单字母。

解 conflict

merge / rebase 冲突时 lazygit 自动跳到 conflict 面板:

↓↑ 选要保留的版本 (ours / theirs / both) → <space> 选 → 下一处

复杂 conflict 仍要进编辑器手解,但简单的可以 lazygit 内完成。

自动 push / fetch

P  push
p  pull
f  fetch

shift+P 强制推(force push with lease)。

自定义命令

~/.config/lazygit/config.yml

gui:
  showFileTree: true        # 文件树视图
  showRandomTip: false
  language: 'auto'
  scrollHeight: 2

customCommands:
  - key: 'C'
    context: 'files'
    command: "git commit -m '{{.Form.Message}}' --signoff"
    prompts:
      - type: 'input'
        title: 'commit message (with --signoff)'
        key: 'Message'

  - key: 'b'
    context: 'commits'
    description: 'git bisect from this commit'
    command: "git bisect start && git bisect bad HEAD && git bisect good {{.SelectedLocalCommit.Sha}}"

与 nvim / vscode 集成

# 在 vim 里直接打开 lazygit
nnoremap <leader>g :LazyGit<CR>
# 需要装 kdheepak/lazygit.nvim

VSCode 装 lazygit-vscode 扩展,Cmd+Shift+P → LazyGit 启动。

效果

  • daily 流程从 "切窗口 → 打命令 → 看输出 → 想下一步" 变成 "lg → 几个
    字母 → 完成"
  • 复杂的 rebase / cherry-pick / branch 切换不再让我犹豫"忘了命令"
  • 同事被我安利后我推荐过的 5 个人有 4 个换成了 lazygit
  • 老 git 命令照样会用(脚本里、CI 里),lazygit 是 interactive 场景的
    覆盖

踩过的坑

  1. 大仓库(10w+ files)启动慢:lazygit 会扫整个 status。在 monorepo
    里设 git config core.untrackedCache true + core.fsmonitor true 提速。

  2. conflict UI 不显示 conflict marker 颜色:终端不支持 truecolor。
    换 iTerm2 / Alacritty / WezTerm 解决。

  3. 快捷键和 vim 冲突:把 : 作为 lazygit menu 的话和 vim 命令模式
    冲突。x : 改 keybinding。

  4. 跨平台行尾问题:Windows + Linux 协作时 lazygit 显示一堆 "modified
    no diff",是 CRLF 自动转换。git config core.autocrlf 团队统一。

  5. git LFS:lazygit 不知道 LFS 文件,stage 大文件可能不走 LFS pointer。
    git lfs track 配好 .gitattributes。

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

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

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

登录后参与评论。

还没有评论,来说两句。