| 1 |
# Using Jujutsu with GitHub and GitLab Projects |
| 2 |
|
| 3 |
This guide assumes a basic understanding of either Git or Mercurial. |
| 4 |
|
| 5 |
## Basic workflow |
| 6 |
|
| 7 |
The simplest way to start with Jujutsu is to create a stack of commits first. |
| 8 |
You will only need to create a bookmark when you need to push the stack to a |
| 9 |
remote. There are two primary workflows: using a generated bookmark name or |
| 10 |
naming a bookmark. |
| 11 |
|
| 12 |
### Using a generated bookmark name |
| 13 |
|
| 14 |
In this example we're letting Jujutsu auto-create a bookmark. |
| 15 |
|
| 16 |
```shell |
| 17 |
# Start a new commit off of the default bookmark. |
| 18 |
$ jj new main |
| 19 |
# Refactor some files, then add a description and start a new commit |
| 20 |
$ jj commit -m 'refactor(foo): restructure foo()' |
| 21 |
# Add a feature, then add a description and start a new commit |
| 22 |
$ jj commit -m 'feat(bar): add support for bar' |
| 23 |
# Let Jujutsu generate a bookmark name and push that to GitHub. Note that we |
| 24 |
# push the working-copy commit's *parent* because the working-copy commit |
| 25 |
# itself is empty. |
| 26 |
$ jj git push --change @- # or -c for short |
| 27 |
``` |
| 28 |
|
| 29 |
### Using a named bookmark |
| 30 |
|
| 31 |
In this example, we create a bookmark named `bar` and then push it to the remote. |
| 32 |
|
| 33 |
```shell |
| 34 |
# Start a new commit off of the default bookmark. |
| 35 |
$ jj new main |
| 36 |
# Refactor some files, then add a description and start a new commit |
| 37 |
$ jj commit -m 'refactor(foo): restructure foo()' |
| 38 |
# Add a feature, then add a description and start a new commit |
| 39 |
$ jj commit -m 'feat(bar): add support for bar' |
| 40 |
# Create a bookmark so we can push it to GitHub. Note that we created the bookmark |
| 41 |
# on the working-copy commit's *parent* because the working copy itself is empty. |
| 42 |
$ jj bookmark create bar -r @- # `bar` now contains the previous two commits. |
| 43 |
# Set the bookmark to be tracked on the remote. |
| 44 |
$ jj bookmark track bar |
| 45 |
# Push the bookmark to GitHub (pushes only `bar`) |
| 46 |
$ jj git push |
| 47 |
``` |
| 48 |
|
| 49 |
While it's possible to create a bookmark in advance and commit on top of it in a |
| 50 |
Git-like manner, you will then need to move the bookmark manually when you create |
| 51 |
a new commits. Unlike Git, Jujutsu will not do it automatically. |
| 52 |
|
| 53 |
## Updating the repository |
| 54 |
|
| 55 |
Jujutsu does not currently have a direct equivalent of `git pull`, though it may |
| 56 |
gain [a similar command someday][sync-issue]. |
| 57 |
|
| 58 |
In the meantime, updating your branches is a two step process. The first is to |
| 59 |
`jj git fetch` to fetch everything that's happened on your remote. The second is |
| 60 |
to rebase all of your branches on top of your `main` branch with `jj rebase -o |
| 61 |
main`. This command's default is the same as passing `-b @`, so if you have more |
| 62 |
than one outstanding branch, you'll need to call `jj rebase` again with the `-b` |
| 63 |
flag for each of them, or pass multiple `-b` arguments, one for every branch. |
| 64 |
|
| 65 |
[sync-issue]: https://github.com/jj-vcs/jj/issues/1039 |
| 66 |
|
| 67 |
## Working in a Git colocated workspaces |
| 68 |
|
| 69 |
After doing `jj git init`, which |
| 70 |
[colocates](git-compatibility.md#colocated-jujutsugit-repos) the `.jj` and |
| 71 |
`.git` directories, Git will be in a [detached HEAD state][detached], which is |
| 72 |
unusual, as Git mainly works with named branches; jj does not. |
| 73 |
|
| 74 |
In a colocated workspace, every `jj` command will automatically synchronize |
| 75 |
Jujutsu's view of the repo with Git's view. For example, `jj commit` updates the |
| 76 |
HEAD of the Git repository, enabling an incremental migration. |
| 77 |
|
| 78 |
```shell |
| 79 |
$ nvim docs/tutorial.md |
| 80 |
$ # Do some more work. |
| 81 |
$ jj commit -m "Update tutorial" |
| 82 |
# Create a bookmark on the working-copy commit's parent |
| 83 |
$ jj bookmark create doc-update -r @- |
| 84 |
$ jj bookmark track doc-update |
| 85 |
$ jj git push |
| 86 |
``` |
| 87 |
|
| 88 |
## Working in a Jujutsu repository |
| 89 |
|
| 90 |
In a Jujutsu repository, the workflow is simplified. If there's no need for |
| 91 |
explicitly named bookmarks, you can just generate one for a change. As Jujutsu is |
| 92 |
able to create a bookmark for a revision. |
| 93 |
|
| 94 |
```shell |
| 95 |
$ # Do your work |
| 96 |
$ jj commit |
| 97 |
$ # Push change "mw", letting Jujutsu automatically create a bookmark called |
| 98 |
$ # "push-mwmpwkwknuz" |
| 99 |
$ jj git push -c mw |
| 100 |
``` |
| 101 |
|
| 102 |
## Addressing review comments |
| 103 |
|
| 104 |
There are two workflows for addressing review comments, depending on your |
| 105 |
project's preference. Many projects prefer that you address comments by adding |
| 106 |
commits to your bookmark[^1]. Some projects (such as Jujutsu and LLVM) instead |
| 107 |
prefer that you keep your commits clean by rewriting them and then |
| 108 |
force-pushing[^2]. |
| 109 |
|
| 110 |
### Adding new commits |
| 111 |
|
| 112 |
If your project prefers that you address review comments by adding commits on |
| 113 |
top, you can do that by doing something like this: |
| 114 |
|
| 115 |
```shell |
| 116 |
$ # Create a new commit on top of the `your-feature` bookmark from above. |
| 117 |
$ jj new your-feature |
| 118 |
$ # Address the comments by updating the code. Then review the changes. |
| 119 |
$ jj diff |
| 120 |
$ # Give the fix a description and create a new working-copy on top. |
| 121 |
$ jj commit -m 'address pr comments' |
| 122 |
$ # Update the bookmark to point to the new commit. |
| 123 |
$ jj bookmark move your-feature --to @- |
| 124 |
$ # Push it to your remote |
| 125 |
$ jj git push |
| 126 |
``` |
| 127 |
|
| 128 |
Notably, the above workflow creates a new commit for you. The same can be |
| 129 |
achieved without creating a new commit. |
| 130 |
|
| 131 |
!!! warning |
| 132 |
|
| 133 |
We strongly suggest to `jj new` after the example below, as all further edits |
| 134 |
still get amended to the previous commit. |
| 135 |
|
| 136 |
```shell |
| 137 |
$ # Create a new commit on top of the `your-feature` bookmark from above. |
| 138 |
$ jj new your-feature |
| 139 |
$ # Address the comments by updating the code. Then review the changes. |
| 140 |
$ jj diff |
| 141 |
$ # Give the fix a description. |
| 142 |
$ jj describe -m 'address pr comments' |
| 143 |
$ # Update the bookmark to point to the current commit. |
| 144 |
$ jj bookmark move your-feature --to @ |
| 145 |
$ # Push it to your remote |
| 146 |
$ jj git push |
| 147 |
``` |
| 148 |
|
| 149 |
### Rewriting commits |
| 150 |
|
| 151 |
If your project prefers that you keep commits clean, you can do that by doing |
| 152 |
something like this: |
| 153 |
|
| 154 |
```shell |
| 155 |
$ # Create a new commit on top of the second-to-last commit in `your-feature`, |
| 156 |
$ # as reviewers requested a fix there. |
| 157 |
$ jj new your-feature- # NOTE: the trailing hyphen is not a typo! |
| 158 |
$ # Address the comments by updating the code. Then review the changes. |
| 159 |
$ jj diff |
| 160 |
$ # Squash the changes into the parent commit |
| 161 |
$ jj squash |
| 162 |
$ # Push the updated bookmark to the remote. Jujutsu automatically makes it a |
| 163 |
$ # force push |
| 164 |
$ jj git push --bookmark your-feature |
| 165 |
``` |
| 166 |
|
| 167 |
The hyphen after `your-feature` comes from the [revset](revsets.md) syntax. |
| 168 |
|
| 169 |
## Working with other people's bookmarks |
| 170 |
|
| 171 |
By default, `jj git clone` imports the default remote bookmark (which is usually |
| 172 |
`main` or `master`), but `jj git fetch` doesn't import new remote bookmarks to |
| 173 |
local bookmarks. This means that if you want to iterate or test another |
| 174 |
contributor's bookmark, you'll need to do `jj new <bookmark>@<remote>` onto it. |
| 175 |
|
| 176 |
If you want to import all remote bookmarks including inactive ones, set |
| 177 |
`remotes.<name>.auto-track-bookmarks = "*"` in the config file. Then you can |
| 178 |
specify a contributor's bookmark as `jj new <bookmark>` instead of `jj new |
| 179 |
<bookmark>@<remote>`. |
| 180 |
|
| 181 |
You can find more information on that setting [here][auto-bookmark]. |
| 182 |
|
| 183 |
## Using GitHub CLI |
| 184 |
|
| 185 |
GitHub CLI will have trouble finding the proper Git repository path in jj repos |
| 186 |
that aren't [colocated](./git-compatibility.md#colocated-jujutsugit-repos) |
| 187 |
(see [issue #1008]). You can configure the `$GIT_DIR` environment variable to |
| 188 |
point it to the right path: |
| 189 |
|
| 190 |
```shell |
| 191 |
$ GIT_DIR=$(jj git root) gh issue list |
| 192 |
``` |
| 193 |
|
| 194 |
You can make that automatic by installing [direnv](https://direnv.net) and |
| 195 |
defining hooks in a `.envrc` file in the repository root to configure `$GIT_DIR`. |
| 196 |
Just add this line into `.envrc`: |
| 197 |
|
| 198 |
```shell |
| 199 |
export GIT_DIR=$(jj git root) |
| 200 |
``` |
| 201 |
|
| 202 |
and run `direnv allow` to approve it for direnv to run. Then GitHub CLI will |
| 203 |
work automatically even in workspaces that aren't colocated so you can execute |
| 204 |
commands like `gh issue list` normally. |
| 205 |
|
| 206 |
[issue #1008]: https://github.com/jj-vcs/jj/issues/1008 |
| 207 |
|
| 208 |
## Useful Revsets |
| 209 |
|
| 210 |
Log all revisions across all local bookmarks that aren't on the main bookmark nor |
| 211 |
on any remote: |
| 212 |
|
| 213 |
```shell |
| 214 |
$ jj log -r 'bookmarks() & ~(main | remote_bookmarks())' |
| 215 |
``` |
| 216 |
|
| 217 |
Log all revisions that you authored, across all bookmarks that aren't on any |
| 218 |
remote: |
| 219 |
|
| 220 |
```shell |
| 221 |
$ jj log -r 'mine() & bookmarks() & ~remote_bookmarks()' |
| 222 |
``` |
| 223 |
|
| 224 |
Log all remote bookmarks that you authored or committed to: |
| 225 |
|
| 226 |
```shell |
| 227 |
$ jj log -r 'remote_bookmarks() & (mine() | committer([email protected]))' |
| 228 |
``` |
| 229 |
|
| 230 |
Log all ancestors of the current working copy that aren't on any remote: |
| 231 |
|
| 232 |
```shell |
| 233 |
$ jj log -r 'remote_bookmarks()..@' |
| 234 |
``` |
| 235 |
|
| 236 |
## Merge conflicts |
| 237 |
|
| 238 |
For a detailed overview, how Jujutsu handles conflicts, revisit |
| 239 |
the [tutorial][tut]. |
| 240 |
|
| 241 |
[^1]: |
| 242 |
This is a GitHub-style review, as GitHub currently only is able to compare |
| 243 |
bookmarks. |
| 244 |
|
| 245 |
[^2]: |
| 246 |
If you're wondering why we prefer clean commits in this project, see |
| 247 |
e.g. [this blog post][stacked] |
| 248 |
|
| 249 |
[auto-bookmark]: config.md#automatic-tracking-of-bookmarks |
| 250 |
[detached]: https://git-scm.com/docs/git-checkout#_detached_head |
| 251 |
[tut]: tutorial.md#conflicts |
| 252 |
[stacked]: https://jg.gg/2018/09/29/stacked-diffs-versus-pull-requests/ |
| 253 |
|
| 254 |
## Using several remotes |
| 255 |
|
| 256 |
It is common to use several remotes when contributing to a shared repository. |
| 257 |
For example, "upstream" can designate the remote where the changes will be |
| 258 |
merged through a pull-request while "origin" is your private fork of the |
| 259 |
project. |
| 260 |
|
| 261 |
```shell |
| 262 |
$ jj git clone --remote upstream https://github.com/upstream-org/repo |
| 263 |
$ cd repo |
| 264 |
$ jj git remote add origin [email protected]:your-org/your-repo-fork |
| 265 |
``` |
| 266 |
|
| 267 |
This will automatically setup your repository to track the main |
| 268 |
bookmark from the upstream repository, typically `main@upstream` |
| 269 |
or `master@upstream`. |
| 270 |
|
| 271 |
You might want to `jj git fetch` from "upstream" and to `jj git push` |
| 272 |
to "origin". You can configure the default remotes to fetch from and |
| 273 |
push to in your configuration file |
| 274 |
(`jj config edit --user|repo|workspace`): |
| 275 |
|
| 276 |
```toml |
| 277 |
[git] |
| 278 |
fetch = "upstream" |
| 279 |
push = "origin" |
| 280 |
``` |
| 281 |
|
| 282 |
The default for both `git.fetch` and `git.push` is "origin". |
| 283 |
|
| 284 |
If you usually work on a project from several computers, you may |
| 285 |
configure `jj` to fetch from both repositories by default, in order to |
| 286 |
keep your own bookmarks synchronized through your `origin` repository: |
| 287 |
|
| 288 |
```toml |
| 289 |
[git] |
| 290 |
fetch = ["upstream", "origin"] |
| 291 |
push = "origin" |
| 292 |
``` |
| 293 |
|
| 294 |
## Git push options |
| 295 |
|
| 296 |
`jj git push` supports passing Git “push options” to the server via |
| 297 |
`-o`/`--option`. These are forwarded to the remote and interpreted by the hosting |
| 298 |
platform, if it supports them. You can repeat `-o` to send multiple options. |
| 299 |
|
| 300 |
- Syntax: `jj git push -o <push_option>` or `jj git push --option <push_option>` |
| 301 |
- Multiple options: `jj git push -o foo -o bar=val` |
| 302 |
- Quoting: If the option’s value has spaces, wrap it in double quotes. |
| 303 |
|
| 304 |
Support is server-dependent. GitLab supports push options for CI and merge |
| 305 |
requests; other platforms may not. Refer to your host’s documentation for the |
| 306 |
full list and behavior. |
| 307 |
|
| 308 |
Examples with GitLab push options (see GitLab docs for details): |
| 309 |
|
| 310 |
- Skip CI for a push: |
| 311 |
```shell |
| 312 |
jj git push -o ci.skip |
| 313 |
``` |
| 314 |
- Pass CI variables to the created pipeline: |
| 315 |
```shell |
| 316 |
jj git push -o 'ci.variable=MAX_RETRIES=10' -o 'ci.variable=MAX_TIME=600' |
| 317 |
``` |
| 318 |
- Create a merge request with metadata on push: |
| 319 |
```shell |
| 320 |
jj git push \ |
| 321 |
-o merge_request.create \ |
| 322 |
-o merge_request.target=main \ |
| 323 |
-o 'merge_request.title=Add feature X' \ |
| 324 |
-o 'merge_request.description=Implements X with tests' \ |
| 325 |
-o merge_request.draft |
| 326 |
``` |
| 327 |
- Auto-merge when pipeline succeeds and remove the source branch: |
| 328 |
```shell |
| 329 |
jj git push \ |
| 330 |
-o merge_request.merge_when_pipeline_succeeds \ |
| 331 |
-o merge_request.remove_source_branch |
| 332 |
``` |
| 333 |
- Add and remove labels: |
| 334 |
```shell |
| 335 |
jj git push \ |
| 336 |
-o 'merge_request.label=label1' \ |
| 337 |
-o 'merge_request.label=label2' \ |
| 338 |
-o 'merge_request.unlabel=label3' |
| 339 |
``` |
| 340 |
- Assign and unassign users: |
| 341 |
```shell |
| 342 |
jj git push \ |
| 343 |
-o 'merge_request.assign=user1' \ |
| 344 |
-o 'merge_request.assign=user2' \ |
| 345 |
-o 'merge_request.unassign=user3' |
| 346 |
``` |