Skip to content

Commit b090708

Browse files
author
John Haley
committed
Add guide for initializing a repo
1 parent c89ab3b commit b090708

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

guides/repositories/initializing/README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,59 @@ title: Initializing
55
description: How to initialize a repository
66
---
77

8+
**In order to run examples, you will need to [Install NodeGit](../../install/basics)
9+
first.**
10+
811
[Return to repository guides](../)
912

1013
* * *
14+
15+
Initializing a Repository
16+
-------------------------
17+
18+
This guide explains how to create a new repository.
19+
20+
[View example source](index.js)
21+
22+
### Requiring NodeGit
23+
24+
In the guides directory, we like to keep our NodeGit relative to the project
25+
root.
26+
27+
``` javascript
28+
var NodeGit = require("../../../");
29+
```
30+
31+
However, in your project you will most likely be using the following command:
32+
33+
``` javascript
34+
var NodeGit = require("nodegit");
35+
```
36+
37+
### Arguments to initialize a repo
38+
39+
There are 2 arguments to the `init` method, a path to initialize the repo in
40+
and whether or not to make a `.git` subfolder in that directory or use the
41+
passed in directory as the `.git` folder itself.
42+
43+
44+
``` javascript
45+
var pathToRepo = require("path").resolve("../my-git-projects/my-project");
46+
var isBare = 0; // lets create a .git subfolder
47+
```
48+
49+
50+
### Initialize the Repo
51+
52+
Now that we have our arguments we can call the `init` method on the
53+
`NodeGit.Repository` module to create the repo.
54+
55+
``` javascript
56+
NodeGit.Repository.init(pathToRepo, isBare).then(function (repo) {
57+
// In this function we have a repo object that we can perform git operations
58+
// on.
59+
60+
// Note that with a new repository many functions will fail until there is
61+
// an initial commit.
62+
});
63+
```
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Require in NodeGit, since we want to use the local copy, we're using a
2+
// relative path. In your project, you will use:
3+
//
4+
// var NodeGit = require("nodegit");
5+
var NodeGit = require("../../../");
6+
7+
// Using the `init` method from the `NodeGit.Repository` module, we can create
8+
// a blank repository using NodeGit
9+
var pathToRepo = require("path").resolve("../my-git-projects/my-project");
10+
11+
// Setting this to 0 will have the effect creating a `.git` folder inside of
12+
// passed path. If this is 1 then we will use the passed path AS our `.git`
13+
// folder.
14+
var isBare = 0;
15+
16+
// In NodeGit we use Promises to make callbacks easier to deal with.
17+
//
18+
// For more information visit https://www.promisejs.org/
19+
NodeGit.Repository.init(pathToRepo, isBare).then(function (repo) {
20+
// In this function we have a repo object that we can perform git operations
21+
// on.
22+
23+
// Note that with a new repository many functions will fail until there is
24+
// an initial commit.
25+
})
26+
.catch(function (reasonForFailure) {
27+
// If the repo cannot be created for any reason we can handle that case here.
28+
// NodeGit won't init a repo over a pre-existing repo.
29+
});

0 commit comments

Comments
 (0)