8

Is there a way to delete a branch from a Git repository without doing a clone or any other sort of local copy?

Basically I'm working on a dashboard for a release pipeline and don't want to have to have any of the working project's code on the dashboards server just to delete deployed feature branches.

In case it matters, we use Atlassian Bitbucket (f.k.a Stash) and not Github.

I'm looking to do something similar to:

git branch -D ssh://git@repository.com/team/project/feature/deleteme
Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
jdarling
  • 2,438
  • 2
  • 14
  • 10
  • Can't exactly call it a duplicate, but [here's a similar question and a good answer that covers your case just as well](http://stackoverflow.com/questions/2003505/delete-a-git-branch-both-locally-and-remotely). UPD: ah, no local copy as well. Then I believe the answer is **no**, unless Stash has this option in its web interface. – D-side Aug 06 '15 at 21:41
  • I saw that, problem is that doesn't work if you don't have a local clone of the project. – jdarling Aug 06 '15 at 21:44
  • If you don't have a local clone, your only means of interacting with that repository is Stash web interface. [`STASH-3347`](https://jira.atlassian.com/browse/STASH-3347) is a fulfilled feature request that seems to be related. – D-side Aug 06 '15 at 21:47
  • Or you actually need a shell command to do that programmatically? – D-side Aug 06 '15 at 21:49
  • Well, might be time to look at js-git or something similar. Leaving open for a little bit just in case someone comes up with something :D – jdarling Aug 06 '15 at 21:51

3 Answers3

8

Easy, from any git repo

git push <repository url> +:refs/heads/branchname

and if you don't happen to have one handy, just make a trash one anywhere with e.g. git init deleteme.

Some repo administrators who've had to give push access to unreliable developers have shut off delete-by-push access, so some repos will reject that command, but it's enabled by default.

thSoft
  • 21,755
  • 5
  • 88
  • 103
jthill
  • 55,082
  • 5
  • 77
  • 137
  • keyboard-to-editbox warning, I'm on a borrowed machine atm w/o a git available. – jthill Aug 06 '15 at 23:13
  • 1
    How exactly is that supposed to work? Sorry, haven't seen it before – jdarling Aug 07 '15 at 01:17
  • git init then, get add remote, then do the above? – jdarling Aug 07 '15 at 01:17
  • No need to add a remote, you can just use the URL directly. – jthill Aug 07 '15 at 01:20
  • 2
    Perfect, finally figured out that u://r/l was a hint to put the actual URL there. Tried it command line and of course works a treat. Thanks greatly. – jdarling Aug 08 '15 at 18:12
  • Not sure when `+` is required, but here is the general documentation: "Pushing an empty allows you to delete the ref from the remote repository" (see: https://git-scm.com/docs/git-push#:~:text=githooks%5B5%5D.-,Pushing%20an%20empty,-%3Csrc%3E%20allows%20you) – haridsv Feb 02 '23 at 10:36
3

You can use Stash REST API (branch utils). You'll need to send an authenticated (see the link for details) DELETE-request to an URL like:

/rest/branch-utils/1.0/projects/{projectKey}/repos/{repositorySlug}/branches

...with parameter name that specifies the branch you want to delete and (optional?) dryRun that, judging by the name, doesn't actually apply changes but results in a response as if the changes were applied.

Request can be issued by pretty much anything able to authenticate. curl can do that, for instance.

D-side
  • 9,150
  • 3
  • 28
  • 44
0

You would have to write a custom git client, possibly using jgit, to do this if the Stash web interface doesn't have an option.

Borealid
  • 95,191
  • 9
  • 106
  • 122