简体   繁体   中英

How to commit and push with simple-git (NODEJS)?

I'm trying to do a simple Commit & Push to an existing repo using simple-git however I can't find any example regarding this issue in the API in NPM (or Github) of simple-git .

I'm talking about this package: https://www.npmjs.com/package/simple-git

Consider the code:

const gitRepo = 'REPO-URL';
const tempFolder = '...';


// Simple Git
const simpleGit = require('simple-git')();

const options = ['--depth', '1'];
const callback = () => {
    console.log('Done cloning!');      
    // Now change some code in the cloned code 
    // and commit && push 
};

// Cloning ...
simpleGit.outputHandler((command, stdout, stderr) => {
    stdout.pipe(process.stdout);
    stderr.pipe(process.stderr)

    stdout.on('data', (data) => {
        // Print data
        console.log(data.toString('utf8'));})
    })
    .clone(gitRepo, tempFolder, options, callback);

How can we commit and push using simple-git ?

Like @Lawrence Cherone said:

You can just use the basic commands as is.

This is how i used it in my project ( though i got a submodule in it where this example is changing its (git)working directory to content(submodule) first. After that i just commit with a message.

app.post("/gitCommit", async function(req, res) {
  try {
    await git.cwd({ path: 'content' }).commit(req.body.msg);    
    res.sendStatus(200)
  } catch(err) {
    console.log(err)
  }
});

If you already have a working and initialised repo your in, then you could just do the following:

  • await git.commit("your_message")
  • await git.push()
  • await git.push('origin', 'master')

You can leave out the 'await' part depending on your code running async.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM