跳至主要内容

lazygit

Overview

lazygit 是目前最受歡迎的 Git TUI 工具,以 Go 語言編寫,GitHub 上擁有 55k+ stars。它將複雜的 Git 操作轉化為直覺的鍵盤快捷鍵,讓你無需記憶冗長的 Git 命令就能高效管理版本控制。

主要面板配置:

面板功能
Status顯示當前 repo 狀態、remote 資訊
Files檔案變更列表,支援 stage/unstage
Branches分支管理、切換、merge
Commits提交歷史、rebase、cherry-pick
StashStash 管理

面板間使用數字鍵 1-5h/l 切換,操作流暢且一致。

Install

# macOS
brew install lazygit

# Arch Linux
pacman -S lazygit

# Go install
go install github.com/jesseduffield/lazygit@latest

# 建議設定 alias
alias lg="lazygit"

Config

配置檔位於 ~/.config/lazygit/config.yml

gui:
theme:
activeBorderColor:
- green
- bold
selectedLineBgColor:
- reverse
showFileTree: true
showRandomTip: false
nerdFontsVersion: "3"

git:
paging:
pager: delta --paging=never # 整合 git-delta,提供語法高亮 diff
autoFetch: true
autoRefresh: true

os:
editPreset: "nvim" # 或 vscode, sublime 等

整合 git-delta 可大幅提升 diff 閱讀體驗,是必裝的搭配工具。

Keybindings

常用操作速查:

按鍵功能說明
spaceStage/Unstage切換檔案暫存狀態
aStage all暫存所有變更
cCommit撰寫 commit message
PPush推送到 remote
pPull拉取更新
rRebase互動式 rebase
dDrop/Discard丟棄變更或刪除 commit
eEdit file用編輯器開啟檔案
wWorktree管理 worktree
Ctrl+zUndo基於 reflog 的撤銷操作
?Help顯示當前面板快捷鍵

Custom Commands

自訂命令是 lazygit 的殺手級功能,可在 config.yml 中定義複雜的 Git 工作流程:

customCommands:
- key: "C"
context: "files"
command: "git commit -m '{{.Form.Type}}({{.Form.Scope}}): {{.Form.Message}}'"
prompts:
- type: input
title: "Type (feat/fix/docs/refactor):"
key: Type
- type: input
title: "Scope:"
key: Scope
- type: input
title: "Message:"
key: Message

- key: "<c-f>"
context: "global"
command: "git fetch --all --prune"
description: "Fetch all remotes"

- key: "G"
context: "commits"
command: "git revert {{.SelectedLocalCommit.Sha}}"
description: "Revert commit"

透過 prompts 支援互動式輸入(input、menu、confirm),可以建構 Conventional Commits 等複雜流程。

Comparison with gitui

特性lazygitgitui
語言GoRust
啟動速度普通快 2x
記憶體用量較高約 1/15
自訂命令✅ 強大
Undo (Ctrl+Z)
Bisect
Worktree
Interactive Rebase✅ 拖放式✅ 基本
社群規模55k+ stars18k+ stars
介面複雜度功能豐富簡潔直覺

使用策略

  • 日常開發alias lg=lazygit,享受完整功能和自訂命令
  • 大型 Repo / 快速操作:gitui 更適合單純的 stage + commit,在超大 repo 中效能明顯更好

兩者並非互斥,根據場景選擇最合適的工具。

Advanced Features

Undo (Ctrl+Z)

基於 git reflog 實現的撤銷功能,幾乎所有操作都可以復原,包括 commit、rebase、merge 等。這是 lazygit 最讓人安心的功能之一。

Interactive Rebase

在 Commits 面板中按 r 進入互動式 rebase,支援:

  • 拖放式重新排序 commits(Ctrl+j/k
  • Squash (s)、Fixup (f)、Edit (e)、Drop (d)
  • 修改任意 commit message(r

Cherry-pick 跨分支

  1. 在來源分支的 Commits 面板選取 commit,按 C 複製
  2. 切換到目標分支
  3. V 貼上(cherry-pick)

Worktree 管理

w 進入 worktree 管理,可同時在多個分支上工作而無需 stash。

Bisect

在 Commits 面板按 b 啟動 bisect,標記 good/bad 快速定位問題 commit。

See Also