Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete a remote branch using Node.js?

Tags:

git

node.js

I'm currently using nodegit to run my git commands and it has worked for everything so far except for deleting a remote branch. I don't mind using another npm package to do this if needed but I would prefer to use nodegit.

Basically, I want a function which can do the same as this command in the terminal

$ git push -d <branch_name>

I want to be able to write something like the following:

function delete_remote_branch(repo, branch_name, callback) {
  repo.getRemote('origin').then(function(remote) {
    repo.getBranch(branch_name).then(function(reference) {
      // delete the branch
      repo.push("-d :"+reference, branch_name).then(function(error_code) { 
        if(error_code) {
          return callback(error_code)
        }
        return callback(null)
      })
    })
  })
}

The documentation for remote.push is here: http://www.nodegit.org/api/remote/#push

Any help would be appreciated. Thanks!

like image 715
Calum Forster Avatar asked Mar 20 '26 00:03

Calum Forster


1 Answers

Push an empty src reference to the origin branch.

remote.push(':refs/heads/my-branch');
like image 148
Chris Thompson Avatar answered Mar 22 '26 16:03

Chris Thompson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!