| 1 |
# Git compatibility |
| 2 |
|
| 3 |
Jujutsu has two backends for storing commits. One of them uses a regular Git |
| 4 |
repo, which means that you can collaborate with Git users without them even |
| 5 |
knowing that you're not using the `git` CLI. |
| 6 |
|
| 7 |
See `jj help git` for help about the `jj git` family of commands, and e.g. |
| 8 |
`jj help git push` for help about a specific command (use `jj git push -h` for |
| 9 |
briefer help). |
| 10 |
|
| 11 |
## Supported features |
| 12 |
|
| 13 |
The following list describes which Git features Jujutsu is compatible with. For |
| 14 |
a comparison with Git, including how workflows are different, see the |
| 15 |
[Git-comparison doc](git-comparison.md). |
| 16 |
|
| 17 |
* **Configuration: Partial.** The only configuration from Git (e.g. in |
| 18 |
`~/.gitconfig`) that's respected is the following. Feel free to file a bug if |
| 19 |
you miss any particular configuration options. |
| 20 |
* The configuration of remotes (`[remote "<name>"]`). Simple fetch refspecs |
| 21 |
are respected when branches are not explicitly specified on the CLI. |
| 22 |
(`git` is used for remote operations) |
| 23 |
* `core.excludesFile` |
| 24 |
* **Authentication: Yes.** `git` is used for remote operations under the hood. |
| 25 |
* **Branches: Yes.** You can read more about |
| 26 |
[how branches work in Jujutsu](bookmarks.md) |
| 27 |
and [how they interoperate with Git](#branches). |
| 28 |
* **Tags: Partial.** You can check out tagged commits by name (pointed to by |
| 29 |
either annotated or lightweight tags). You can also create lightweight tags, |
| 30 |
but you cannot create annotated tags. |
| 31 |
* **.gitignore: Yes.** Patterns in `.gitignore` files are supported. So are |
| 32 |
ignores in `.git/info/exclude` or configured via Git's `core.excludesFile` |
| 33 |
config. Since working-copy files are snapshotted by almost every `jj` command, |
| 34 |
you might need to run `jj file untrack` to exclude newly ignored files from the |
| 35 |
working-copy commit. It's recommended to set up the ignore patterns earlier. |
| 36 |
The `.gitignore` support uses a native implementation, so please report a bug |
| 37 |
if you notice any difference compared to `git`. |
| 38 |
* **.gitattributes: No.** There's [#53](https://github.com/jj-vcs/jj/issues/53) |
| 39 |
about adding support for at least the `eol` attribute. |
| 40 |
* **Hooks: No.** There's [#405](https://github.com/jj-vcs/jj/issues/405) |
| 41 |
specifically for providing the checks from <https://pre-commit.com>. |
| 42 |
* **Merge commits: Yes.** Octopus merges (i.e. with more than 2 parents) are |
| 43 |
also supported. |
| 44 |
* **Detached HEAD: Yes.** Jujutsu supports anonymous branches, so this is a |
| 45 |
natural state. |
| 46 |
* **Orphan branch: Yes.** Jujutsu has a virtual root commit that appears as |
| 47 |
parent of all commits Git would call "root commits". |
| 48 |
* **Staging area: Kind of.** The staging area will be ignored. For example, |
| 49 |
`jj diff` will show a diff from the Git HEAD to the working copy. There are |
| 50 |
[ways of fulfilling your use cases without a staging |
| 51 |
area](git-comparison.md#the-index). |
| 52 |
* **Garbage collection: Yes.** It should be safe to run `git gc` in the Git |
| 53 |
repo, but it's not tested, so it's probably a good idea to make a backup of |
| 54 |
the whole workspace first. There's [no garbage collection and repacking of |
| 55 |
Jujutsu's own data structures yet](https://github.com/jj-vcs/jj/issues/12), |
| 56 |
however. |
| 57 |
* **Bare repositories: Yes.** You can use `jj git init --git-repo=<path>` to |
| 58 |
create a repo backed by a bare Git repo. |
| 59 |
* **Submodules: No.** They will not show up in the working copy, but they will |
| 60 |
not be lost either. |
| 61 |
* **Partial clones: No.** |
| 62 |
* **Shallow clones: Kind of.** Shallow commits all have the virtual root commit |
| 63 |
as their parent. However, deepening or fully unshallowing a repository is |
| 64 |
currently not yet supported and will cause issues. |
| 65 |
* **git-worktree: No.** However, there's native support for multiple working |
| 66 |
copies backed by a single repo. See the `jj workspace` family of commands. |
| 67 |
* **Sparse checkouts: No.** However, there's native support for sparse |
| 68 |
checkouts. See the `jj sparse` command. |
| 69 |
* **Signed commits: Yes.** |
| 70 |
You can sign commits automatically [by configuration](config.md#commit-signing), |
| 71 |
or use the `jj sign` command. |
| 72 |
* **Git LFS: No.** ([#80](https://github.com/jj-vcs/jj/issues/80)) |
| 73 |
|
| 74 |
## Creating an empty repo |
| 75 |
|
| 76 |
To create an empty repo using the Git backend, use `jj git init <name>`. This |
| 77 |
creates a [colocated](#colocated-jujutsugit-workspaces) Jujutsu workspace, |
| 78 |
there will be a `.jj` directory and a `.git` directory. |
| 79 |
|
| 80 |
## Creating a repo backed by an existing Git repo |
| 81 |
|
| 82 |
To create a Jujutsu repo backed by a Git repo you already have on disk, use `jj |
| 83 |
git init --git-repo=<path to Git repo> <name>`. The repo will work similar to a |
| 84 |
[Git worktree](https://git-scm.com/docs/git-worktree), meaning that the working |
| 85 |
copies files and the record of the working-copy commit will be separate, but the |
| 86 |
commits will be accessible in both repos. Use `jj git import` to update the |
| 87 |
Jujutsu repo with changes made in the Git repo. Use `jj git export` to update |
| 88 |
the Git repo with changes made in the Jujutsu repo. |
| 89 |
|
| 90 |
## Creating a repo by cloning a Git repo |
| 91 |
|
| 92 |
To create a Jujutsu repo from a remote Git URL, use `jj git clone <URL> |
| 93 |
[<destination>]`. For example, `jj git clone |
| 94 |
https://github.com/octocat/Hello-World` will clone GitHub's "Hello-World" repo |
| 95 |
into a directory by the same name. |
| 96 |
|
| 97 |
By default, the remote repository will be named `origin`. You can use a name of |
| 98 |
your choice by adding `--remote <remote name>` to the `jj git clone` command. |
| 99 |
|
| 100 |
## <a name="colocated-jujutsugit-repos"></a>Colocated Jujutsu/Git workspaces |
| 101 |
|
| 102 |
A colocated Jujutsu workspace is a hybrid Jujutsu/Git workspace. This is the |
| 103 |
default for Git-backed workspace created with `jj git init` or `jj git clone`. |
| 104 |
The Git repo and the Jujutsu workspace then share the same working copy. Jujutsu |
| 105 |
will import and export from and to the Git repo on every `jj` command |
| 106 |
automatically. |
| 107 |
|
| 108 |
This mode is very convenient when tools (e.g. build tools) expect a Git repo to |
| 109 |
be present. |
| 110 |
|
| 111 |
It is allowed to mix `jj` and `git` commands in such a workspace in any order. |
| 112 |
However, it may be easier to keep track of what is going on if you mostly use |
| 113 |
read-only `git` commands and use `jj` to make changes to the repo. One reason |
| 114 |
for this (see below for more) is that `jj` commands will usually put the Git |
| 115 |
repo in a "detached HEAD" state, since in `jj` there is not concept of a |
| 116 |
"currently tracked branch". Before doing mutating Git commands, you may need to |
| 117 |
tell Git what the current branch should be with a `git switch` command. |
| 118 |
|
| 119 |
You can undo the results of mutating `git` commands using `jj undo` and `jj op |
| 120 |
restore`. Inside `jj op log`, changes by `git` will be represented as an "import |
| 121 |
git refs" operation. |
| 122 |
|
| 123 |
You can disable colocation with the `--no-colocate` flag on the commands `jj git |
| 124 |
init` and `jj git clone` or by setting the configuration `git.colocate = false`. |
| 125 |
Much of the repo data will still be stored in the Git repository format, but the |
| 126 |
Git repository will be hidden inside a sub-directory of the `.jj` directory. |
| 127 |
Moreover, unless you explicitly use the `jj git import` and `jj git export` |
| 128 |
commands, that Git repository will either have no branches at all (not even a |
| 129 |
main branch) or will have branches that are out of date with jj's bookmarks. |
| 130 |
|
| 131 |
Colocation can be disabled because it does have some disadvantages: |
| 132 |
|
| 133 |
* Interleaving `jj` and `git` commands increases the chance of confusing branch |
| 134 |
conflicts or [conflicted (AKA divergent) change |
| 135 |
ids](glossary.md#divergent-change). These never lose data, but can be |
| 136 |
annoying. |
| 137 |
|
| 138 |
Such interleaving can happen unknowingly. For example, some IDEs can cause |
| 139 |
it because they automatically run `git fetch` in the background from time to |
| 140 |
time. |
| 141 |
|
| 142 |
* In colocated workspaces with a very large number of branches or other refs, |
| 143 |
`jj` commands can get noticeably slower because of the automatic |
| 144 |
`jj git import` executed on each command. This can be mitigated by |
| 145 |
occasionally running `jj util gc` to speed up the import (that command |
| 146 |
includes packing the Git refs). |
| 147 |
|
| 148 |
* Git tools will have trouble with revisions that contain conflicted files. |
| 149 |
While `jj` renders these files with conflict markers in the working copy, they |
| 150 |
are stored in a non-human-readable fashion inside the repo. Git tools will |
| 151 |
often see this non-human-readable representation. |
| 152 |
|
| 153 |
* When a `jj` branch is conflicted, the position of the branch in the Git repo |
| 154 |
will disagree with one or more of the conflicted positions. The state of that |
| 155 |
branch in git will be labeled as though it belongs to a remote named "git", |
| 156 |
e.g. `branch@git`. |
| 157 |
|
| 158 |
* Jujutsu will ignore Git's staging area. It will not understand merge conflicts |
| 159 |
as Git represents them, unfinished `git rebase` states, as well as other less |
| 160 |
common states a Git repository can be in. |
| 161 |
|
| 162 |
* Colocated workspaces are less resilient to |
| 163 |
[concurrency](technical/concurrency.md#syncing-with-rsync-nfs-dropbox-etc) |
| 164 |
issues if you share the repo using an NFS filesystem or Dropbox. In general, |
| 165 |
such use of Jujutsu is not currently thoroughly tested. |
| 166 |
|
| 167 |
* There may still be bugs when interleaving mutating `jj` and `git` commands, |
| 168 |
usually having to do with a branch pointer ending up in the wrong place. We |
| 169 |
are working on the known ones, and are not aware of any major ones. Please |
| 170 |
report any new ones you find, or if any of the known bugs are less minor than |
| 171 |
they appear. |
| 172 |
|
| 173 |
### Converting a workspace into a colocated workspace |
| 174 |
|
| 175 |
A Jujutsu workspace backed by a Git repo has a full Git repo inside. Such a |
| 176 |
workspace can be converted into a colocated workspace using the |
| 177 |
`jj git colocation` command. |
| 178 |
|
| 179 |
To check the current colocation status of your workspace: |
| 180 |
|
| 181 |
```bash |
| 182 |
jj git colocation status |
| 183 |
``` |
| 184 |
|
| 185 |
To convert to a colocated workspace: |
| 186 |
|
| 187 |
```bash |
| 188 |
jj git colocation enable |
| 189 |
``` |
| 190 |
|
| 191 |
To convert to a non-colocated workspace: |
| 192 |
|
| 193 |
```bash |
| 194 |
jj git colocation disable |
| 195 |
``` |
| 196 |
|
| 197 |
The `jj git colocation enable` command automates the following manual process: |
| 198 |
|
| 199 |
```bash |
| 200 |
# Ignore the .jj directory in Git |
| 201 |
echo '/*' > .jj/.gitignore |
| 202 |
# Move the Git repo |
| 203 |
mv .jj/repo/store/git .git |
| 204 |
# Tell jj where to find it (do not use on Windows! See below.) |
| 205 |
echo -n '../../../.git' > .jj/repo/store/git_target |
| 206 |
# Make the Git repository non-bare and set HEAD |
| 207 |
git config --unset core.bare |
| 208 |
# Convince jj to update .git/HEAD to point to the working-copy commit's parent |
| 209 |
jj new && jj undo |
| 210 |
``` |
| 211 |
|
| 212 |
!!! warning |
| 213 |
|
| 214 |
On Windows, the `echo` command will append line endings and cause `jj` |
| 215 |
to complain about the contents of `git_target`. |
| 216 |
|
| 217 |
Instead of the `echo -n ...` line, use: |
| 218 |
`Set-Content -Path .jj/repo/store/git_target -Value ../../../.git -NoNewLine` |
| 219 |
|
| 220 |
## Branches |
| 221 |
|
| 222 |
TODO: Describe how branches are mapped |
| 223 |
|
| 224 |
## Format mapping details |
| 225 |
|
| 226 |
Paths are assumed to be UTF-8. I have no current plans to support paths with |
| 227 |
other encodings. |
| 228 |
|
| 229 |
Commits created by `jj` have a ref starting with `refs/jj/` to prevent GC. |
| 230 |
|
| 231 |
Commits with conflicts cannot be represented in Git. They appear in the Git |
| 232 |
commit as root directories called`.jjconflict-base-*/` and |
| 233 |
`.jjconflict-side-*/`. Note that the purpose of this representation is only to |
| 234 |
prevent GC of the relevant trees; the authoritative information is in a |
| 235 |
non-standard `jj:trees` commit header. As long as you use `jj` |
| 236 |
commands to work with them, you won't notice those paths. If, on the other hand, |
| 237 |
you use e.g. `git switch` to check one of them out, you will see those |
| 238 |
directories in your working copy. If you then run e.g. `jj status`, the |
| 239 |
resulting snapshot will contain the contents of the first side of the conflict |
| 240 |
as well as the `.jjconflict-*/` directories, making it look like they |
| 241 |
were added to your working copy. You will probably want to run |
| 242 |
`jj abandon` to get back to the state with the unresolved conflicts. |
| 243 |
|
| 244 |
Change IDs are stored in git commit headers as reverse hex encodings. This is |
| 245 |
a non-standard header and is not preserved by all `git` tooling. For example, |
| 246 |
the header is preserved by a `git commit --amend`, but is not preserved through |
| 247 |
a rebase operation. GitHub and other major forges seem to preserve them for the |
| 248 |
most part. The header is written by default in commits created by `jj` (since |
| 249 |
version `0.30.0`) and this functionality can be turned off using the |
| 250 |
`git.write-change-id-header` setting. |
| 251 |
|
| 252 |
Note that `git show` and `git log` do not print this header. To verify presence |
| 253 |
of a `change-id` commit header using `git` itself, use |
| 254 |
[`git-cat-file`](https://git-scm.com/docs/git-cat-file) like this: |
| 255 |
|
| 256 |
```shell |
| 257 |
git cat-file -p <commit ref> |
| 258 |
``` |