| 1 |
# Handling divergent changes |
| 2 |
|
| 3 |
## What are divergent changes? |
| 4 |
|
| 5 |
A [divergent change] occurs when multiple [visible commits] have the same change |
| 6 |
ID. These changes are displayed with a [change offset] after their change ID and |
| 7 |
a label of "divergent": |
| 8 |
|
| 9 |
```shell |
| 10 |
$ jj log |
| 11 |
@ mzvwutvl/0 [email protected] 2001-02-03 08:05:12 29d07a2d (divergent) |
| 12 |
│ a divergent change |
| 13 |
``` |
| 14 |
|
| 15 |
Normally, when commits are rewritten, the original version (the "predecessor") |
| 16 |
becomes hidden and the new commit (the "successor") is visible. Thus, only one |
| 17 |
commit with a given change ID is visible at a time. |
| 18 |
|
| 19 |
But, a hidden commit can become visible again. This can happen if: |
| 20 |
|
| 21 |
- A visible descendant is added locally. For example, `jj new REV` will make |
| 22 |
`REV` visible even if it was hidden before. |
| 23 |
|
| 24 |
- A visible descendant is fetched from a remote. If the hidden commit was pushed |
| 25 |
to a remote, others may base new commits off of them. When their new commits are |
| 26 |
fetched, their visibility makes the hidden commit visible again. |
| 27 |
|
| 28 |
- It is made the working copy. `jj edit REV` will make `REV` and all its |
| 29 |
ancestors visible if it wasn't already. |
| 30 |
|
| 31 |
- Some other operations make hidden commits visible. For example, adding a |
| 32 |
bookmark to a hidden commit makes it visible with the assumption that you are |
| 33 |
now working with that commit again. |
| 34 |
|
| 35 |
Divergent changes also occur if two different users or processes amend the same |
| 36 |
change, creating two visible successors. This can happen when: |
| 37 |
|
| 38 |
- Another author modifies commits in a branch that you have also modified |
| 39 |
locally. |
| 40 |
|
| 41 |
- You perform operations on the same change from different workspaces of the |
| 42 |
same repository. |
| 43 |
|
| 44 |
- Two programs modify the repository at the same time. For example, you run |
| 45 |
`jj describe` and, while writing your commit description, an IDE integration |
| 46 |
fetches and rebases the branch you're working on. |
| 47 |
|
| 48 |
[divergent change]: ../glossary.md#divergent-change |
| 49 |
[visible commits]: ../glossary.md#visible-commits |
| 50 |
[change offset]: ../glossary.md#change-offset |
| 51 |
|
| 52 |
## How do I resolve divergent changes? |
| 53 |
|
| 54 |
When you encounter divergent changes, you have several strategies to choose |
| 55 |
from. The best approach depends on whether you want to keep the content from one |
| 56 |
commit, both commits, or merge them together. |
| 57 |
|
| 58 |
Note that revsets must refer to the divergent commit either using its commit ID |
| 59 |
or using its change ID with a [change offset] like `/0` or `/1` as shown in the |
| 60 |
log, since the change ID is ambiguous by itself. |
| 61 |
|
| 62 |
### Strategy 1: Use jj converge |
| 63 |
|
| 64 |
There is an experimental `jj converge` command that tries to resolve divergence |
| 65 |
automatically, and prompts the user for input to help the process otherwise. |
| 66 |
|
| 67 |
!!! note |
| 68 |
|
| 69 |
This command uses heuristics to try to find a good solution, but no |
| 70 |
algorithm will *always* do what the user wants since there is no objective |
| 71 |
way to define what that is. You can inspect what the command did with `jj op |
| 72 |
show -p`. You can run `jj undo` if you are not satisfied with the changes. |
| 73 |
|
| 74 |
### Strategy 2: Abandon one of the commits |
| 75 |
|
| 76 |
If one of the divergent commits is clearly obsolete or incorrect, simply abandon |
| 77 |
it: |
| 78 |
|
| 79 |
```shell |
| 80 |
# Abandon the unwanted commit using its commit ID (or change ID with offset) |
| 81 |
jj abandon <unwanted-commit-id> |
| 82 |
|
| 83 |
# You can abandon several at once with: |
| 84 |
# jj abandon abc def 123 |
| 85 |
# jj abandon abc:: |
| 86 |
``` |
| 87 |
|
| 88 |
This is the simplest solution when you know which version to keep. |
| 89 |
|
| 90 |
### Strategy 3: Generate a new change ID |
| 91 |
|
| 92 |
If you want to keep both versions as separate changes with different change IDs, |
| 93 |
you can generate a new change ID for one of the commits: |
| 94 |
|
| 95 |
```shell |
| 96 |
jj metaedit --update-change-id <commit-id> |
| 97 |
``` |
| 98 |
|
| 99 |
This preserves both versions of the content while resolving the divergence. |
| 100 |
|
| 101 |
### Strategy 4: Squash the commits together |
| 102 |
|
| 103 |
When you want to combine the content from both divergent commits: |
| 104 |
|
| 105 |
```shell |
| 106 |
# Squash one commit into the other |
| 107 |
jj squash --from <source-commit-id> --into <target-commit-id> |
| 108 |
``` |
| 109 |
|
| 110 |
This combines the changes from both commits into a single commit. The source |
| 111 |
commit will be abandoned. |
| 112 |
|
| 113 |
### Strategy 5: Ignore the divergence |
| 114 |
|
| 115 |
Divergence isn't an error. If the divergence doesn't cause immediate problems, |
| 116 |
you can leave it as-is. If both commits are part of immutable history, this may |
| 117 |
be your only option. |
| 118 |
|
| 119 |
However, it can be inconvenient since you cannot refer to divergent changes |
| 120 |
unambiguously using their change ID alone. |