You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

@lennym/commit

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@lennym/commit

Simple API for committing file changes to github

1.0.1
latest
Source
npm
Version published
Weekly downloads
105
-38.24%
Maintainers
1
Weekly downloads
 
Created
Source

@lennym/commit

API for committing file changes to github

Usage

To write content to a file on the default branch:

const Git = require('@lennym/commit');

const git = Git({
  repo: 'lennym/test-repo',
  token: 'YOUR_GITHUB_ACCESS_TOKEN'
});

git
  .add('file.txt', 'File content')
  .commit('Set file content')
  .push()
  .then(() => console.log('All done'));

Branches

To write to a branch other than the default branch for the repo:

const git = Git({
  repo: 'lennym/test-repo',
  token: '...',
  branch: 'development'
});

git
  .add('file.txt', 'File content')
  .commit('Set file content')
  .push()
  .then(() => console.log('All done'));

To create a new branch:

const git = Git({
  repo: 'lennym/test-repo',
  token: '...'
});

git
  .branch('my-new-branch')
  .add('file.txt', 'File content')
  .commit('Set file content')
  .push()
  .then(() => console.log('All done'));

Updating multiple files

To update multiple files in one commit:

const git = Git({
  repo: 'lennym/test-repo',
  token: '...'
});

git
  .add('file1.txt', 'File 1 content')
  .add('file2.txt', 'File 2 content')
  .commit('Set multiple file content')
  .push()
  .then(() => console.log('All done'));

To update multiple files in multiple commits:

const git = Git({
  repo: 'lennym/test-repo',
  token: '...'
});

git
  .add('file1.txt', 'File 1 content')
  .commit('Update file 1')
  .add('file2.txt', 'File 2 content')
  .commit('Update file 2')
  .push()
  .then(() => console.log('All done'));

Non-text files

To add a non-text file, pass a Buffer instead of a string to add:

const img = fs.readFileSync('./some-image.png');

const git = Git({
  repo: 'lennym/test-repo',
  token: '...'
});

git
  .add('image.png', img)
  .commit('Add image file')
  .push()
  .then(() => console.log('All done'));

Other actions

Get the hash of the latest commit

const git = Git({
  repo: 'lennym/test-repo',
  token: '...'
});

git.head()
  .then(sha => console.log(sha));

Keywords

git

FAQs

Package last updated on 28 Feb 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts