Jujutsu 版本控制系统入门指南
本指南面向熟悉 Git 的读者,简要介绍 Jujutsu(jj)这个版本控制系统(VCS)。
为什么要试试 Jujutsu?
jj 提供的版本控制原语简单易学,又能优雅地组合出强大的工作流。自动 rebase、原生撤销等特性抚平了 Git 的诸多棱角。用着用着,你会自然而然地发现一些新的高效工作流——用其他大多数 VCS 工具做这些事都太麻烦了。
只要你在用 Git 仓库,今天就可以上手试试。Jujutsu 和 Git 可以在同一个仓库中并存,所以你可以在自己的 git 仓库上试用 jj,完全不必脱离 git。
就我个人而言,jj 显著提升了我的工作效率:它让我不必把版本控制的琐事记在脑子里,自动 rebase 也让堆叠式 diff 工作流变得非常轻松,省去了不少心智负担。而且它用起来真的很有趣。
安装 jj
官方安装说明见这里,以下是几种常见的安装方式:
# macOS
brew install jj
# Arch Linux
pacman -S jujutsu
# NixOS(最好用 nixpkgs-unstable)
nix-shell -p jujutsu
# 从源码安装
#(需要较新的 Rust,且 ~/.cargo/bin 在 PATH 中)
cargo install --locked --bin jj jj-cli
设置与配置
在现有的 git 仓库中运行:
jj git init
此后,jj 与 git 的仓库状态会自动保持同步。
要全局设置用户信息,运行类似下面的命令:
jj config set --user user.name "Some One"
jj config set --user user.email "someone@example.com"
可以用编辑器打开并修改 jj 的用户级 TOML 配置:
jj config edit --user
除了 --user 作用域,jj config 还支持 --repo 和 --workspace。查看合并后的配置可运行:
jj config list
提交与变更
提交(commit),也称修订(revision),本质上与 git 的提交相同。它表示仓库状态的一个快照,该快照派生自一个或多个父仓库快照。通常,它被可视化为相对于单个父提交的 diff,或者在合并提交的情况下,是相对于多个父提交合并后的 diff。
当使用 Git 仓库后端时,jj 的提交 ID与 git 的提交 ID 相同:一个以十六进制字符串形式表示的哈希值。在下方 jj log 的输出中,右侧的十六进制哈希值 2bea5e3d 和 849165bc 就是提交 ID。
$ jj log
@ kpqxywon ethan@example.com 2026-11-15 12:17:12 849165bc
│ (empty) (no description set)
◆ royxmykx ethan@example.com 2026-11-15 12:10:46 main 2bea5e3d
│ Adjust config
~
左侧的 ID(royxmykx 和 kpqxywon)是变更 ID(change ID),它们是逻辑变更在物理演化过程中的稳定标识符。我们将在下一节中详细讨论这些 ID。
提交 ID 和变更 ID 都可以通过无歧义(唯一)的前缀进行简写。为了方便手动输入,jj 会高亮显示大多数 ID 的最短唯一前缀。例如,
无需复制粘贴完整的 kpqxywon,我们只需输入前缀,甚至最短的唯一前缀 k 即可。
我们可以使用短前缀 2 通过 jj show 来检查提交 2bea5e3d。
$ jj show 2
Commit ID: 2bea5e3d1992a60ba9ee52c6ce37c55adba9e8fb
Change ID: royxmykxtrkrqppotnrvutxlvrvqyxmy
Bookmarks: main main@git main@origin
Author : Ethan Lowman <ethan@example.com> (2026-11-15 12:10:46)
Committer: Ethan Lowman <ethan@example.com> (2026-11-15 12:10:46)
Adjust config
diff --git a/config.py b/config.py
index 7366a4e31d..174ffea06b 100644
--- a/config.py
+++ b/config.py
@@ -1,1 +1,1 @@
-DEFAULT_LIMIT = 10
+DEFAULT_LIMIT = 20
很多人会配置自定义的 diff 格式化工具,例如 Delta。有兴趣的话,请参见配置文档。
创建与演进变更
再看一下我们的提交图:
$ jj log
@ kpqxywon ethan@example.com 2026-11-15 12:17:12 849165bc
│ (empty) (no description set)
◆ royxmykx ethan@example.com 2026-11-15 12:10:46 main 2bea5e3d
│ Adjust config
~
在提交 849165bc 的节点上有一个 @ 符号,它表示我们的工作副本的快照,也就是本地目录中实际检出的代码。我们可以用多种方式查看这个特殊的提交指针:
jj show 显示提交详情和 diff,这里是空的。
$ jj show
Commit ID: 849165bca19639dbd6ccb53e8c1340e78c36d59a
Change ID: kpqxywonksrltlrkyqukqqsxpltznztk
Author : Ethan Lowman <ethan@example.com> (2026-11-15 12:17:12)
Committer: Ethan Lowman <ethan@example.com> (2026-11-15 12:17:12)
(no description set)
jj status 显示仓库状态和 diff 统计。
$ jj status
The working copy has no changes.
Working copy (@) : kpqxywon 849165bc (empty) (no description set)
Parent commit (@-): royxmykx 2bea5e3d main | Adjust config
jj diff 的用法和 git diff 类似。这里输出为空,因为当前工作副本相对父提交没有任何改动。
$ jj diff
由于 @ 没有改动,输出没什么看头。我们来做个示例改动看看效果。编辑 main.py 后再运行 jj show,可以看到:
$ jj show
Commit ID: 66b59f20c534705cfb753c722222290e9185b22c
Change ID: kpqxywonksrltlrkyqukqqsxpltznztk
Author : Ethan Lowman <ethan@example.com> (2026-11-15 12:26:43)
Committer: Ethan Lowman <ethan@example.com> (2026-11-15 12:26:43)
(no description set)
diff --git a/main.py b/main.py
new file mode 100644
index 0000000000..c74cd2e0e0
--- /dev/null
+++ b/main.py
@@ -0,0 +1,2 @@ def my_new_feature():
+def my_new_feature():
+ pass
$ jj log
@ kpqxywon ethan@example.com 2026-11-15 12:26:43 66b59f20
│ (no description set)
◆ royxmykx ethan@example.com 2026-11-15 12:10:46 main 2bea5e3d
│ Adjust config
~
你会注意到有几处变化:工作副本已自动提交,@ 不再显示 (empty) 标记。由于我们添加了更改,@ 的新提交 ID 为 66b59f20,但变更 ID 仍为 kpqxywon。jj 不像 git 那样有暂存区,因此无需手动添加文件。在编辑文件后,运行任何用于检查或修改仓库状态的 jj 命令时,jj 会自动将工作副本快照为相关逻辑变更的新物理提交。
让我们为这个变更新增描述(即提交信息)。可以运行 jj desc 打开编辑器,或使用 -m 参数直接提供描述。
$ jj desc -m "Implement the feature"
Working copy (@) now at: kpqxywon 6b89ffb2 Implement the feature
Parent commit (@-) : royxmykx 2bea5e3d main | Adjust config
再次注意,提交 ID 发生了变化,因为哈希值涵盖了提交信息,而变更 ID 保持稳定。
现在,假设我们准备使用新功能,并希望将该变更整理到独立的提交中。可以使用 jj new 创建新提交:
$ jj new
Working copy (@) now at: nlrtlrxv 6cca2add (empty) (no description set)
Parent commit (@-) : kpqxywon 6b89ffb2 Implement the feature
$ jj log
@ nlrtlrxv ethan@example.com 2026-11-15 12:35:49 6cca2add
│ (empty) (no description set)
○ kpqxywon ethan@example.com 2026-11-15 12:33:23 6b89ffb2
│ Implement the feature
◆ royxmykx ethan@example.com 2026-11-15 12:10:46 main 2bea5e3d
│ Adjust config
~
我们在 kpqxywon 之上创建了一个空变更 nlrtlrxv。jj status 显示我们正编辑这个新的修订版本,它目前与其父提交没有差异。
$ jj status
The working copy has no changes.
Working copy (@) : nlrtlrxv 6cca2add (empty) (no description set)
Parent commit (@-): kpqxywon 6b89ffb2 Implement the feature
jj commit 是常用组合 jj desc 和 jj new 的简写形式。让我们再进行一次更改以使用新功能,并将其提交。
$ jj commit -m "Use the feature"
Working copy (@) now at: qmkrwlvp 91067ad0 (empty) (no description set)
Parent commit (@-) : nlrtlrxv e1b06678 Use the feature
$ jj log
@ qmkrwlvp ethan@example.com 2026-11-15 12:41:30 91067ad0
│ (empty) (no description set)
○ nlrtlrxv ethan@example.com 2026-11-15 12:41:30 e1b06678
│ Use the feature
○ kpqxywon ethan@example.com 2026-11-15 12:33:23 6b89ffb2
│ Implement the feature
◆ royxmykx ethan@example.com 2026-11-15 12:10:46 main 2bea5e3d
│ Adjust config
~
要查看刚才创建的提交(n),不能再直接用 jj show,因为它默认显示的是 @,需要显式传入想查看的修订:
$ jj show n
Commit ID: e1b066782aa1a565c1e5d38ef27a308aa58e7e4c
Change ID: nlrtlrxvuuskxprvtuznquqzqzzwvnzk
Author : Ethan Lowman <ethan@example.com> (2026-11-15 12:41:30)
Committer: Ethan Lowman <ethan@example.com> (2026-11-15 12:41:30)
Use the feature
diff --git a/main.py b/main.py
index c74cd2e0e0..0714ec450b 100644
--- a/main.py
+++ b/main.py
@@ -1,2 +1,4 @@ def my_new_feature():
def my_new_feature():
pass
+
+do_something(my_new_feature())
我们可以先给空提交写好描述,在动手之前记录下意图。下面在 k 之上新建一个提交,提醒自己别忘了给这个功能写文档。
$ jj new k -m "Document the feature"
Working copy (@) now at: qwyusntz 46a2ffda (empty) Document the feature
Parent commit (@-) : kpqxywon 6b89ffb2 Implement the feature
Added 0 files, modified 1 files, removed 0 files
不用 jj new 的 -m 参数,事后单独执行一次 jj desc 也完全没问题。当前的提交图如下:
$ jj log
@ qwyusntz ethan@example.com 2026-11-15 12:46:35 46a2ffda
│ (empty) Document the feature
│ ○ nlrtlrxv ethan@example.com 2026-11-15 12:41:30 e1b06678
├─┘ Use the feature
○ kpqxywon ethan@example.com 2026-11-15 12:33:23 6b89ffb2
│ Implement the feature
◆ royxmykx ethan@example.com 2026-11-15 12:10:46 main 2bea5e3d
│ Adjust config
~
注意,我们在没有创建命名分支的情况下生成了分支提交。这突显了 jj 的无分支模型与 git 的分支中心模型在组织工作流上的关键差异。在 Git 中,你需要在分支间来回切换以管理进行中的工作;而在 Jujutsu 中,你是在一个提交图上操作。当准备好共享时,你只需从提交图中将分支推送到 Git 托管平台即可(下文会更详细地介绍这一点)。
Revset(修订集)
Revset 是一个借鉴自 Mercurial 的概念,它是一种表达式,用于从提交图中选择一批修订。大多数 jj 命令的参数都接受 revset。此前运行 jj new k 时,参数 k 实际上是一个简单的 revset,它解析为单个 ID 为 kpqxywon 的变更。jj log 命令提供了一个 -r <REVSET> 标志,可用于预览 revset 的选择结果。以下是一些示例。
@ 的父级:
$ jj log -r @-
○ kpqxywon ethan@example.com 2026-11-15 12:33:23 6b89ffb2
│ Implement the feature
~
r 的孙级:
$ jj log -r r++
@ qwyusntz ethan@example.com 2026-11-15 12:51:45 5b55579a
│ Document the feature
~
○ nlrtlrxv ethan@example.com 2026-11-15 12:41:30 e1b06678
│ Use the feature
~
位于 r 和 @ 之间的提交(含边界):
$ jj log -r r::@
@ qwyusntz ethan@example.com 2026-11-15 12:51:45 5b55579a
│ Document the feature
○ kpqxywon ethan@example.com 2026-11-15 12:33:23 6b89ffb2
│ Implement the feature
◆ royxmykx ethan@example.com 2026-11-15 12:10:46 main 2bea5e3d
│ Adjust config
~
提交 k 及其所有后代:
$ jj log -r k::
@ qwyusntz ethan@example.com 2026-11-15 12:51:45 5b55579a
│ Document the feature
│ ○ nlrtlrxv ethan@example.com 2026-11-15 12:41:30 e1b06678
├─┘ Use the feature
○ kpqxywon ethan@example.com 2026-11-15 12:33:23 6b89ffb2
│ Implement the feature
~
作者为 ethan@example.com,且 ID 为 k 或 q 的提交:
$ jj log -r 'author("ethan@example.com") & (k | q)'
@ qwyusntz ethan@example.com 2026-11-15 12:51:45 5b55579a
│ Document the feature
○ kpqxywon ethan@example.com 2026-11-15 12:33:23 6b89ffb2
│ Implement the feature
~
详尽内容可以参考官方文档。Revsets 语言的规范文档是我查阅最多的 jj 页面,建议大致浏览一遍,了解它能做什么。
在提交图中导航
假设我们在 qwyusntz 里为功能补文档时,发现父提交 kpqxywon 的实现有误。我们不需要手动 commit 或 stash 就能切换任务,只需用 jj edit 把工作区切到 kpqxywon:
$ jj edit k
Working copy (@) now at: kpqxywon 6b89ffb2 Implement the feature
Parent commit (@-) : royxmykx 2bea5e3d main | Adjust config
Added 0 files, modified 0 files, removed 1 files
工作区现在又显示了 kpqxywon 的改动。
$ jj show
Commit ID: 6b89ffb2a250f622a8638145d65fe8c9bb308dbf
Change ID: kpqxywonksrltlrkyqukqqsxpltznztk
Author : Ethan Lowman <ethan@example.com> (2026-11-15 12:26:43)
Committer: Ethan Lowman <ethan@example.com> (2026-11-15 12:33:23)
Implement the feature
diff --git a/main.py b/main.py
new file mode 100644
index 0000000000..c74cd2e0e0
--- /dev/null
+++ b/main.py
@@ -0,0 +1,2 @@ def my_new_feature():
+def my_new_feature():
+ pass
jj 并没有丢掉工作区之上的那些改动,包括我们最近对 q 的编辑:
$ jj log
○ qwyusntz ethan@example.com 2026-11-15 12:51:45 5b55579a
│ Document the feature
│ ○ nlrtlrxv ethan@example.com 2026-11-15 12:41:30 e1b06678
├─┘ Use the feature
@ kpqxywon ethan@example.com 2026-11-15 12:33:23 6b89ffb2
│ Implement the feature
◆ royxmykx ethan@example.com 2026-11-15 12:10:46 main 2bea5e3d
│ Adjust config
~
除了 jj edit k,我们也可以直接运行 jj edit @-。revset @- 会在切换工作区之前求值,所以会切到父提交。内置命令 jj next [--edit] 和 jj prev [--edit] 则提供了在提交栈中上下移动的另一种方式。
自动 rebase
如果我们在工作副本中编辑文件,后代提交会在下次调用 jj 时自动进行变基(rebase)。例如,如果我们在 main.py 中修改了 my_new_feature 的实现,然后运行 jj log,会发现变更 k、n 和 q 都获得了新的提交 ID。jj 也会提示我们它执行了一次变基。
$ jj log
Rebased 2 descendant commits onto updated working copy.
○ qwyusntz ethan@example.com 2026-11-15 13:03:41 7d293f19
│ Document the feature
│ ○ nlrtlrxv ethan@example.com 2026-11-15 13:03:41 41a43a84
├─┘ Use the feature
@ kpqxywon ethan@example.com 2026-11-15 13:03:41 d9f35831
│ Implement the feature
◆ royxmykx ethan@example.com 2026-11-15 12:10:46 main 2bea5e3d
│ Adjust config
~
处理冲突
如果自动变基引发了合并冲突,jj 并不像 git rebase 那样强制要求立即解决。相反,jj 会标记该提交存在冲突,让你继续手头的工作。当堆叠的提交变得越来越复杂时,这种机制能极大减少上下文切换带来的认知负担。
$ jj log
Rebased 2 descendant commits onto updated working copy.
○ qwyusntz ethan@example.com 2026-11-15 13:05:35 b96f8df4
│ Document the feature
│ × nlrtlrxv ethan@example.com 2026-11-15 13:05:35 e562bff4 (conflict)
├─┘ Use the feature
@ kpqxywon ethan@example.com 2026-11-15 13:05:35 c501c0bd
│ Implement the feature
◆ royxmykx ethan@example.com 2026-11-15 12:10:46 main 2bea5e3d
│ Adjust config
~
要查看冲突,我们可以编辑存在冲突的提交……
$ jj edit n
Working copy (@) now at: nlrtlrxv e562bff4 (conflict) Use the feature
Parent commit (@-) : kpqxywon c501c0bd Implement the feature
Added 0 files, modified 1 files, removed 0 files
Warning: There are unresolved conflicts at these paths:
main.py 2-sided conflict
然后检查 main.py 文件。
def my_new_feature():
print("Hello from the new feature!")
<<<<<<< conflict 1 of 1
%%%%%%% diff from: kpqxywon d9f35831 "Implement the feature" (parents of rebased revision)
\\\\\\\ to: kpqxywon c501c0bd "Implement the feature" (rebase destination)
- pass
+ return 42
+++++++ nlrtlrxv 41a43a84 "Use the feature" (rebased revision)
pass
do_something(my_new_feature())
>>>>>>> conflict 1 of 1 ends
在文本编辑器中解决这些冲突后,下次运行任意 jj 命令时,就能看到 jj 已把该提交标记为无冲突状态:
$ jj status
Working copy changes:
M main.py
Working copy (@) : nlrtlrxv 34f2e2a1 Use the feature
Parent commit (@-): kpqxywon c501c0bd Implement the feature
撤销与操作日志
假设我们在解决冲突时出了错,想撤销这次操作。在 git 里,撤销一个操作往往很麻烦,甚至根本做不到;而在 jj 中,随时可以运行 jj undo:
$ jj undo
Undid operation: 35beb0741f17 (2026-11-15 13:08:39) snapshot working copy
Restored to operation: f134ca987b59 (2026-11-15 13:07:18) edit commit e562bff4876f3ea76248c3f730e9e74d3e28e8a0
Working copy (@) now at: nlrtlrxv e562bff4 (conflict) Use the feature
Parent commit (@-) : kpqxywon c501c0bd Implement the feature
Added 0 files, modified 1 files, removed 0 files
Warning: There are unresolved conflicts at these paths:
main.py 2-sided conflict
New conflicts appeared in 1 commits:
nlrtlrxv e562bff4 (conflict) Use the feature
Hint: To resolve the conflicts, start by creating a commit on top of
the conflicted commit:
jj new nlrtlrxv
Then use `jj resolve`, or edit the conflict markers in the file directly.
Once the conflicts are resolved, you can inspect the result with `jj diff`.
Then run `jj squash` to move the resolution into the conflicted commit.
更普遍地说,jj 会通过 jj op log 记录所有操作的历史。我们可以查看这份日志,用 jj op restore 回到其中某个时点,或者用 jj op revert 撤销某一条单独的操作。如果 jj undo 本身是个误操作,还可以用 jj redo 恢复。
上面输出中的 Hint 其实剧透了我们的下一个主题——squash 工作流。
压缩(Squash)工作流
编辑那些已有后续提交的 commit 有时让人心里没底。当你正在一个超出工作记忆承载范围的大 diff 上迭代时,如果能把新改动和现有 diff 分离开,那就舒服多了。比如,我们可能想单独检查那个用于解决 rebase 冲突的 diff。
我们按照上面输出的提示来操作。首先在存在冲突的 commit 之上创建一个新的 commit:
$ jj new n
Working copy (@) now at: wqxolloz b878878a (conflict) (empty) (no description set)
Parent commit (@-) : nlrtlrxv e562bff4 (conflict) Use the feature
Warning: There are unresolved conflicts at these paths:
main.py 2-sided conflict
$ jj log -r n::
@ wqxolloz ethan@example.com 2026-11-15 13:11:44 b878878a (conflict)
│ (empty) (no description set)
× nlrtlrxv ethan@example.com 2026-11-15 13:05:35 e562bff4 (conflict)
│ Use the feature
~
现在,我们可以用编辑器解决 main.py 中的冲突,然后重新运行 jj log:
$ jj log -r n::
@ wqxolloz ethan@example.com 2026-11-15 13:14:12 91181aec
│ (no description set)
× nlrtlrxv ethan@example.com 2026-11-15 13:05:35 e562bff4 (conflict)
│ Use the feature
~
我们可以查看 @ 中的 diff,当我们确认冲突解决得满意后,就可以把它压缩(squash)进父 commit:
$ jj squash
Working copy (@) now at: vkywoywq f6b548b3 (empty) (no description set)
Parent commit (@-) : nlrtlrxv 7208deb7 Use the feature
Existing conflicts were resolved or abandoned from 1 commits.
$ jj log -r n::
@ vkywoywq ethan@example.com 2026-11-15 13:15:39 f6b548b3
│ (empty) (no description set)
○ nlrtlrxv ethan@example.com 2026-11-15 13:15:39 7208deb7
│ Use the feature
~
冲突现已解决,我们可以继续工作了。
更多 Commit 图操作
jj 的一个强大之处在于,它让 diff 像黏土一样可以随意塑形的。凭借自动 rebase 和 jj undo 带来的底气,我们可以轻松地移动、合并、删除或拆分 diff。
我们试着在当前 commit 图上执行几个操作。这是起始状态。
$ jj log
@ vkywoywq ethan@example.com 2026-11-15 13:15:39 f6b548b3
│ (empty) (no description set)
○ nlrtlrxv ethan@example.com 2026-11-15 13:15:39 7208deb7
│ Use the feature
│ ○ qwyusntz ethan@example.com 2026-11-15 13:05:35 b96f8df4
├─┘ Document the feature
○ kpqxywon ethan@example.com 2026-11-15 13:05:35 c501c0bd
│ Implement the feature
◆ royxmykx ethan@example.com 2026-11-15 12:10:46 main 2bea5e3d
│ Adjust config
~
Rebase
我们可以用 jj rebase 把分叉的提交图整理成线性历史。假设想让文档提交(q)排在实现(k)和使用(n)之间,可以把 nlrtlrxv(连同它下面的空提交)移到 qwyusntz 之上:
$ jj rebase -s n -o q
Rebased 2 commits to destination.
Working copy (@) now at: vkywoywq 0405560c (empty) (no description set)
Parent commit (@-) : nlrtlrxv 9920235b Use the feature
Added 1 files, modified 0 files, removed 0 files
$ jj log
@ vkywoywq ethan@example.com 2026-11-15 13:20:18 0405560c
│ (empty) (no description set)
○ nlrtlrxv ethan@example.com 2026-11-15 13:20:18 9920235b
│ Use the feature
○ qwyusntz ethan@example.com 2026-11-15 13:05:35 b96f8df4
│ Document the feature
○ kpqxywon ethan@example.com 2026-11-15 13:05:35 c501c0bd
│ Implement the feature
◆ royxmykx ethan@example.com 2026-11-15 12:10:46 main 2bea5e3d
│ Adjust config
~
要达到同样效果,也可以用 jj rebase -r q --before n。很多 jj 命令都支持多种方式来指定 revset,jj rebase --help 对此有详细说明。
由于 jj rebase 各个参数接受的都是 revset,我们可以写出选中一段连续提交或子图的 revset,把它们整体移动到图中的其他位置。
如果想在 k 之下插入一个提交,最直接的方式是 jj new --before k;不过为了演示,我们也可以换个做法:先在 r 之上创建新提交,再把 k:: rebase 到它上面:
$ jj new r -m "Prepare for the feature"
Working copy (@) now at: wxzmtyol be5ca29b (empty) Prepare for the feature
Parent commit (@-) : royxmykx 2bea5e3d main | Adjust config
Added 0 files, modified 0 files, removed 2 files
$ jj log
@ wxzmtyol ethan@example.com 2026-11-15 13:23:29 be5ca29b
│ (empty) 为功能做准备
│ ○ nlrtlrxv ethan@example.com 2026-11-15 13:20:18 9920235b
│ │ 使用该功能
│ ○ qwyusntz ethan@example.com 2026-11-15 13:05:35 b96f8df4
│ │ 记录该功能文档
│ ○ kpqxywon ethan@example.com 2026-11-15 13:05:35 c501c0bd
├─┘ 实现该功能
◆ royxmykx ethan@example.com 2026-11-15 12:10:46 main 2bea5e3d
│ 调整配置
~
$ jj rebase -s k -o w
Rebased 3 commits to destination.
$ jj log
○ nlrtlrxv ethan@example.com 2026-11-15 13:28:02 cda0de88
│ 使用该功能
○ qwyusntz ethan@example.com 2026-11-15 13:28:02 ec9de16f
│ 记录该功能文档
○ kpqxywon ethan@example.com 2026-11-15 13:28:02 722e7268
│ 实现该功能
@ wxzmtyol ethan@example.com 2026-11-15 13:23:29 be5ca29b
│ (empty) 为功能做准备
◆ royxmykx ethan@example.com 2026-11-15 12:10:46 main 2bea5e3d
│ 调整配置
~
如果你喜爱使用 TUI 界面,jj arrange 提供了一个类似 git rebase -i 的提交图编辑器,用于重新排列提交。
Squash
如果我们决定文档应与实现在同一提交中,可以执行 squash 合并:
$ jj squash --from q --into k
Rebased 1 descendant commits.
jj 会打开编辑器,让我们有机会编写合并后的提交信息。
$ jj log
○ nlrtlrxv ethan@example.com 2026-11-15 13:31:30 cb468825
│ 使用该功能
○ kpqxywon ethan@example.com 2026-11-15 13:31:30 b93e4ef7
│ 实现并记录该功能
@ wxzmtyol ethan@example.com 2026-11-15 13:23:29 be5ca29b
│ (empty) 为功能做准备
◆ royxmykx ethan@example.com 2026-11-15 12:10:46 main 2bea5e3d
│ 调整配置
~
Abandon
若要移除提交图中的某个提交,可以使用 jj abandon。让我们丢弃那个准备阶段的提交:
$ jj abandon w
Abandoned 1 commits:
wxzmtyol be5ca29b (empty) 为功能做准备
Rebased 2 descendant commits onto parents of abandoned commits.
Working copy (@) now at: xzwrrwyv d460fbd4 (empty) (no description set)
Parent commit (@-) : royxmykx 2bea5e3d main | 调整配置
$ jj log
@ xzwrrwyv ethan@example.com 2026-11-15 13:33:49 d460fbd4
│ (empty) (no description set)
│ ○ nlrtlrxv ethan@example.com 2026-11-15 13:33:49 b1284f61
│ │ Use the feature
│ ○ kpqxywon ethan@example.com 2026-11-15 13:33:49 df93f0a6
├─┘ Implement and document the feature
◆ royxmykx ethan@example.com 2026-11-15 12:10:46 main 2bea5e3d
│ Adjust config
~
要丢弃一个提交中的部分内容(某些 hunk 或整个文件的改动),可以使用 jj diffedit 这个 TUI(参见文档)。
拆分(Split)
jj split 的 TUI 可以把一个提交拆成两个连续的提交,只需选择哪些文件或 hunk 归入第一个提交。我们用 jj split -r k 再次把实现和文档拆开:
jj-hunk 项目提供了自动化拆分提交的手段。如果你愿意,还可以让 agent 使用这个工具,把提交拆成一系列更小的改动,方便审查。
Bookmarks 与 Git 分支
jj 没有原生的分支概念,但有一个叫 bookmark(书签)的机制,对应 Git 的分支。需要操作 Git 分支时,我们通过 bookmark 来完成。
用 Git 时你可能从没意识到这一点,但其实 Git 分支不过是指向某个提交的指针。jj 明确按这种方式来建模 bookmark。jj 的 bookmark 本质上是贴在提交上的命名标签,我们可以在修改提交图时随意移动它,也可以推送到或拉取自远程仓库。
下面介绍几个围绕 Git 分支的基本工作流,更完整的指南可以参阅 jj 文档中的Working with GitHub。
推送新分支
假设我们是在一个带有远程仓库的 git 仓库里初始化的 jj,那么运行 jj git remote list 应该能看到一些输出。
$ jj git remote list
origin git@github.com:ethanal/example.git
由于 jj 不依赖分支,我们本地创建的提交没有分支名。幸运的是,jj 不强制要求我们手动指定,而是会自动生成一个。自动生成的分支名让你无需纠结于构思一个独特的名称,从而能更专注于编写高质量的提交信息(通常这是描述变更更具持久性的方式)。
要为变更 ID nlrtlrxv 创建自动生成的书签并推送相应的 Git 分支,可以运行如下命令:
$ jj git push -c n
Creating bookmark push-nlrtlrxvuusk for revision nlrtlrxv
Changes to push to origin:
bookmark: push-nlrtlrxvuusk [add to b1284f61]
git: Enumerating objects: 17, done.
git: Counting objects: 100% (17/17), done.
git: Delta compression using up to 32 threads
git: Compressing objects: 100% (14/14), done.
git: Writing objects: 100% (16/16), 25.21 KiB | 6.30 MiB/s, done.
git: Total 16 (delta 1), reused 5 (delta 0), pack-reused 0 (from 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
remote:
remote: Create a pull request for 'push-nlrtlrxvuusk' on GitHub by visiting:
remote: https://github.com/ethanal/example/pull/new/push-nlrtlrxvuusk
remote:
此时 jj log 显示我们的变更已关联到同名书签 push-nlrtlrxvuusk:
$ jj log -r n
○ nlrtlrxv ethan@example.com 2026-11-15 13:33:49 push-nlrtlrxvuusk b1284f61
│ Use the feature
~
现在,我们可以自由地从该分支在 GitHub 上创建 PR。你可以通过这里记录的 git_push_bookmark 模板配置来自定义书签命名方案。将分支名格式化为 <作者名>/<变更 ID> 是一种常见模式。
由于 jj git push -c 会创建与变更 ID 对应的分支名,这似乎鼓励每个 PR 只包含一个演进中的变更。我认为最好顺应这种人体工学设计,坚持每个 PR 只包含一个变更。
更新分支
更新分支的一般流程是:要么将提交压缩到已书签化的变更中,要么创建新提交并向前移动书签。
书签压缩流程
如果你想将提交 squash 进已有的 bookmark(推荐每个 PR 只保留一个 change),流程如下。
首先,在我们的分支之上创建一个新提交。
$ jj new push-nlrtlrxvuusk
Working copy (@) now at: stzvpxow f14a0cf7 (empty) (no description set)
Parent commit (@-) : nlrtlrxv b1284f61 push-nlrtlrxvuusk | Use the feature
Added 2 files, modified 0 files, removed 0 files
然后进行修改。
$ jj status
Working copy changes:
M README.md
Working copy (@) : stzvpxow 6bdd43dc (no description set)
Parent commit (@-): nlrtlrxv b1284f61 push-nlrtlrxvuusk | Use the feature
把这些改动 squash 进 bookmark 分支。
$ jj squash --from @ --into push-nlrtlrxvuusk
Working copy (@) now at: xryuuprx 04d65a03 (empty) (no description set)
Parent commit (@-) : nlrtlrxv aeb869ae push-nlrtlrxvuusk* | Use the feature
可以看到 push-nlrtlrxvuusk 旁边多了一个星号,表示它还有未推送到远程的改动。那就推一下吧。
$ jj git push -b push-nlrtlrxvuusk
Changes to push to origin:
bookmark: push-nlrtlrxvuusk [move sideways from b1284f61c417 to aeb869aef99b]
远程分支现在已经反映了新的改动。上面的输出说明我们把分支从一个提交"平移"到了另一个提交,也就是做了一次 force-push。jj git push 相当于 git push --force-with-lease,所以如果不确定推送操作会产生什么效果,可以用 jj git push --dry-run 先看看。jj 会拒绝改写一个可配置的不可变提交 revset,从而帮你挡住大多数危险的 force-push。默认的不可变 revset 设置是合理的,你也可以通过配置自己的 immutable heads 来调整严格程度。
Bookmark 前移流程
这个流程是在 bookmark 之上添加新提交,然后把 bookmark 移动到指向新提交。
先准备好新提交。
$ jj new push-nlrtlrxvuusk
Working copy (@) now at: pptrztmx 6e49ff1f (empty) (no description set)
Parent commit (@-) : nlrtlrxv aeb869ae push-nlrtlrxvuusk | Use the feature
然后进行修改。
$ jj status
Working copy changes:
M README.md
Working copy (@) : pptrztmx 89027165 (no description set)
Parent commit (@-): nlrtlrxv aeb869ae push-nlrtlrxvuusk | Use the feature
$ jj commit -m "Add a usage example"
Working copy (@) now at: wunttkrp 044f2d98 (empty) (no description set)
Parent commit (@-) : pptrztmx 2341ee4a Add a usage example
书签不会自动移动到新提交上。这是书签和 Git 分支之间的最大区别。目前 push-nlrtlrxvuusk 仍指向下方提交:
$ jj log -r n::
@ wunttkrp ethan@example.com 2026-11-15 13:51:57 044f2d98
│ (empty) (no description set)
○ pptrztmx ethan@example.com 2026-11-15 13:51:57 2341ee4a
│ Add a usage example
○ nlrtlrxv ethan@example.com 2026-11-15 13:43:16 push-nlrtlrxvuusk aeb869ae
│ Use the feature
~
有多种方式可以将书签移动到新的提交上。最简单的方法大概是使用 jj bookmark advance,它会将最近的书签移动到指定提交上:
$ jj bookmark advance --to @-
Advanced 1 bookmarks to pptrztmx 2341ee4a push-nlrtlrxvuusk* | Add a usage example
现在我们准备推送已移动的书签。
$ jj git push -b push-nlrtlrxvuusk
Changes to push to origin:
bookmark: push-nlrtlrxvuusk [move forward from aeb869aef99b to 2341ee4aa554]
检出远程分支
由于 jj 本质上是“无分支”的,严格来说,我们不会“检出”分支,但可以在远程分支之上创建一个提交。首先执行相当于 git fetch 的操作,导入远程书签:
$ jj git fetch
并在远程分支之上创建一个新的空提交:
$ jj new some-cool-feature@origin
此时我们的工作副本已包含远程分支上的所有提交。但如果在 git 中查看,会发现从 git 的视角看,我们处于 detached head 状态,并未停留在任何分支上。
$ git status
Not currently on any branch.
nothing to commit, working tree clean
这是因为 jj 正在编辑书签上方的提交,而它没有对应 git 中“当前分支”状态的概念。
从主干拉取变更
与主干(如 main)同步只是这个流程的一个具体场景:先 jj git fetch 把本地的 main 更新到上游版本,再把我们的提交变基到新提交之上。例如,把 my-branch 变基到新的 main 上:
jj rebase -b my-branch -o main
在提交图中保持方向感
如果屏幕上能实时看到提交图,用 jj 操作提交图会轻松得多。实现方式有很多种。
jjui 是一个 jj 的 TUI 工具,可以直接在可视化的提交图上进行编辑。我自己没用过,但认识不少喜欢它的人。
我更喜欢用 viddy(一个支持彩色输出的 watch 增强版)在屏幕角落里常驻一个实时刷新的 jj log:
viddy -t -n1 jj log --ignore-working-copy --color=always
建议把这个 log 过滤到一个代表你当前工作的 revset,这里有一个起点供参考:
visible() & (trunk() | (tracked_remote_bookmarks() & ~::trunk()) | (mutable() & mine())::)
你也可以试试我的实验性项目 jj-stamp,它是为 squash 工作流设计的本地代码评审 GUI。即使你不用 squash 评审工作流,它也是个不错的 diff 查看器,侧边有实时 jj log,还内置一个可以运行 jj 命令的终端。
简谈堆叠式 PR
jj 带来的最大生产力提升之一,就是对堆叠式 PR 工作流的支持。这个话题值得单独写一篇,这里先简单说说。
把大改动拆成一系列逻辑步骤,会更容易理解。将一个大变更分解为更小的、按顺序排列的评审单元,能让整个变更更易读。堆叠式 PR 正是利用这一点来提升合并吞吐量,同时消除评审流程中的阻塞。
假设我们要开发一个新功能,可以把实现拆成三个阶段:
- 先做重构,让功能更容易实现
- 实现功能
- 在另一个系统中使用该功能
jj 对 stacked changes 的支持以及自动 rebase,天生就适合实现这三个阶段:每个阶段对应一个 commit,每个 commit 都关联一个 bookmark/branch/PR:
$ jj log -r oomyzqmz::sxummoow
○ sxummoow ethan@example.com 2026-11-15 14:10:48 ethan/sxummoow b5f0af21
│ Use the feature
○ vqpzktsp ethan@example.com 2026-11-15 14:06:27 ethan/vqpzktsp fe52c25f
│ Implement the feature
○ oomyzqmz ethan@example.com 2026-11-15 14:01:19 ethan/oomyzqmz 07f69717
│ Refactor for the feature
~
完成第一个 change 后,你可以发起 PR 供人审查,同时立刻开始下一个 change。整个工作流变成一条长队列:底部的 PR 就绪即合并,顶部不断推进新工作,流水线不会有任何停滞。
用 jj 和 GitHub 手动搭建小型 PR 堆栈并不难,但一旦你迷上这种做法(堆栈深达 5–10 个 PR),就会需要一个工具。jj-pancake 就是我在 stacked PR 工具领域的作品,稍后详细介绍。
资源
- Jujutsu 官方文档。我最常查阅的是 revset 规范。
- Steve Klabnik 的
jj教程是我初次接触jj的入门材料。它应该不是长期维护的资源(我也不敢保证我的会!),但仍值得一读。 - 我订阅了
jj发布的 RSS。最近的版本更新带来了大量新功能。jj整体相当稳定,偶发的 bug 也能很快修复。 - Jujutsu Discord 社区活跃且友好!
- East River Source Control 的博客,这家公司正在打造一个对 Jujutsu 友好的全新代码托管和代码评审平台。Jujutsu 的作者 Martin von Zweigbergk 已加入担任 CTO,团队里还有不少其他高手。也许有朝一日能成为 GitHub 的替代品?我为他们加油。
- 我的个人
jj配置。里面可能有些实用的别名或技巧,不过我没打算把它写得易读。 - Austin Seipp 的
jj配置,他是jj的维护者之一,里面有不少好玩的技巧。