https://github.com/vweevers/find-gitdir
Find .git directory with support of submodules and --separate-git-dir.
https://github.com/vweevers/find-gitdir
git gitdir nodejs npm-package submodules
Last synced: 8 days ago
JSON representation
Find .git directory with support of submodules and --separate-git-dir.
- Host: GitHub
- URL: https://github.com/vweevers/find-gitdir
- Owner: vweevers
- License: mit
- Created: 2020-11-28T21:20:11.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2023-11-01T04:59:25.000Z (over 2 years ago)
- Last Synced: 2025-07-04T23:54:40.878Z (7 months ago)
- Topics: git, gitdir, nodejs, npm-package, submodules
- Language: JavaScript
- Homepage:
- Size: 12.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# find-gitdir
**Find `.git` directory with support of submodules, [`--separate-git-dir`](https://git-scm.com/docs/git-init#Documentation/git-init.txt---separate-git-dirltgitdirgt) and [linked worktrees](https://git-scm.com/docs/git-worktree).**
[](https://www.npmjs.org/package/find-gitdir)
[](https://www.npmjs.org/package/find-gitdir)

[](https://standardjs.com)
## Usage
```js
const gitdir = require('find-gitdir')
```
Let's say we have a git repository at `/example` and that this is our working directory. Its gitdir will commonly be at `/example/.git`.
```js
await gitdir() // /example/.git
```
Let's also say we have a git submodule at `./beep`. Then `find-gitdir` will find a symbolic link at `./beep/.git` pointing to the gitdir `../.git/modules/beep`:
```js
await gitdir('./beep') // /example/.git/modules/beep
```
By default `find-gitdir` does not look in parent directories. It can be enabled:
```js
await gitdir('./node_modules/find-gitdir') // null
await gitdir('./node_modules/find-gitdir', { roam: true }) // /example/.git
```
## API
### `gitdir([cwd][, options][, callback])`
Yields an absolute path to a `.git` directory or `null` if not found, given working directory `cwd` which defaults to `process.cwd()`. If no callback if provided, a promise is returned. Options:
- `roam` (boolean, default `false`): enable looking in parent directories
- `common` (boolean, default `false`): if `cwd` is a [linked worktree](https://git-scm.com/docs/git-worktree) then return the gitdir of the main worktree (a.k.a. [`$GIT_COMMON_DIR`](https://git-scm.com/docs/gitrepository-layout)) instead of the gitdir of the linked worktree. This can be useful if you need non-worktree files like `config`.
If `options` is a boolean, it is interpreted as `options.roam` for backwards compatibility.
### `gitdir.sync([cwd][, options])`
Synchronous variant. Returns an absolute path or `null`.
## Install
With [npm](https://npmjs.org) do:
```
npm install find-gitdir
```
## License
[MIT](LICENSE) © Vincent Weevers