7
git ls-remote --h <domain_specific_url>.git 'refs/heads/*' 

which lists all the branches created on remote repository.

But I would like to extend this command to use --merged option, since I would like to get lists of branches which are already merged to develop/master

something like:

git ls-remote --h <domain_specific_url>.git 'refs/heads/*' --merged develop/master 

Is this possible?

I could find many solutions, but I don't want to work with cloned repository. I need simply one command which takes repository URL and works.

rkta
  • 3,959
  • 7
  • 25
  • 37
Neha
  • 73
  • 1
  • 4

2 Answers2

12

the good new is that gitbash also provides some usefull Un*x utilities:

 git branch -r --merged master | grep "origin/" 
Timothy Truckle
  • 15,071
  • 2
  • 27
  • 51
  • But here in this case how should I specify "url of remote repository"? The above command works only when you have local repository. Isn't it? – Neha Jun 26 '18 at 11:04
  • please suggest me if functionality for git branch can be enhanced by specifying only url without having local repo... or any other git command – Neha Jun 26 '18 at 11:06
4

This cannot be done without making a clone. It's as simple as that: the hash IDs you get from git ls-remote uniquely identify commits, but unless you have the commits that come before those commits, the only way to tell if some refs/heads/branchname is an ancestor of refs/heads/master is if they have the exact same hash ID. That only suffices for a branch that was just merged.

Note that some servers provide their own (non-Git) interfaces, and at least one of those (GitHub) shows you that some branch is "merged" (and can therefore be deleted). This is not something you can access using git ls-remote.

torek
  • 448,244
  • 59
  • 642
  • 775
  • Could you please help me with some hints about such interfaces? I am beginner to git – Neha Jun 27 '18 at 09:57
  • The only one I've used is GitHub. GitHub shows a purple "merged" icon on the web page if the branch in question is merged to master. But you'd be much better served by just making a clone: that's how Git is intended to be used. – torek Jun 27 '18 at 16:46
  • Ok thanks for your answer. Ultimately I wanted to automate my script where I can clean branches. As of now I can work on clone and see if I can get better option in future :) – Neha Jul 03 '18 at 04:32
  • Ok Sorry.. I tried following command: git branch --remote --merged develop/master |grep 'feature/*' |grep -v '>' |grep -v master |grep -v develop |xargs -L1 |cut -d"/" -f2-|xargs git push origin --delete But I can see branches which has not been merged to develop are also getting deleted. For example some feature branches which was merged to develop and some feature open which are open... All are getting deleted. Can someone help me with more details? – Neha Jul 04 '18 at 09:02
  • I mean I have scenario where I dont have any PR created on that branch. Its a empty branch. – Neha Jul 04 '18 at 09:38
  • @Neha: this is a new question, about hwo to use `git branch --remote --merged` to find merged branches and send delete requests to the server. You can ask it as a new question, but I'd recommend doing a StackOverflow search first, as there are already questions of this form. – torek Jul 04 '18 at 15:13
  • Regarding "not possible", doesn't `git merge-base --is-ancestor` play here? I think it's been around over 7 years, so maybe I misunderstand merge-base or this answer. https://stackoverflow.com/a/53852911 – shannon Oct 27 '21 at 23:04
  • 1
    @shannon: `git merge-base --is-ancestor` works great ... *on a clone*. The question was: "can this be done without making a clone", and the answer to that is still "no". – torek Oct 27 '21 at 23:05