A Better Merge Workflow with Jujutsu

Introduction

Since reading Chris Krychoโ€™s essay introduction to Jujutsu, Iโ€™ve been excited about the possibilities of a more modern, user-friendly Version Control System (VCS) tool. For those of you who arenโ€™t familiar, Jujutsu (binary name: jj) is a new VCS which is compatible with existing Git repositories. Iโ€™ve been using Jujutsu as my daily Git driver for a while now. Though itโ€™s still experimental softwareโ€”with its fair share of bugs and unimplemented features, it has made my day-to-day interactions with Git repositories a much more pleasant experience.

Thereโ€™s a really cool workflow that Austin Seipp shared on Jujutsuโ€™s Discord, which Iโ€™m beginning to use everywhere, that I thought was worth writing more about. He calls it The Austinโ„ข Mega Merge Strategyยฎ, but meโ€”Iโ€™m just going to call it for what it is: a Better Workflow for Manipulating Merge Commits in Jujutsu1. This workflow makes it easy to simultaneously activate multiple branches in the same working directory.

Before I go through the workflow, letโ€™s take a look at some of the basics of Jujutsu.

A quick primer on Jujutsu

Thereโ€™s no better way to demonstrate a VCS than by using it on its own repository. Jujutsu is compatible with Git repositories, and its own repository is a Git repository as well, hosted on GitHub.

Hereโ€™s the terminal output of jj log, which displays the graph of commits in the repository:

โฏ jj log -r ::@
@  ytruvzpy benjamin@dev.ofcr.se 11 seconds ago e1b74dba
โ”‚  (empty) (no description set)
โ—‰  oqtzskyx martinvonz@google.com 1 day ago v0.16.0 HEAD@git 2dcdc7fb
โ”‚  release: release version 0.16.0
โ—‰  kyxszyrv 49699333+dependabot[bot]@users.noreply.github.com 1 day ago 6826be4a
โ”‚  cargo: bump the cargo-dependencies group with 2 updates
โ—‰  rxqmmmry yuya@tcha.org 2 days ago 363b5084
โ”‚  cli: ditch Deref, implement AsRef and Display for RevisionArg instead
โ—‰  zynytznv yuya@tcha.org 2 days ago c596d457
โ”‚  cli: migrate singular parse/resolve revset argument to RevisionArg
โ—‰  wnkplmms yuya@tcha.org 2 days ago 311bdbf5
โ”‚  cli: use RevisionArg type in "resolve -r", "bench", and example command
โ—‰  usyxoklz yuya@tcha.org 2 days ago ae91adba
โ”‚  cli: preserve RevisionArg type as much as possible
โ—‰  prqkmmqn yuya@tcha.org 2 days ago 426ee1c1
โ”‚  cli: abuse Cow to declare RevisionArg("@") constant
โ—‰  zrpqktts dev@noahmayr.com 2 days ago 88a4a828
โ”‚  cli: add better error message when immutable_heads() cannot be resolved
โ—‰  nopsqtrw dev@noahmayr.com 2 days ago b7998488
โ”‚  cli: only use default log revset when neither path nor revset is provided
โ—‰  rrtlsuyn ilyagr@users.noreply.github.com 2 days ago 670e6ac6
โ”‚  cmd `squash`: alias `--to` for the `--into` flag

Thereโ€™s a couple of things to note, which differ from Git:

  1. Jujutsu has separate notions of changes and commits. Changes are identified by the alphabetical IDs on the left of the log, whilst commits are identified by the hexadecimal IDs on the right. When using Jujutsuโ€™s Git backend, commits are just Git commits (the ID is the commit SHA), whereas a change is just a constant ID with an associated commit2.

    Change IDs address one of the pain points of Git: what we call โ€œamending a commitโ€ actually creates a brand new commit object with a different ID. After amending a commit, youโ€™d need to check the status message or go back to the commit log to get the new commit ID to address the commit again.

    Using Jujutsu, โ€œamending a commitโ€ also produces a new commit object, as in Git, but the new commit has the same change ID as the original. The same ID always represent the same change, and points to the latest version of the commit, no matter how much you amend the commit history. This is really useful if youโ€™re doing a lot of such operations.

    (Theyโ€™re also really helpful in allowing you to understand how your change has evolved over time, although we wonโ€™t be going through that much in this article.)

  2. Jujutsu has a language called revsets (similar to Mercurialโ€™s revsets), which allows you to select commits based on given properties. jj log -r [REVSET] only displays the commits in the log which the revset evaluates to. jj log -r ::@ shows all ancestors of the current working copy commit (denoted by @), and is the equivalent to git log. Revsets allow for much more succinct and expressive filters than possible in Git.

  3. Jujutsu removes the concept of the index by using working copy commits. Changes made in the repository always update the working copy commit, and do not need to be separately staged and committed. Once youโ€™re done working on a change, jj commit will update the commit description and create a new empty change3, or you can use jj commit -i to interactively select the changes you want and split the working copy commit into two.

Creating a new merge commit

In the repository, Iโ€™ve been working on a few distinct features, some of which Iโ€™ve already pushed to various branches in my fork of the repository. Letโ€™s take a look at the commits that Iโ€™m interested inโ€”commits for which Iโ€™m the author, and have been pushed to a remote branch:

โฏ jj log -r "mine() & remote_branches()"
โ—‰  nvympzuk benjamin@dev.ofcr.se 1 day ago push-nvympzukzzqo ebac1982
โ”‚  rebase: refactor to `MutRepo::set_extracted_commit` API
~
 
โ—‰  wtmqulxn benjamin@dev.ofcr.se 2 days ago push-uqxvnturzsuu 932e59d2
โ”‚  rebase: allow both `--insert-after` and `--insert-before` to be used simultaneously
~
 
โ—‰  qklyrnvv benjamin@dev.ofcr.se 2 days ago push-qklyrnvvuksv 579ecb73
โ”‚  cli: print conflicted paths whenever the working copy is changed
~
 
โ—‰  zozvwmow benjamin@dev.ofcr.se 3 days ago ssh-openssh ea93486e
โ”‚  git: update error message for SSH error to stop referencing libssh2
~
 
โ—‰  xstwkkpp benjamin@dev.ofcr.se 1 month ago push-syolynqvlwsl 4d3bb253
โ”‚  git: Add revset alias for `trunk()` on `git clone`
~

Iโ€™d like to create a new merge commit which includes the changes from zoz and qkl. With Jujutsu, you can use jj new to start working on a new change, and specify any number of parent changes:

โฏ jj new zoz qkl
Working copy now at: orllnptq 5ea75c06 (empty) (no description set)
Parent commit      : zozvwmow ea93486e ssh-openssh | git: update error message for SSH error to stop referencing libssh2
Parent commit      : qklyrnvv 579ecb73 push-qklyrnvvuksv | cli: print conflicted paths whenever the working copy is changed
Added 0 files, modified 14 files, removed 0 files

This creates a new merge commit with the change ID of orl, with the 2 parents specified. Note that you can specify as many parents as you want, and Jujutsu can still merge them. (Iโ€™m only specifying 2 here, so I can add more later manually.) Hereโ€™s what the commit graph looks like at this point:

โฏ jj log
@    orllnptq benjamin@dev.ofcr.se 34 seconds ago 5ea75c06
โ”œโ”€โ•ฎ  (empty) (no description set)
โ”‚ โ—‰  qklyrnvv benjamin@dev.ofcr.se 2 days ago push-qklyrnvvuksv 579ecb73
โ”‚ โ”‚  cli: print conflicted paths whenever the working copy is changed
โ”‚ ~
โ”‚
โ—‰  zozvwmow benjamin@dev.ofcr.se 3 days ago ssh-openssh HEAD@git ea93486e
โ”‚  git: update error message for SSH error to stop referencing libssh2
โ—‰  yowkkkqn benjamin@dev.ofcr.se 3 days ago 6222cb24
โ”‚  git: use prerelease version of `git2` with OpenSSH support
~

A new merge workflow?

Merge commits, you might be wondering. Is that a new workflow? Canโ€™t you just use Git for this?

Merge commits definitely arenโ€™t anything newโ€”nearly every modern VCS tool has merge commits. However, Jujutsuโ€™s support for manipulating the commit graph is miles ahead of Gitโ€™s. With Jujutsu, you can merge commits without fear of modifying your repository to an unrecoverable state. Jujutsuโ€™s first-class conflicts and jj undo makes it safe to merge different branches, play around with different configurations of your code, and then restore your original changes.

Whether you find this article useful likely depends on how youโ€™re using your VCS right now. If youโ€™re just building a linear stack of commits, then this is probably not going to be very helpful. However, if you use separate branches to work on different features and group commits together for code review, then you might find this useful. (If youโ€™ve read Jackson Gabbardโ€™s article on Stacked Diffs vs Pull Requests, I like to think that this workflow allows you to enjoy the benefits of a Stacked Diff-like workflow of working on a single branch, but still allows you to work with code forges like GitHub which expect a Pull Request-style workflow.)

The gist of this workflow is basically: merge all or as many of your branches/commits together as you need, and keep that combined merge commit in your working directory.

Why is this useful? Some good usecases include:

Modifying existing merge commits is difficult using Git, but is much simpler with Jujutsu. Letโ€™s go through a few examples.

Adding a new parent from an existing commit

Hereโ€™s how to add another parent to the merge commit:

โฏ jj rebase -s orl -d "all:orl-" -d wtm
Rebased 1 commits
Working copy now at: orllnptq dd20e255 (empty) (no description set)
Parent commit      : qklyrnvv 579ecb73 push-qklyrnvvuksv | cli: print conflicted paths whenever the working copy is changed
Parent commit      : zozvwmow ea93486e ssh-openssh | git: update error message for SSH error to stop referencing libssh2
Parent commit      : wtmqulxn 932e59d2 push-uqxvnturzsuu | rebase: allow both `--insert-after` and `--insert-before` to be used simultaneously
Added 0 files, modified 3 files, removed 0 files

This command rebases the commit with change ID orl and all its descendants on top of all the given destinations. The given destinations here were all:orl-, which means all of orlโ€™s existing parents, as well as the new destination of wtm. Weโ€™ve now got a new merge commit with the 2 original parents and the new one:

โฏ jj log
@      orllnptq benjamin@dev.ofcr.se 20 seconds ago dd20e255
โ”œโ”€โ”ฌโ”€โ•ฎ  (empty) (no description set)
โ”‚ โ”‚ โ—‰  wtmqulxn benjamin@dev.ofcr.se 2 days ago push-uqxvnturzsuu 932e59d2
โ”‚ โ”‚ โ”‚  rebase: allow both `--insert-after` and `--insert-before` to be used simultaneously
โ”‚ โ”‚ โ—‰  uqxvntur benjamin@dev.ofcr.se 2 days ago 2ac431c5
โ”‚ โ”‚ โ”‚  rebase: add `--insert-after` and `--insert-before` options
โ”‚ โ”‚ โ—‰  nkzsqppm benjamin@dev.ofcr.se 2 days ago ecf4a6e8
โ”‚ โ”‚ โ”‚  rebase: extract out some functions from `rebase_revision`
โ”‚ โ”‚ ~
โ”‚ โ”‚
โ”‚ โ—‰  zozvwmow benjamin@dev.ofcr.se 3 days ago ssh-openssh ea93486e
โ”‚ โ”‚  git: update error message for SSH error to stop referencing libssh2
โ”‚ โ—‰  yowkkkqn benjamin@dev.ofcr.se 3 days ago 6222cb24
โ”‚ โ”‚  git: use prerelease version of `git2` with OpenSSH support
โ”‚ ~
โ”‚
โ—‰  qklyrnvv benjamin@dev.ofcr.se 2 days ago push-qklyrnvvuksv HEAD@git 579ecb73
โ”‚  cli: print conflicted paths whenever the working copy is changed
~

If youโ€™ve got any other commits on top of the one you specified for -s, Jujutsu also correctly rebases all of the original commitโ€™s descendants on top of the new commit, so you donโ€™t have to worry about those commits going out of sync.

Rebasing all parents

In this case, I realized that all my feature branches were outdated, and Iโ€™d like to rebase them on top of main. Hereโ€™s the command to do that:

โฏ jj rebase -s 'all:roots(main..@)' -d main
Rebased 9 commits
Working copy now at: orllnptq 6e4f5799 (empty) (no description set)
Parent commit      : qklyrnvv 28af9083 push-qklyrnvvuksv* | cli: print conflicted paths whenever the working copy is changed
Parent commit      : zozvwmow c6c73906 ssh-openssh* | git: update error message for SSH error to stop referencing libssh2
Parent commit      : wtmqulxn 8673733e push-uqxvnturzsuu* | rebase: allow both `--insert-after` and `--insert-before` to be used simultaneously
Added 0 files, modified 3 files, removed 0 files

The syntax is a bit of a doozy, but can be better understood by breaking it down part-by-part:

  1. main..@: This finds all ancestors of @, the working copy commit, which are not ancestors of main. (By coincidence, my default configured revset for jj log shows exactly all the commits in the main..@ set.)
  2. roots(main..@): This gets the roots of commits in main..@ set, which are commits that do not have any ancestors within the set. This evaluates to the first commit of each arm of the merge commit in the log above (qkl, yow, and zoz).
  3. all:roots(main..@): The all prefix is required since -s expects a single commit by default, but roots(main..@) evaluates to multiple commits.

Each of these 3 commits are rebased on top of the destination, main, and have their descendants automatically rebased as well. This results in a subgraph where the root is main, and the leaf is the merge commit with its 3 parents:

โฏ jj log
@      orllnptq benjamin@dev.ofcr.se 1 minute ago 6e4f5799
โ”œโ”€โ”ฌโ”€โ•ฎ  (empty) (no description set)
โ”‚ โ”‚ โ—‰  wtmqulxn benjamin@dev.ofcr.se 1 minute ago push-uqxvnturzsuu* 8673733e
โ”‚ โ”‚ โ”‚  rebase: allow both `--insert-after` and `--insert-before` to be used simultaneously
โ”‚ โ”‚ โ—‰  uqxvntur benjamin@dev.ofcr.se 1 minute ago dd7454a2
โ”‚ โ”‚ โ”‚  rebase: add `--insert-after` and `--insert-before` options
โ”‚ โ”‚ โ—‰  nkzsqppm benjamin@dev.ofcr.se 1 minute ago 0a949714
โ”‚ โ”‚ โ”‚  rebase: extract out some functions from `rebase_revision`
โ”‚ โ—‰ โ”‚  zozvwmow benjamin@dev.ofcr.se 1 minute ago ssh-openssh* c6c73906
โ”‚ โ”‚ โ”‚  git: update error message for SSH error to stop referencing libssh2
โ”‚ โ—‰ โ”‚  yowkkkqn benjamin@dev.ofcr.se 1 minute ago ffec92c9
โ”‚ โ”œโ”€โ•ฏ  git: use prerelease version of `git2` with OpenSSH support
โ—‰ โ”‚  qklyrnvv benjamin@dev.ofcr.se 1 minute ago push-qklyrnvvuksv* HEAD@git 28af9083
โ”œโ”€โ•ฏ  cli: print conflicted paths whenever the working copy is changed
โ—‰  oqtzskyx martinvonz@google.com 1 day ago main* v0.16.0 2dcdc7fb
โ”‚  release: release version 0.16.0
~

Here, weโ€™ve automatically rebased all the changes weโ€™re interested in with just a single command! ๐Ÿ˜ฒ

Adding a new parent from new changes

Whilst testing out the features from these different changes, you might want to work on a new change. Instead of having to check out a new branch as you would in Git, you can just work on the new change on top of this merge commit:

โฏ jj new
Working copy now at: rwqywnzl 461d45c8 (empty) (no description set)
Parent commit      : orllnptq 6e4f5799 (empty) (no description set)
 
โฏ nvim
 
โฏ jj commit -m "new: avoid manual `unwrap()` call"
Working copy now at: ovypxnus e0c160c9 (empty) (no description set)
Parent commit      : rwqywnzl 919fae76 new: avoid manual `unwrap()` call
 
โฏ jj show rwq
Commit ID: 919fae76dccba57d1df3df3125f4d4eac6676ce9
Change ID: rwqywnzlzmnoqrqkosupxwtyrxumymxs
Author: Benjamin Tan <benjamin@dev.ofcr.se> (23 hours ago)
Committer: Benjamin Tan <benjamin@dev.ofcr.se> (23 hours ago)
 
    new: avoid manual `unwrap()` call
 
diff --git a/cli/src/commands/new.rs b/cli/src/commands/new.rs
index eeeb50aee6...e0defba129 100644
--- a/cli/src/commands/new.rs
+++ b/cli/src/commands/new.rs
@@ -193,7 +193,7 @@
             writeln!(formatter)?;
         }
     } else {
-        tx.edit(&new_commit).unwrap();
+        tx.edit(&new_commit)?;
         // The description of the new commit will be printed by tx.finish()
     }
     if num_rebased > 0 {

Hereโ€™s the updated commit graph, with the new commit (change ID rwq) as a child of the merge commit:

โฏ jj log
@  ovypxnus benjamin@dev.ofcr.se 1 minute ago e0c160c9
โ”‚  (empty) (no description set)
โ—‰  rwqywnzl benjamin@dev.ofcr.se 1 minute ago HEAD@git 919fae76
โ”‚  new: avoid manual `unwrap()` call
โ—‰      orllnptq benjamin@dev.ofcr.se 15 minutes ago 6e4f5799
โ”œโ”€โ”ฌโ”€โ•ฎ  (empty) (no description set)
โ”‚ โ”‚ โ—‰  wtmqulxn benjamin@dev.ofcr.se 15 minutes ago push-uqxvnturzsuu* 8673733e
โ”‚ โ”‚ โ”‚  rebase: allow both `--insert-after` and `--insert-before` to be used simultaneously
โ”‚ โ”‚ โ—‰  uqxvntur benjamin@dev.ofcr.se 15 minutes ago dd7454a2
โ”‚ โ”‚ โ”‚  rebase: add `--insert-after` and `--insert-before` options
โ”‚ โ”‚ โ—‰  nkzsqppm benjamin@dev.ofcr.se 15 minutes ago 0a949714
โ”‚ โ”‚ โ”‚  rebase: extract out some functions from `rebase_revision`
โ”‚ โ—‰ โ”‚  zozvwmow benjamin@dev.ofcr.se 15 minutes ago ssh-openssh* c6c73906
โ”‚ โ”‚ โ”‚  git: update error message for SSH error to stop referencing libssh2
โ”‚ โ—‰ โ”‚  yowkkkqn benjamin@dev.ofcr.se 15 minutes ago ffec92c9
โ”‚ โ”œโ”€โ•ฏ  git: use prerelease version of `git2` with OpenSSH support
โ—‰ โ”‚  qklyrnvv benjamin@dev.ofcr.se 15 minutes ago push-qklyrnvvuksv* 28af9083
โ”œโ”€โ•ฏ  cli: print conflicted paths whenever the working copy is changed
โ—‰  oqtzskyx martinvonz@google.com 1 day ago main* v0.16.0 2dcdc7fb
โ”‚  release: release version 0.16.0
~

Although this change was made on top of the merge commit, you typically wouldnโ€™t want to leave it there for long. Youโ€™d likely want to rebase it to a better location (not on top of the mega merge commit), before sending the change up for code review. For example, you can first rebase the new change onto main:

โฏ jj rebase -r rwq -d main
Also rebased 1 descendant commits onto parent of rebased commit
Working copy now at: ovypxnus 68bacc1f (empty) (no description set)
Parent commit      : orllnptq 6e4f5799 (empty) (no description set)
Added 0 files, modified 1 files, removed 0 files

The -r option rebases only the given revision on top of the destination; it rebases all of its descendants on top of its parents. Effectively, this is similar to moving a commit to another location in the graph.

After rebasing onto main, you can then add rwq as a new parent of the merge commit to keep the change applied to your working directory:

โฏ jj rebase -s orl -d "all:orl-" -d rwq
Rebased 2 commits
Working copy now at: ovypxnus 7f278c0d (empty) (no description set)
Parent commit      : orllnptq 7b028dc9 (empty) (no description set)
Added 0 files, modified 1 files, removed 0 files
 
โฏ jj log
@  ovypxnus benjamin@dev.ofcr.se 18 seconds ago 7f278c0d
โ”‚  (empty) (no description set)
โ—‰        orllnptq benjamin@dev.ofcr.se 18 seconds ago HEAD@git 7b028dc9
โ”œโ”€โ”ฌโ”€โ”ฌโ”€โ•ฎ  (empty) (no description set)
โ”‚ โ”‚ โ”‚ โ—‰  rwqywnzl benjamin@dev.ofcr.se 47 seconds ago 402f7ad8
โ”‚ โ”‚ โ”‚ โ”‚  new: avoid manual `unwrap()` call
โ”‚ โ”‚ โ—‰ โ”‚  qklyrnvv benjamin@dev.ofcr.se 23 minutes ago push-qklyrnvvuksv* 28af9083
โ”‚ โ”‚ โ”œโ”€โ•ฏ  cli: print conflicted paths whenever the working copy is changed
โ”‚ โ—‰ โ”‚  zozvwmow benjamin@dev.ofcr.se 23 minutes ago ssh-openssh* c6c73906
โ”‚ โ”‚ โ”‚  git: update error message for SSH error to stop referencing libssh2
โ”‚ โ—‰ โ”‚  yowkkkqn benjamin@dev.ofcr.se 23 minutes ago ffec92c9
โ”‚ โ”œโ”€โ•ฏ  git: use prerelease version of `git2` with OpenSSH support
โ—‰ โ”‚  wtmqulxn benjamin@dev.ofcr.se 23 minutes ago push-uqxvnturzsuu* 8673733e
โ”‚ โ”‚  rebase: allow both `--insert-after` and `--insert-before` to be used simultaneously
โ—‰ โ”‚  uqxvntur benjamin@dev.ofcr.se 23 minutes ago dd7454a2
โ”‚ โ”‚  rebase: add `--insert-after` and `--insert-before` options
โ—‰ โ”‚  nkzsqppm benjamin@dev.ofcr.se 23 minutes ago 0a949714
โ”œโ”€โ•ฏ  rebase: extract out some functions from `rebase_revision`
โ—‰  oqtzskyx martinvonz@google.com 1 day ago main* v0.16.0 2dcdc7fb
โ”‚  release: release version 0.16.0
~

This persists the change in the working directory, whilst extracting it to a standalone commit on top of the main branch which can be sent for code review. Hereโ€™s how you can create a branch and push to an upstream repository (GitHub in this case):

โฏ jj branch create test -r rwq
 
โฏ jj git push --remote bnjmnt4n --branch test
Branch changes to push to bnjmnt4n:
  Add branch test to 402f7ad8b9bd
remote:
remote: Create a pull request for 'test' on GitHub by visiting:
remote:      https://github.com/bnjmnt4n/jj/pull/new/test
remote:

Moving a change to a parent

Another possible scenario is that youโ€™ve made some modifications to your working copy, and want to shift the commit into one of the arms of the merge commit.

This is what the commit graph looks like after making the change:

โฏ nvim
 
โฏ jj commit -m "test change"
Working copy now at: uyllouwm e9febf2c (empty) (no description set)
Parent commit      : ovypxnus e99d7578 misc: test change
 
โฏ jj log
@  uyllouwm benjamin@dev.ofcr.se 13 seconds ago e9febf2c
โ”‚  (empty) (no description set)
โ—‰  ovypxnus benjamin@dev.ofcr.se 13 seconds ago HEAD@git e99d7578
โ”‚  misc: test change
โ—‰        orllnptq benjamin@dev.ofcr.se 1 hour ago 7b028dc9
โ”œโ”€โ”ฌโ”€โ”ฌโ”€โ•ฎ  (empty) (no description set)
โ”‚ โ”‚ โ”‚ โ—‰  rwqywnzl benjamin@dev.ofcr.se 1 hour ago test 402f7ad8
โ”‚ โ”‚ โ”‚ โ”‚  new: avoid manual `unwrap()` call
โ”‚ โ”‚ โ—‰ โ”‚  qklyrnvv benjamin@dev.ofcr.se 1 hour ago push-qklyrnvvuksv* 28af9083
โ”‚ โ”‚ โ”œโ”€โ•ฏ  cli: print conflicted paths whenever the working copy is changed
โ”‚ โ—‰ โ”‚  zozvwmow benjamin@dev.ofcr.se 1 hour ago ssh-openssh* c6c73906
โ”‚ โ”‚ โ”‚  git: update error message for SSH error to stop referencing libssh2
โ”‚ โ—‰ โ”‚  yowkkkqn benjamin@dev.ofcr.se 1 hour ago ffec92c9
โ”‚ โ”œโ”€โ•ฏ  git: use prerelease version of `git2` with OpenSSH support
โ—‰ โ”‚  wtmqulxn benjamin@dev.ofcr.se 1 hour ago push-uqxvnturzsuu* 8673733e
โ”‚ โ”‚  rebase: allow both `--insert-after` and `--insert-before` to be used simultaneously
โ—‰ โ”‚  uqxvntur benjamin@dev.ofcr.se 1 hour ago dd7454a2
โ”‚ โ”‚  rebase: add `--insert-after` and `--insert-before` options
โ—‰ โ”‚  nkzsqppm benjamin@dev.ofcr.se 1 hour ago 0a949714
โ”œโ”€โ•ฏ  rebase: extract out some functions from `rebase_revision`
โ—‰  oqtzskyx martinvonz@google.com 1 day ago main* v0.16.0 2dcdc7fb
โ”‚  release: release version 0.16.0
~

Thereโ€™s a new change ovy which we want to set as the child of our previous change rwq, then update the branch test to point to ovy. Thereโ€™s two possible ways to do this right now using Jujutsu:

  1. Rebase ovy onto rwq, rebase the merge commit to point to ovy instead of rwq, then update the branch test to point to ovy.
  2. Create a new commit after rwq, squash the changes from ovy into it, then update the branch test to point to ovy.

The first way is similar to whatโ€™s already been done above, so Iโ€™ll show the second way of doing this. First, we insert a new commit after rwq, making sure to specify --no-edit to avoid checking out the changes in rwq:

โฏ jj new --after rwq --no-edit
Created new commit lqksrtkk 6a38dd7a (empty) (no description set)
Rebased 3 descendant commits
Working copy now at: uyllouwm 355ea4ba (empty) (no description set)
Parent commit      : ovypxnus 27baf0ef misc: test change

A new, empty commit with change ID lqks was created after rwq. Note how lqks was correctly inserted between orl and rwq, maintaining the ancestry of the merge commit:

โฏ jj log
@  uyllouwm benjamin@dev.ofcr.se 1 minute ago 355ea4ba
โ”‚  (empty) (no description set)
โ—‰  ovypxnus benjamin@dev.ofcr.se 1 minute ago HEAD@git 27baf0ef
โ”‚  misc: test change
โ—‰        orllnptq benjamin@dev.ofcr.se 1 minute ago 8c486dfd
โ”œโ”€โ”ฌโ”€โ”ฌโ”€โ•ฎ  (empty) (no description set)
โ”‚ โ”‚ โ”‚ โ—‰  lqksrtkk benjamin@dev.ofcr.se 1 minute ago 6a38dd7a
โ”‚ โ”‚ โ”‚ โ”‚  (empty) (no description set)
โ”‚ โ”‚ โ”‚ โ—‰  rwqywnzl benjamin@dev.ofcr.se 1 hour ago test 402f7ad8
โ”‚ โ”‚ โ”‚ โ”‚  new: avoid manual `unwrap()` call
โ”‚ โ”‚ โ—‰ โ”‚  qklyrnvv benjamin@dev.ofcr.se 1 hour ago push-qklyrnvvuksv* 28af9083
โ”‚ โ”‚ โ”œโ”€โ•ฏ  cli: print conflicted paths whenever the working copy is changed
โ”‚ โ—‰ โ”‚  zozvwmow benjamin@dev.ofcr.se 1 hour ago ssh-openssh* c6c73906
โ”‚ โ”‚ โ”‚  git: update error message for SSH error to stop referencing libssh2
โ”‚ โ—‰ โ”‚  yowkkkqn benjamin@dev.ofcr.se 1 hour ago ffec92c9
โ”‚ โ”œโ”€โ•ฏ  git: use prerelease version of `git2` with OpenSSH support
โ—‰ โ”‚  wtmqulxn benjamin@dev.ofcr.se 1 hour ago push-uqxvnturzsuu* 8673733e
โ”‚ โ”‚  rebase: allow both `--insert-after` and `--insert-before` to be used simultaneously
โ—‰ โ”‚  uqxvntur benjamin@dev.ofcr.se 1 hour ago dd7454a2
โ”‚ โ”‚  rebase: add `--insert-after` and `--insert-before` options
โ—‰ โ”‚  nkzsqppm benjamin@dev.ofcr.se 1 hour ago 0a949714
โ”œโ”€โ•ฏ  rebase: extract out some functions from `rebase_revision`
โ—‰  oqtzskyx martinvonz@google.com 1 day ago main* v0.16.0 2dcdc7fb
โ”‚  release: release version 0.16.0
~

Next, we can โ€œsquashโ€ or move the changes from ovy into lqks. This is followed by updating the branch test to point to lqks:

โฏ jj squash --from ovy --into lqks
Rebased 2 descendant commits
Working copy now at: uyllouwm 23f02b9f (empty) (no description set)
Parent commit      : orllnptq ec83f9fc (empty) (no description set)
 
โฏ jj branch set test -r lqks
 
โฏ jj log
@  uyllouwm benjamin@dev.ofcr.se 1 minute ago 23f02b9f
โ”‚  (empty) (no description set)
โ—‰        orllnptq benjamin@dev.ofcr.se 1 minute ago HEAD@git ec83f9fc
โ”œโ”€โ”ฌโ”€โ”ฌโ”€โ•ฎ  (empty) (no description set)
โ”‚ โ”‚ โ”‚ โ—‰  lqksrtkk benjamin@dev.ofcr.se 1 minute ago test* 07d8a576
โ”‚ โ”‚ โ”‚ โ”‚  misc: test change
โ”‚ โ”‚ โ”‚ โ—‰  rwqywnzl benjamin@dev.ofcr.se 1 hour ago test@bnjmnt4n 402f7ad8
โ”‚ โ”‚ โ”‚ โ”‚  new: avoid manual `unwrap()` call
โ”‚ โ”‚ โ—‰ โ”‚  qklyrnvv benjamin@dev.ofcr.se 2 hours ago push-qklyrnvvuksv* 28af9083
โ”‚ โ”‚ โ”œโ”€โ•ฏ  cli: print conflicted paths whenever the working copy is changed
โ”‚ โ—‰ โ”‚  zozvwmow benjamin@dev.ofcr.se 2 hours ago ssh-openssh* c6c73906
โ”‚ โ”‚ โ”‚  git: update error message for SSH error to stop referencing libssh2
โ”‚ โ—‰ โ”‚  yowkkkqn benjamin@dev.ofcr.se 2 hours ago ffec92c9
โ”‚ โ”œโ”€โ•ฏ  git: use prerelease version of `git2` with OpenSSH support
โ—‰ โ”‚  wtmqulxn benjamin@dev.ofcr.se 2 hours ago push-uqxvnturzsuu* 8673733e
โ”‚ โ”‚  rebase: allow both `--insert-after` and `--insert-before` to be used simultaneously
โ—‰ โ”‚  uqxvntur benjamin@dev.ofcr.se 2 hours ago dd7454a2
โ”‚ โ”‚  rebase: add `--insert-after` and `--insert-before` options
โ—‰ โ”‚  nkzsqppm benjamin@dev.ofcr.se 2 hours ago 0a949714
โ”œโ”€โ•ฏ  rebase: extract out some functions from `rebase_revision`
โ—‰  oqtzskyx martinvonz@google.com 1 day ago main* v0.16.0 2dcdc7fb
โ”‚  release: release version 0.16.0
~

The log now shows that test@bnjmnt4n (the branch test on the remote bnjmnt4n) points to the previous commit, whilst test is pointing to the commit with change ID orl. The * indicator shows that the branch has been updated, but isnโ€™t consistent with the remote.

The biggest downside of the jj squash workflow is that the change ID of the squashed commit is lost. Youโ€™ll need to refer to the change ID of the newly created commit instead.

However, there are plans to improve Jujutsu to make it easier to move commits around the commit graph. In the future, a command like jj rebase -r ovy --after rwq might be able to move the commit whilst maintaining its chnage ID.

Removing parents

Again, we can use jj rebase (and a small change to the revset) to remove parents from a merge commit:

โฏ jj rebase -s orl -d "all:orl- ~ qkl"
Rebased 2 commits
Working copy now at: uyllouwm 521e9749 (empty) (no description set)
Parent commit      : orllnptq 090ffb0d (empty) (no description set)
Added 0 files, modified 9 files, removed 0 files

Previously, when adding new parents, weโ€™ve specified the destinations using the flags -d "all:orl-" -d NEW_PARENT_ID. Now, weโ€™re specifying the destinations using -d "all:orl- ~ qkl". The new argument for the destination highlights more of the revset language, in particular the set difference operator. As before, orl- evaluates to the set of all parents of orl, but ~ qkl now subtracts qkl from that set.

This has the effect of removing qkl from the merge commit:

โฏ jj log
@  uyllouwm benjamin@dev.ofcr.se 14 seconds ago 521e9749
โ”‚  (empty) (no description set)
โ—‰      orllnptq benjamin@dev.ofcr.se 14 seconds ago HEAD@git 090ffb0d
โ”œโ”€โ”ฌโ”€โ•ฎ  (empty) (no description set)
โ”‚ โ”‚ โ—‰  zozvwmow benjamin@dev.ofcr.se 2 hours ago ssh-openssh* c6c73906
โ”‚ โ”‚ โ”‚  git: update error message for SSH error to stop referencing libssh2
โ”‚ โ”‚ โ—‰  yowkkkqn benjamin@dev.ofcr.se 2 hours ago ffec92c9
โ”‚ โ”‚ โ”‚  git: use prerelease version of `git2` with OpenSSH support
โ”‚ โ—‰ โ”‚  wtmqulxn benjamin@dev.ofcr.se 2 hours ago push-uqxvnturzsuu* 8673733e
โ”‚ โ”‚ โ”‚  rebase: allow both `--insert-after` and `--insert-before` to be used simultaneously
โ”‚ โ—‰ โ”‚  uqxvntur benjamin@dev.ofcr.se 2 hours ago dd7454a2
โ”‚ โ”‚ โ”‚  rebase: add `--insert-after` and `--insert-before` options
โ”‚ โ—‰ โ”‚  nkzsqppm benjamin@dev.ofcr.se 2 hours ago 0a949714
โ”‚ โ”œโ”€โ•ฏ  rebase: extract out some functions from `rebase_revision`
โ—‰ โ”‚  lqksrtkk benjamin@dev.ofcr.se 6 minutes ago test* 07d8a576
โ”‚ โ”‚  misc: test change
โ—‰ โ”‚  rwqywnzl benjamin@dev.ofcr.se 1 hour ago test@bnjmnt4n 402f7ad8
โ”œโ”€โ•ฏ  new: avoid manual `unwrap()` call
โ—‰  oqtzskyx martinvonz@google.com 1 day ago main* v0.16.0 2dcdc7fb
โ”‚  release: release version 0.16.0
~

(Likewise, we could also have used set operations to add new parents to the merge commit: jj rebase -d "all:orl- | NEW_PARENT_ID" uses the set union operator to add the new ID to the set of existing parents.)

Conflicting changes

What happens if you update something in the working directory, which you want to shift to a specific parent of the merge commit, but it actually conflicts with another change you made in another parent?

Here, Iโ€™ve committed a change which modifies the same lines as the previous commit rwq:

โฏ jj commit -m "conflicting change"
Working copy now at: ywryozyt 7fd247f5 (empty) (no description set)
Parent commit      : uyllouwm 128d5444 conflicting change
 
โฏ jj show uyl
Commit ID: e8cc1f87020ecfabc4fa4b44a6a8a8d67a5de23c
Change ID: uyllouwmkkkkrkvtzynuqwuqvxsrmpvx
Author: Benjamin Tan <benjamin@dev.ofcr.se> (21 hours ago)
Committer: Benjamin Tan <benjamin@dev.ofcr.se> (20 minutes ago)
 
    (no description set)
 
diff --git a/cli/src/commands/new.rs b/cli/src/commands/new.rs
index e0defba129...b946b93769 100644
--- a/cli/src/commands/new.rs
+++ b/cli/src/commands/new.rs
@@ -193,7 +193,8 @@
             writeln!(formatter)?;
         }
     } else {
-        tx.edit(&new_commit)?;
+        let commit = new_commit;
+        tx.edit(&commit).unwrap();
         // The description of the new commit will be printed by tx.finish()
     }
     if num_rebased > 0 {

Hereโ€™s the updated commit graph now, with uyl containing the change and no longer being empty:

โฏ jj log
@  ywryozyt benjamin@dev.ofcr.se 35 seconds ago 7fd247f5
โ”‚  (empty) (no description set)
โ—‰  uyllouwm benjamin@dev.ofcr.se 35 seconds ago HEAD@git 128d5444
โ”‚  conflicting change
โ—‰      orllnptq benjamin@dev.ofcr.se 21 hours ago 090ffb0d
โ”œโ”€โ”ฌโ”€โ•ฎ  (empty) (no description set)
โ”‚ โ”‚ โ—‰  zozvwmow benjamin@dev.ofcr.se 23 hours ago ssh-openssh* c6c73906
โ”‚ โ”‚ โ”‚  git: update error message for SSH error to stop referencing libssh2
โ”‚ โ”‚ โ—‰  yowkkkqn benjamin@dev.ofcr.se 23 hours ago ffec92c9
โ”‚ โ”‚ โ”‚  git: use prerelease version of `git2` with OpenSSH support
โ”‚ โ—‰ โ”‚  wtmqulxn benjamin@dev.ofcr.se 23 hours ago push-uqxvnturzsuu* 8673733e
โ”‚ โ”‚ โ”‚  rebase: allow both `--insert-after` and `--insert-before` to be used simultaneously
โ”‚ โ—‰ โ”‚  uqxvntur benjamin@dev.ofcr.se 23 hours ago dd7454a2
โ”‚ โ”‚ โ”‚  rebase: add `--insert-after` and `--insert-before` options
โ”‚ โ—‰ โ”‚  nkzsqppm benjamin@dev.ofcr.se 23 hours ago 0a949714
โ”‚ โ”œโ”€โ•ฏ  rebase: extract out some functions from `rebase_revision`
โ—‰ โ”‚  lqksrtkk benjamin@dev.ofcr.se 21 hours ago test* 07d8a576
โ”‚ โ”‚  misc: test change
โ—‰ โ”‚  rwqywnzl benjamin@dev.ofcr.se 23 hours ago test@bnjmnt4n 402f7ad8
โ”œโ”€โ•ฏ  new: avoid manual `unwrap()` call
โ—‰  oqtzskyx martinvonz@google.com 2 days ago main* v0.16.0 2dcdc7fb
โ”‚  release: release version 0.16.0
~

I now want to shift this new commit into the arm of the merge commit with zoz (the ssh-openssh branch), so I create a new, empty commit after zoz:

โฏ jj new --after zoz --no-edit
Created new commit txsrozwq ae4fdff5 (empty) (no description set)
Rebased 5 descendant commits
Working copy now at: ywryozyt e1fa0851 (empty) (no description set)
Parent commit      : uyllouwm 454caf02 conflicting change
 
โฏ jj log
@  ywryozyt benjamin@dev.ofcr.se 5 seconds ago e1fa0851
โ”‚  (empty) (no description set)
โ—‰  uyllouwm benjamin@dev.ofcr.se 6 seconds ago HEAD@git 454caf02
โ”‚  conflicting change
โ—‰      orllnptq benjamin@dev.ofcr.se 6 seconds ago fabcecf1
โ”œโ”€โ”ฌโ”€โ•ฎ  (empty) (no description set)
โ”‚ โ”‚ โ—‰  txsrozwq benjamin@dev.ofcr.se 6 seconds ago ae4fdff5
โ”‚ โ”‚ โ”‚  (empty) (no description set)
โ”‚ โ”‚ โ—‰  zozvwmow benjamin@dev.ofcr.se 23 hours ago ssh-openssh* c6c73906
โ”‚ โ”‚ โ”‚  git: update error message for SSH error to stop referencing libssh2
โ”‚ โ”‚ โ—‰  yowkkkqn benjamin@dev.ofcr.se 23 hours ago ffec92c9
โ”‚ โ”‚ โ”‚  git: use prerelease version of `git2` with OpenSSH support
โ”‚ โ—‰ โ”‚  wtmqulxn benjamin@dev.ofcr.se 23 hours ago push-uqxvnturzsuu* 8673733e
โ”‚ โ”‚ โ”‚  rebase: allow both `--insert-after` and `--insert-before` to be used simultaneously
โ”‚ โ—‰ โ”‚  uqxvntur benjamin@dev.ofcr.se 23 hours ago dd7454a2
โ”‚ โ”‚ โ”‚  rebase: add `--insert-after` and `--insert-before` options
โ”‚ โ—‰ โ”‚  nkzsqppm benjamin@dev.ofcr.se 23 hours ago 0a949714
โ”‚ โ”œโ”€โ•ฏ  rebase: extract out some functions from `rebase_revision`
โ—‰ โ”‚  lqksrtkk benjamin@dev.ofcr.se 21 hours ago test* 07d8a576
โ”‚ โ”‚  misc: test change
โ—‰ โ”‚  rwqywnzl benjamin@dev.ofcr.se 23 hours ago test@bnjmnt4n 402f7ad8
โ”œโ”€โ•ฏ  new: avoid manual `unwrap()` call
โ—‰  oqtzskyx martinvonz@google.com 2 days ago main* v0.16.0 2dcdc7fb
โ”‚  release: release version 0.16.0
~

The new commit has the change ID txs, so Iโ€™ll squash my changes from uyl into txs:

โฏ jj squash --from uyl --into txs
Rebased 4 descendant commits
New conflicts appeared in these commits:
  txsrozwq 0bbdad29 (conflict) conflicting change
To resolve the conflicts, start by updating to it:
  jj new txsrozwqlunv
Then use `jj resolve`, or edit the conflict markers in the file directly.
Once the conflicts are resolved, you may want inspect the result with `jj diff`.
Then run `jj squash` to move the resolution into the conflicted commit.
Working copy now at: ywryozyt 631fda4b (empty) (no description set)
Parent commit      : orllnptq 06531057 (empty) (no description set)

Jujutsu now warns that a new conflict appeared in txs, as expected. Thatโ€™s because txs doesnโ€™t have rwq in its history, which was where the first modification came from. Letโ€™s take a look at the log now:

โฏ jj log
@  ywryozyt benjamin@dev.ofcr.se 5 seconds ago 631fda4b
โ”‚  (empty) (no description set)
โ—‰      orllnptq benjamin@dev.ofcr.se 5 seconds ago HEAD@git 06531057
โ”œโ”€โ”ฌโ”€โ•ฎ  (empty) (no description set)
โ”‚ โ”‚ โ—‰  txsrozwq benjamin@dev.ofcr.se 5 seconds ago 0bbdad29 conflict
โ”‚ โ”‚ โ”‚  conflicting change
โ”‚ โ”‚ โ—‰  zozvwmow benjamin@dev.ofcr.se 23 hours ago ssh-openssh* c6c73906
โ”‚ โ”‚ โ”‚  git: update error message for SSH error to stop referencing libssh2
โ”‚ โ”‚ โ—‰  yowkkkqn benjamin@dev.ofcr.se 23 hours ago ffec92c9
โ”‚ โ”‚ โ”‚  git: use prerelease version of `git2` with OpenSSH support
โ”‚ โ—‰ โ”‚  wtmqulxn benjamin@dev.ofcr.se 23 hours ago push-uqxvnturzsuu* 8673733e
โ”‚ โ”‚ โ”‚  rebase: allow both `--insert-after` and `--insert-before` to be used simultaneously
โ”‚ โ—‰ โ”‚  uqxvntur benjamin@dev.ofcr.se 23 hours ago dd7454a2
โ”‚ โ”‚ โ”‚  rebase: add `--insert-after` and `--insert-before` options
โ”‚ โ—‰ โ”‚  nkzsqppm benjamin@dev.ofcr.se 23 hours ago 0a949714
โ”‚ โ”œโ”€โ•ฏ  rebase: extract out some functions from `rebase_revision`
โ—‰ โ”‚  lqksrtkk benjamin@dev.ofcr.se 21 hours ago test* 07d8a576
โ”‚ โ”‚  misc: test change
โ—‰ โ”‚  rwqywnzl benjamin@dev.ofcr.se 23 hours ago test@bnjmnt4n 402f7ad8
โ”œโ”€โ•ฏ  new: avoid manual `unwrap()` call
โ—‰  oqtzskyx martinvonz@google.com 2 days ago main* v0.16.0 2dcdc7fb
โ”‚  release: release version 0.16.0
~

The commit txs is marked in the log as containing a conflict. Hereโ€™s what txs looks like:

โฏ jj show txs
Commit ID: 0bbdad290b695b94aab4e973349e1a5dda6ef0ce
Change ID: txsrozwqlunvppzymmtrnvotvtrnuwxr
Author: Benjamin Tan <benjamin@dev.ofcr.se> (14 minutes ago)
Committer: Benjamin Tan <benjamin@dev.ofcr.se> (13 minutes ago)
 
    conflicting change
 
diff --git a/cli/src/commands/new.rs b/cli/src/commands/new.rs
index eeeb50aee6...0000000000 100644
--- a/cli/src/commands/new.rs
+++ b/cli/src/commands/new.rs
@@ -193,7 +193,14 @@
             writeln!(formatter)?;
         }
     } else {
-        tx.edit(&new_commit).unwrap();
+<<<<<<<
+%%%%%%%
+-        tx.edit(&new_commit)?;
++        tx.edit(&new_commit).unwrap();
++++++++
+        let commit = new_commit;
+        tx.edit(&commit).unwrap();
+>>>>>>>
         // The description of the new commit will be printed by tx.finish()
     }
     if num_rebased > 0 {

The original line from txsโ€™s parent commit in red is replaced with the new conflicting changes in green. Jujutsuโ€™s conflict markers are slightly different from Git: lines following %%%%%%% are a diff between 2 sides, whilst lines following +++++++ are a snapshot of the changes a side. Hereโ€™s my annotations on what the conflict markers mean:

<<<<<<<
%%%%%%% Diff from destination `txs` to base tree of `orl`
-        tx.edit(&new_commit)?;
+        tx.edit(&new_commit).unwrap();
+++++++ Snapshot of new changes from source `uyl`
        let commit = new_commit;
        tx.edit(&commit).unwrap();
>>>>>>>

Even though txs has a conflict, note that the merge commit orl isnโ€™t in a conflicted state. This is because Jujutsu doesnโ€™t just store conflict markers, but the full metadata of the conflicts, so it can resolve the conflicts by applying all the changes from each of orlโ€™s parents.

However, if we want to update the ssh-openssh branch to include the changes in txs, we canโ€™t just push a conflicted file since it wonโ€™t be accepted in any code review. We need to first resolve the conflict in txs. Iโ€™m doing this manually here by checking out txs and editing the file in the working directory, but you can also use a graphical tool for conflict resolution.

โฏ jj new txs
Working copy now at: yswompum 3ea0e8e9 (conflict) (empty) (no description set)
Parent commit      : txsrozwq 0bbdad29 (conflict) conflicting change
Added 0 files, modified 5 files, removed 0 files
 
โฏ nvim
 
โฏ jj status
Working copy changes:
M cli/src/commands/new.rs
Working copy : yswompum 509f7ea1 (no description set)
Parent commit: txsrozwq 0bbdad29 (conflict) conflicting change

The changes are updated in my working copy commit, so I can squash the changes into txs to apply the resolution there as well:

โฏ jj squash
Rebased 3 descendant commits
Existing conflicts were resolved or abandoned from these commits:
  txsrozwq hidden 0bbdad29 (conflict) conflicting change
Working copy now at: xmynmysw 53a37139 (empty) (no description set)
Parent commit      : txsrozwq a11303a3 conflicting change

So, txs no longer has any conflicts, and we can update our branch to point to it and push it for review. However, if we go back to our merge commit orl, we can see that the merge commit is now marked as conflicting:

โฏ jj new orl
Working copy now at: nlwnwups 69a1f4ce (conflict) (empty) (no description set)
Parent commit      : orllnptq 9192cf35 (conflict) (empty) (no description set)
Added 0 files, modified 5 files, removed 0 files
 
โฏ jj log
@  nlwnwups benjamin@dev.ofcr.se 1 minute ago 69a1f4ce conflict
โ”‚  (empty) (no description set)
โ—‰      orllnptq benjamin@dev.ofcr.se 5 minutes ago HEAD@git 9192cf35 conflict
โ”œโ”€โ”ฌโ”€โ•ฎ  (empty) (no description set)
โ”‚ โ”‚ โ—‰  txsrozwq benjamin@dev.ofcr.se 5 minutes ago a11303a3
โ”‚ โ”‚ โ”‚  conflicting change
โ”‚ โ”‚ โ—‰  zozvwmow benjamin@dev.ofcr.se 1 day ago ssh-openssh* c6c73906
โ”‚ โ”‚ โ”‚  git: update error message for SSH error to stop referencing libssh2
โ”‚ โ”‚ โ—‰  yowkkkqn benjamin@dev.ofcr.se 1 day ago ffec92c9
โ”‚ โ”‚ โ”‚  git: use prerelease version of `git2` with OpenSSH support
โ”‚ โ—‰ โ”‚  wtmqulxn benjamin@dev.ofcr.se 1 day ago push-uqxvnturzsuu* 8673733e
โ”‚ โ”‚ โ”‚  rebase: allow both `--insert-after` and `--insert-before` to be used simultaneously
โ”‚ โ—‰ โ”‚  uqxvntur benjamin@dev.ofcr.se 1 day ago dd7454a2
โ”‚ โ”‚ โ”‚  rebase: add `--insert-after` and `--insert-before` options
โ”‚ โ—‰ โ”‚  nkzsqppm benjamin@dev.ofcr.se 1 day ago 0a949714
โ”‚ โ”œโ”€โ•ฏ  rebase: extract out some functions from `rebase_revision`
โ—‰ โ”‚  lqksrtkk benjamin@dev.ofcr.se 23 hours ago test* 07d8a576
โ”‚ โ”‚  misc: test change
โ—‰ โ”‚  rwqywnzl benjamin@dev.ofcr.se 1 day ago test@bnjmnt4n 402f7ad8
โ”œโ”€โ•ฏ  new: avoid manual `unwrap()` call
โ—‰  oqtzskyx martinvonz@google.com 2 days ago main* v0.16.0 2dcdc7fb
โ”‚  release: release version 0.16.0
~

This makes sense, because weโ€™ve removed manually resolved the conflicts from txs. Jujutsu no longer has the metadata about how the conflict came about from merging different files, so orl now has a conflict. Typically, this isnโ€™t that big an issue since you can just delay conflict resolution for that individual commit until youโ€™re done working on that branch. You can then remove that branch from the merge commit after that.

If you do want to continue working on both branches, you can also restore the state of the working directory to its original state, before squashing the commit. First, get the commit ID of the change uyl that we wrote originally, before any commit manipulation (128d5444 from above), then run jj restore:

โฏ jj restore --from 128d5444 --to orl
Created orllnptq f2388131 (no description set)
Rebased 1 descendant commits
Working copy now at: nlwnwups d37fb681 (empty) (no description set)
Parent commit      : orllnptq f2388131 (no description set)
Added 0 files, modified 1 files, removed 0 files

This restores all the files in commit orl to their state in commit 128d5444โ€”the original files before the conflict occured due to squashing of commits. This has the effect of solving the conflict within the merge commit orl:

โฏ jj log
@  nlwnwups benjamin@dev.ofcr.se 39 seconds ago d37fb681
โ”‚  (empty) (no description set)
โ—‰      orllnptq benjamin@dev.ofcr.se 39 seconds ago HEAD@git f2388131
โ”œโ”€โ”ฌโ”€โ•ฎ  (no description set)
โ”‚ โ”‚ โ—‰  txsrozwq benjamin@dev.ofcr.se 3 days ago a11303a3
โ”‚ โ”‚ โ”‚  conflicting change
โ”‚ โ”‚ โ—‰  zozvwmow benjamin@dev.ofcr.se 4 days ago ssh-openssh* c6c73906
โ”‚ โ”‚ โ”‚  git: update error message for SSH error to stop referencing libssh2
โ”‚ โ”‚ โ—‰  yowkkkqn benjamin@dev.ofcr.se 4 days ago ffec92c9
โ”‚ โ”‚ โ”‚  git: use prerelease version of `git2` with OpenSSH support
โ”‚ โ—‰ โ”‚  wtmqulxn benjamin@dev.ofcr.se 4 days ago push-uqxvnturzsuu* 8673733e
โ”‚ โ”‚ โ”‚  rebase: allow both `--insert-after` and `--insert-before` to be used simultaneously
โ”‚ โ—‰ โ”‚  uqxvntur benjamin@dev.ofcr.se 4 days ago dd7454a2
โ”‚ โ”‚ โ”‚  rebase: add `--insert-after` and `--insert-before` options
โ”‚ โ—‰ โ”‚  nkzsqppm benjamin@dev.ofcr.se 4 days ago 0a949714
โ”‚ โ”œโ”€โ•ฏ  rebase: extract out some functions from `rebase_revision`
โ—‰ โ”‚  lqksrtkk benjamin@dev.ofcr.se 4 days ago test* 07d8a576
โ”‚ โ”‚  misc: test change
โ—‰ โ”‚  rwqywnzl benjamin@dev.ofcr.se 4 days ago test@bnjmnt4n 402f7ad8
โ”œโ”€โ•ฏ  new: avoid manual `unwrap()` call
โ—‰  oqtzskyx martinvonz@google.com 5 days ago main* v0.16.0 2dcdc7fb
โ”‚  release: release version 0.16.0
~
 
โฏ jj show orl
Commit ID: f2388131cba4be8fe0b267dcef1af8d823184851
Change ID: orllnptqzkuqpsonzzkytxlrzpyxmwtn
Author: Benjamin Tan <benjamin@dev.ofcr.se> (4 days ago)
Committer: Benjamin Tan <benjamin@dev.ofcr.se> (1 minute ago)
 
    (no description set)
 
diff --git a/cli/src/commands/new.rs b/cli/src/commands/new.rs
index 0000000000...b946b93769 100644
--- a/cli/src/commands/new.rs
+++ b/cli/src/commands/new.rs
@@ -193,14 +193,8 @@
             writeln!(formatter)?;
         }
     } else {
-<<<<<<<
-%%%%%%%
--        tx.edit(&new_commit).unwrap();
-+        tx.edit(&new_commit)?;
-+++++++
         let commit = new_commit;
         tx.edit(&commit).unwrap();
->>>>>>>
         // The description of the new commit will be printed by tx.finish()
     }
     if num_rebased > 0 {

Conclusion

Iโ€™ve shown how you can use Jujutsu to manipulate merge commits and work on separate logical branches of code, by adding and removing parents using the jj rebase command. Arguably, this workflow might be better visualized with a graphical interface to see how the commit graph is being manipulated. Thereโ€™s a GUI app for Jujutsu, called GG, with a pretty convincing demo of this workflow.

Working on multiple branches of code at the same time can be really powerful. Personally, I use this workflow all the time to avoid having to switch branches. This is especially convenient when working on small bugfixes where itโ€™s definitely easier to just work in the current directory.

Merging multiple branches together also allows you to very simply test out various features at the same time, without having to wait for all of them to be merged into the main branch. In fact, I use this to build a custom jj binary which contains various features which havenโ€™t been merged into main.

Even if you arenโ€™t convinced about switching to Jujutsu, I think this workflow is still valuable. In fact, GitButlerโ€”a new Git clientโ€”was recently launched with a similar end product: making it easy to activate different โ€œvirtual branchesโ€ in your working directory. Otherwise, alternative tools like git-branchless might allow you to do something similar.

If you are intrigued by Jujutsu, do check out the introduction and tutorial. Iโ€™d also recommend Chrisโ€™s article and video series. Steve Klabnik also has a long-form tutorial on Jujutsu, which includes a chapter on this workflow.

Footnotes

  1. Hmmm, maybe The Austinโ„ข Mega Merge Strategyยฎ is the better name after allโ€ฆ โ†ฉ

  2. Actually, you can have change IDs associated with multiple commits, but thatโ€™s out of the scope of this article. โ†ฉ

  3. jj commit is a shorthand for jj describe, to describe the current change, and jj new, to create a new change. โ†ฉ