简体   繁体   English

如何使用 simple-git (NODEJS) 提交和推送?

[英]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 .我正在尝试使用simple-git对现有存储库进行简单的提交和推送,但是我在simple-git的 NPM (或 Github)的 API 中找不到任何有关此问题的示例。

I'm talking about this package: https://www.npmjs.com/package/simple-git我说的是这个 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 ?我们如何使用simple-git提交和推送?

Like @Lawrence Cherone said:就像@Lawrence Cherone 说的:

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.这就是我在项目中使用它的方式(尽管我在其中有一个子模块,该示例首先将其(git)工作目录更改为内容(子模块)。之后我只需提交一条消息。

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")等待 git.commit("your_message")
  • await git.push()等待 git.push()
  • await git.push('origin', 'master')等待 git.push('origin', 'master')

You can leave out the 'await' part depending on your code running async.您可以根据运行异步的代码省略“等待”部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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