|
| 1 | +# Git Authentication |
| 2 | + |
| 3 | +Cargo supports some forms of authentication when using git dependencies and |
| 4 | +registries. This appendix contains some information for setting up git |
| 5 | +authentication in a way that works with Cargo. |
| 6 | + |
| 7 | +If you need other authentication methods, the [`net.git-fetch-with-cli`] |
| 8 | +config value can be set to cause Cargo to execute the `git` executable to |
| 9 | +handle fetching remote repositories instead of using the built-in support. |
| 10 | +This can be enabled with the `CARGO_NET_GIT_FETCH_WITH_CLI=true` environment |
| 11 | +variable. |
| 12 | + |
| 13 | +## HTTPS authentication |
| 14 | + |
| 15 | +HTTPS authentication requires the [`credential.helper`] mechanism. There are |
| 16 | +multiple credential helpers, and you specify the one you want to use in your |
| 17 | +global git configuration file. |
| 18 | + |
| 19 | +```ini |
| 20 | +# ~/.gitconfig |
| 21 | + |
| 22 | +[credential] |
| 23 | +helper = store |
| 24 | +``` |
| 25 | + |
| 26 | +Cargo does not ask for passwords, so for most helpers you will need to give |
| 27 | +the helper the initial username/password before running Cargo. One way to do |
| 28 | +this is to run `git clone` of the private git repo and enter the |
| 29 | +username/password. |
| 30 | + |
| 31 | +> **Tip:**<br> |
| 32 | +> macOS users may want to consider using the osxkeychain helper.<br> |
| 33 | +> Windows users may want to consider using the [GCM] helper. |
| 34 | +
|
| 35 | +> **Note:** Windows users will need to make sure that the `sh` shell is |
| 36 | +> available in your `PATH`. This typically is available with the Git for |
| 37 | +> Windows installation. |
| 38 | +
|
| 39 | +## SSH authentication |
| 40 | + |
| 41 | +SSH authentication requires `ssh-agent` to be running to acquire the SSH key. |
| 42 | +Make sure the appropriate environment variables are set up (`SSH_AUTH_SOCK` on |
| 43 | +most Unix-like systems), and that the correct keys are added (with `ssh-add`). |
| 44 | +Windows uses Pageant for SSH authentication. |
| 45 | + |
| 46 | +> **Note:** Cargo does not support git's shorthand SSH URLs like |
| 47 | +> `git@example.com/user/repo.git`. Use a full SSH URL like |
| 48 | +> `ssh://git@example.com/user/repo.git`. |
| 49 | +> |
| 50 | +> **Note:** SSH configuration files (like OpenSSH's `~/.ssh/config`) are not |
| 51 | +> used by Cargo's built-in SSH library. More advanced requirements should use |
| 52 | +> [`net.git-fetch-with-cli`]. |
| 53 | +
|
| 54 | +[`credential.helper`]: https://git-scm.com/book/en/v2/Git-Tools-Credential-Storage |
| 55 | +[`net.git-fetch-with-cli`]: ../reference/config.md#netgit-fetch-with-cli |
| 56 | +[GCM]: https://github.com/Microsoft/Git-Credential-Manager-for-Windows/ |
0 commit comments