Debian Codegraph @

cli/docs/bookmarks.md

raw
1 # Bookmarks
2
3 ## Introduction
4
5 Bookmarks are named pointers to revisions (just like branches are in Git). You
6 can move them without affecting the target revision's identity. Bookmarks
7 automatically move when revisions are rewritten (e.g. by `jj rebase`). You can
8 pass a bookmark's name to commands that want a revision as argument. For example,
9 `jj new main` will create a new revision on top of the `main` bookmark. Use
10 `jj bookmark list` to list bookmarks and `jj bookmark <subcommand>` to create,
11 move, or delete bookmarks. There is currently no concept of an
12 active/current/checked-out bookmark.
13
14 ## Mapping to Git branches
15
16 Jujutsu maps its bookmarks to Git branches when interacting with Git repos. For
17 example, `jj git push --bookmark foo` will push the state of the `foo` bookmark
18 to the `foo` branch on the Git remote. Similarly, if you create a `bar` branch
19 in the backing Git repo, then a subsequent `jj git import` will create a `bar`
20 bookmark (reminder: that import happens automatically in
21 [colocated workspaces][colocated-workspaces]).
22
23 ## Remotes and tracked bookmarks
24
25 Jujutsu records the last seen position of a bookmark on each remote (just like
26 Git's remote-tracking branches). This record is updated on every `jj git fetch`
27 and `jj git push` of the bookmark. You can refer to the remembered remote bookmark
28 positions with `<bookmark name>@<remote name>`, such as `jj new main@origin`. `jj`
29 does not provide a way to manually edit these recorded positions.
30
31 A remote bookmark can be associated with a local bookmark of the same name. This
32 is called a **tracked remote bookmark** (which maps to a Git remote branch when
33 using the Git backend). When you pull a tracked bookmark from a remote, any
34 changes compared to the current record of the remote's state will be propagated
35 to the corresponding local bookmark, which will be created if it doesn't exist
36 already.
37
38 !!! note "Details: how `fetch` pulls bookmarks"
39
40 Let's say you run `jj git fetch --remote origin` and, during the fetch, `jj`
41 determines that the remote's `main` bookmark has been moved so that its target is
42 now ahead of the local record in `main@origin`.
43
44 `jj` will then update `main@origin` to the new target. If `main@origin` is
45 **tracked**, `jj` will also apply the change to the local bookmark `main`. If the
46 local target has also been moved compared to `main@origin` (probably because you
47 ran `jj bookmark set main`), then the two updates will be merged. If one is ahead
48 of the other, then that target will become the new target. Otherwise, the local
49 bookmark will become conflicted (see the ["Conflicts" section](#conflicts) below
50 for details).
51
52 Most commands don't show the tracked remote bookmark if it has the same target as
53 the local bookmark. The local bookmark (without `@<remote name>`) is considered the
54 bookmark's desired target. Consequently, if you want to update a bookmark on a
55 remote, you first update the bookmark locally and then push the update to the
56 remote. If a local bookmark also exists on some remote but points to a different
57 target there, `jj log` will show the bookmark name with an asterisk suffix (e.g.
58 `main*`). That is meant to remind you that you may want to push the bookmark to
59 some remote.
60
61 If you want to know the internals of bookmark tracking, consult the
62 [Design Doc][design].
63
64 ### Terminology summary
65
66 - A **remote bookmark** is a bookmark ref on the remote. `jj` can find out its
67 actual state only when it's actively communicating with the remote. However,
68 `jj` does store the last-seen position of the remote bookmark; this is the
69 commit `jj show <bookmark name>@<remote name>` would show. This notion is
70 completely analogous to Git's "remote-tracking branches".
71 - A **tracked (remote) bookmark** is defined above. You can make a remote bookmark
72 tracked with the [`jj bookmark track` command](#manually-tracking-a-bookmark), for
73 example.
74 - A **tracking (local) bookmark** is the local bookmark that `jj` tries to keep
75 in sync with the tracked remote bookmark. For example, after `jj bookmark
76 track mybookmark --remote=origin`, there will be a local bookmark `mybookmark`
77 that's tracking the remote `mybookmark@origin` bookmark. A local bookmark can
78 track a bookmark of the same name on 0 or more remotes.
79
80 The notion of tracked bookmarks serves a similar function to the Git notion of an
81 "upstream branch". Unlike Git, a single local bookmark can be tracking remote
82 bookmarks on multiple remotes, and the names of the local and remote bookmarks
83 must match.
84
85 ### Manually tracking a bookmark
86
87 To track a bookmark permanently use `jj bookmark track <bookmark name>
88 --remote=<remote name>`. It will now be imported as a local bookmark until you
89 untrack it or it is deleted on the remote.
90
91 Example:
92
93 ```sh
94 $ # List all available bookmarks, as we want our colleague's bookmark.
95 $ jj bookmark list --all
96 $ # Find the bookmark.
97 $ # [...]
98 $ # Actually track the bookmark.
99 $ jj bookmark track <bookmark name> --remote=<remote name> # Example: jj bookmark track my-feature --remote=origin
100 $ # From this point on, <bookmark name> will be imported when fetching from <remote name>.
101 $ jj git fetch --remote <remote name>
102 $ # A local bookmark <bookmark name> should have been created or updated while fetching.
103 $ jj new <bookmark name> # Do some local testing, etc.
104 ```
105
106 ### Untracking a bookmark
107
108 To stop following a remote bookmark, you can `jj bookmark untrack` it. After that,
109 subsequent fetches of that remote will no longer move the local bookmark to match
110 the position of the remote bookmark.
111
112 Example:
113
114 ```sh
115 $ # List all local and remote bookmarks.
116 $ jj bookmark list --all
117 $ # Find the bookmark we no longer want to track.
118 $ # [...]
119 # # Actually untrack it.
120 $ jj bookmark untrack <bookmark name> --remote=<remote name> # Example: jj bookmark untrack stuff --remote=origin
121 $ # From this point on, this remote bookmark won't be imported anymore.
122 $ # The local bookmark (e.g. stuff) is unaffected. It may or may not still
123 $ # be tracking bookmarks on other remotes (e.g. stuff@upstream).
124 ```
125
126 ### Listing tracked bookmarks
127
128 To list tracked bookmarks, you can `jj bookmark list --tracked` or `jj bookmark list -t`.
129 This command omits local Git-tracking bookmarks by default.
130
131 You can see if a specific bookmark is tracked with `jj bookmark list --tracked <bookmark name>`.
132
133 ### Automatic tracking of bookmarks & `auto-track-bookmarks` option
134
135 There are two situations where `jj` tracks bookmarks automatically. `jj git
136 clone` automatically sets up the default remote bookmark (e.g. `main@origin`) as
137 tracked. When you push a local bookmark, the newly created bookmark on the remote is
138 marked as tracked.
139
140 By default, every other remote bookmark is marked as "not tracked" when it's
141 fetched. If desired, you need to manually `jj bookmark track` them. This works
142 well for repositories where multiple people work on a large number of bookmarks.
143
144 The default can be changed by setting the config `remotes.<name>.auto-track-bookmarks = "*"`.
145 Then, `jj git fetch` tracks every *newly fetched* bookmark with a local bookmark.
146 Branches that already existed before the `jj git fetch` are not affected. This
147 is similar to Mercurial, which fetches all its bookmarks (equivalent to Git's
148 branches) by default. Similarly, all newly created local bookmarks
149 will be marked as "tracked", preparing them to be pushed with
150 the next `jj git push` command. See ["Automatic tracking of
151 bookmarks"](config.md#automatic-tracking-of-bookmarks) for details.
152
153 ## Bookmark updates
154
155 Currently Jujutsu automatically updates local bookmarks when these conditions are
156 met:
157
158 * When a commit has been rewritten (e.g, when you rebase) bookmarks and the
159 working-copy will move along with it.
160 * When a commit has been abandoned, all associated bookmarks will be deleted.
161
162 You could describe the updates as following along the change-id of the
163 current bookmark commit, even if it isn't entirely accurate.
164
165 ## Pushing bookmarks: Safety checks
166
167 Before `jj git push` actually moves, creates, or deletes a remote bookmark, it
168 makes several safety checks.
169
170 1. `jj` will contact the remote and check that the actual state of the remote
171 bookmark matches `jj`'s record of its last known position. If there is a
172 conflict, `jj` will refuse to push the bookmark. In this case, you need to run
173 `jj git fetch --remote <remote name>` and resolve the resulting bookmark
174 conflict. Then, you can try `jj git push` again.
175
176 If you are familiar with Git, this makes `jj git push` similar to `git
177 push --force-with-lease`.
178
179 There are a few cases where `jj git push` will succeed even though the remote
180 bookmark is in an unexpected location. These are the cases where `jj git fetch`
181 would not create a bookmark conflict and would not move the local bookmark, e.g.
182 if the unexpected location is identical to the local position of the bookmark.
183
184 2. The local bookmark must not be [conflicted](#conflicts). If it is, you would
185 need to use `jj bookmark move`, for example, to resolve the conflict.
186
187 This makes `jj git push` safe even if `jj git fetch` is performed on a timer
188 in the background (this situation is a known issue[^known-issue] with some
189 forms of `git push --force-with-lease`). If the bookmark moves on a remote in a
190 problematic way, `jj git fetch` will create a conflict. This should ensure
191 that the user becomes aware of the conflict before they can `jj git push` and
192 override the bookmark on the remote.
193
194 3. If the remote bookmark already exists on the remote, it must be
195 [tracked](#remotes-and-tracked-bookmarks).
196
197 [^known-issue]: See "A general note on safety" in
198 <https://git-scm.com/docs/git-push#Documentation/git-push.txt---no-force-with-lease>
199
200 ## Conflicts
201
202 Bookmarks can end up in a conflicted state. When that happens, `jj status` will
203 include information about the conflicted bookmarks (and instructions for how to
204 mitigate it). `jj bookmark list` will have details. `jj log` will show the bookmark
205 name with a double question mark suffix (e.g. `main??`) on each of the
206 conflicted bookmark's potential target revisions. Using the bookmark name to look up
207 a revision will resolve to all potential targets. That means that `jj new main`
208 will error out, complaining that the revset resolved to multiple revisions.
209
210 Both local bookmarks (e.g. `main`) and the remote bookmark (e.g. `main@origin`) can
211 have conflicts. Both can end up in that state if concurrent operations were run
212 in the repo. The local bookmark more typically becomes conflicted because it was
213 updated both locally and on a remote.
214
215 To resolve a conflicted state in a local bookmark (e.g. `main`), you can move the
216 bookmark to the desired target with `jj bookmark move`. You may want to first either
217 merge the conflicted targets with `jj new` (e.g. `jj new main`), or you may want to
218 rebase one side on top of the other with `jj rebase`.
219
220 To resolve a conflicted state in a remote bookmark (e.g. `main@origin`), simply
221 pull from the remote (e.g. `jj git fetch`). The conflict resolution will also
222 propagate to the local bookmark (which was presumably also conflicted).
223
224 ## Ease of use
225
226 The use of bookmarks is frequent in some workflows, for example, when
227 interacting with Git repositories containing branches. To this end,
228 one-letter shortcuts have been implemented, both for the `jj bookmark`
229 command itself through an alias (as `jj b`), and for its subcommands.
230 For example, `jj bookmark create BOOKMARK-NAME -r@` can be abbreviated as
231 `jj b c BOOKMARK-NAME -r@`.
232
233 [colocated-workspaces]: git-compatibility.md#colocated-jujutsugit-repos
234 [design]: design/tracking-branches.md