1. Get the Reference of the branch you want to commit to and store the SHA for the latest commit (SHA-LATEST-COMMIT)
https://developer.github.com/v3/git/refs/#get-a-reference
const res = getReference( BRANCH );
const baseCommitSHA = res.object.sha;
https://developer.github.com/v3/git/commits/#get-a-commit
const res = getCommit(baseCommitSHA);
const baseTreeSHA = res.tree.sha;
https://developer.github.com/v3/git/trees/#create-a-tree
- If you have larger files and/or binaries: Create a Blob for each file to commit and store the SHA of each file created: https://developer.github.com/v3/git/blobs/#create-a-blob
//Method 1
const dataEncoding = 'base64';
const fileContent = new Buffer(fs.readFileSync(filePath)).toString(dataEncoding),
const res = createBlob({
content: fileContent,
encoding: dataEncoding
});
const fileSHA = res.sha;
**At this point you should use in the treeData the field sha **
- If you don't have larger files or binaries: Just read the file contents and encode them in utf-8.
//Method 2
const fileContent = new Buffer(fs.readFileSync(filePath)).toString('utf-8'),
**At this point you should use in the treeData the field content **
Create your tree JSON:
// Using method 1
const treeData = [
{
path: "file.js",
mode: "100644",
type: "blob",
sha: fileSHA
},
];
// Using method 2
const treeData = [
{
path: "file.js",
mode: "100644",
type: "blob",
content: fileContent
},
];
Finally create the new tree and Store it's sha
//If you want to delete a file in this commit, you should not use the base_tree, every entry not sent = deleted file.
//If you don't want to delete any file, use the tree_base = baseTreeSHA
const res = createTree({
base_tree: baseTreeSHA,
tree: treeData
});
const treeSHA = res.sha;
https://developer.github.com/v3/git/commits/#create-a-commit
const res = createCommit({
message: 'my commit message',
tree: treeSHA,
parents: [baseCommitSHA]
});
const commitSHA = res.sha;
https://developer.github.com/v3/git/refs/
updateReference({
sha: commitSHA
});
If everything have gone right at this time you should have a another commit with your changes.