https://github.com/sargunv/git-credentials
Library to access the git credential API by wrapping the git credential command.
https://github.com/sargunv/git-credentials
git nodejs typescript
Last synced: 3 months ago
JSON representation
Library to access the git credential API by wrapping the git credential command.
- Host: GitHub
- URL: https://github.com/sargunv/git-credentials
- Owner: sargunv
- License: apache-2.0
- Created: 2023-02-11T20:49:24.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-06-06T02:22:27.000Z (about 3 years ago)
- Last Synced: 2025-09-27T22:32:51.228Z (9 months ago)
- Topics: git, nodejs, typescript
- Language: TypeScript
- Homepage:
- Size: 1.3 MB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://www.npmjs.com/package/git-credentials)
[](https://github.com/sargunv/git-credentials/actions/workflows/ci.yml)
[](https://app.codecov.io/gh/sargunv/git-credentials/)
[](https://github.com/sargunv/git-credentials/blob/main/LICENSE)
[](https://github.com/sargunv/git-credentials/blob/main/package.json)
[](https://github.com/sargunv/git-credentials/blob/main/package.json)
# git-credentials
This library wraps the `git credential` command to make it easy to interact with
the git-credential API. It requires `git` to be installed and available in the
`PATH`.
One common use case is to automagically extract the user's saved GitHub token to
use in a tool that interacts with the GitHub API.
For more details, read the [git credential documentation][git-credential].
## Usage
All `gitCredential*` functions have a `cwd` option to set the working directory
where the `git credential` commands will be run.
### git credential fill
From the [git-credential documentation][git-credential]:
> If the action is `fill`, git-credential will attempt to add "username" and
> "password" attributes to the description by reading config files, by
> contacting any configured credential helpers, or by prompting the user.
```ts
import { gitCredentialFill } from "git-credentials"
await gitCredentialFill(
{
protocol: "https",
host: "example.com",
path: "example/repo.git", // optional
username: "example-username", // optional
},
{
// optional but recommended, sets GIT_ASKPASS
askpass: "true",
},
)
```
The above example will return:
```js
{
protocol: 'https',
host: 'example.com',
username: 'example-username',
password: 'example-password'
}
```
This function has some additional options:
- `askpass: string`
- Sets [`GIT_ASKPASS`][git-env-askpass]. If not provided, it'll be inherited
from the environment. I recommend setting it to `true` (a command that does
nothing and exits with code 0) unless you want the user to be prompted for
their credentials in certain contexts (for example in a VSCode terminal). If
unset, `gitCredentialFill` will throw when `git` fails to fill a password.
- `terminalPrompt: boolean`
- Sets [`GIT_TERMINAL_PROMPT`][git-env-terminal-prompt]. If not provided,
it'll be inherited from the environment. If `GIT_ASKPASS` is set and
successful, git will not show the prompt regardless of this setting.
### git credential approve
From the [git-credential documentation][git-credential]:
> If the action is `approve`, git-credential will send the description to any
> configured credential helpers, which may store the credential for later use.
```mjs
import { gitCredentialApprove } from "git-credentials"
await gitCredentialApprove({
protocol: "https",
host: "example.com",
path: "example/repo.git", // optional
username: "example-username",
password: "example-password",
})
```
### git credential reject
From the [git-credential documentation][git-credential]:
> If the action is `reject`, git-credential will send the description to any
> configured credential helpers, which may erase any stored credential matching
> the description.
```mjs
import { gitCredentialReject } from "git-credentials"
await gitCredentialReject({
protocol: "https",
host: "example.com",
path: "example/repo.git", // optional
username: "example-username",
password: "example-password",
})
```
[git-credential]: https://git-scm.com/docs/git-credential
[git-env-askpass]:
https://git-scm.com/docs/git#Documentation/git.txt-codeGITASKPASScode
[git-env-terminal-prompt]:
https://git-scm.com/docs/git#Documentation/git.txt-codeGITTERMINALPROMPTcode