104

I'm aware that branches don't really store creator information - and they're just a pointer to a commit.

My goal is to be able to clean out my old branches that have been merged back to the main branch, and list branches where this hasn't been done either. (A clean up).

This is different to "finding unmerged branches" because I want to find merged branches as well, and I want to do it by author.

My question is: Is there a script to list git branches created by me?

hawkeye
  • 34,745
  • 30
  • 150
  • 304
  • The title and body of your question seem to be contradicting: Do you want to get branches created by you (which I don't think you can) or do you want to list unmerged branches? Or a combination of both? – morxa Mar 16 '16 at 03:56
  • Possible duplicate of [git finding unmerged branches](http://stackoverflow.com/questions/12276001/git-finding-unmerged-branches) – gzh Mar 16 '16 at 04:09
  • I want both. I want all branches created by me - merged or unmerged. – hawkeye Mar 16 '16 at 04:50
  • 1
    There is no information about who created any branch in git. Period. So short answer is **No, there is none.** – Romain Valeri Apr 04 '22 at 13:20
  • I assume the question is perhaps more about finding branches that you've contributed to. As in, there is a commit authored by you on that branch, since the branch point. – Daniel Stevens Mar 02 '23 at 20:10

8 Answers8

126

This command lists all branches and their author names

git for-each-ref --format=' %(authorname) %09 %(refname)' --sort=authorname 

If you are using github you can also visit https://github.com/author/repo/branches/yours to get all your branches

If you want to just delete all the already merged branches you can us the command

git branch --merged | grep -v "\*" | grep -v master | grep -v dev | xargs -n 1 git branch -d

For more details of git for-each-ref visit here.

SachinSunny
  • 1,681
  • 1
  • 12
  • 20
  • 3
    Is there a better way other than piping through grep to restrict it to just mine? – hawkeye Mar 16 '16 at 04:50
  • 4
    This does not answer the question. The OP explicitly states the question: "My question is: Is there a script to list git branches created by me?" This answer is not sufficient – Caleb Adams Jul 07 '17 at 13:15
  • If you add `--sort=authorname` to the above command you can get your branches grouped together. Otherwise I am not sure if you can list the branches only you created. – SachinSunny Jul 13 '17 at 09:14
  • 6
    This lists branches, but not by their authors. They are sorted by the author of last commit to every branch, not the branch author. – Janez Lukan Mar 02 '18 at 07:40
  • 2
    Nothing was showing for me, so I changed it to: `git for-each-ref --format="%(if)%(authorname)%(then)%(authorname)%(end): %(refname)" --sort=authorname` idea came from https://git-scm.com/docs/git-for-each-ref – JohnnyFun Sep 04 '20 at 18:30
  • git for-each-ref --format=' %(authorname) %09 %(refname)' --sort=authorname | grep 'give user name here' to get user name git config user.name. – Ashok R Jun 08 '21 at 03:00
  • Can you post a working example of the `https://github.com/author/repo/branches/yours` link? – bobobobo Jul 27 '21 at 20:44
  • Use the first snippet, and pipe to `column -t -s $'\t'`, then you can use your terminal's block select feature to grab the list of branch names. Then I can use that list (which will generally be only the local branches which I own) for whatever purpose (such as getting a git --graph) – Steven Lu Feb 17 '22 at 06:39
  • 1
    Pipe the results to grep to filter out the other rows `git for-each-ref --format=' %(authorname) %09 %(refname)' --sort=authorname | grep Andrew` – Andrew Aug 25 '22 at 12:55
  • What is the meaning of `%09` here? Looks like a magic number. – Shiplu Mokaddim May 24 '23 at 18:48
37

A bit late to the party here, but this is the top result on google when searching for "list git branch for author".

A one-liner to find your remote branches in git is:

git branch -r | xargs -L1 git --no-pager show -s --oneline --author="$(git config user.name)"

git branch -r - lists all remote branches.

xargs -L1 - convertes the result of the previous command into arguments for the next command.

git show - git show, shows various kinds of objects in git. In this case it will show the latest commit on each branch, with full diff.

The flag -s suppresses diff output.

The flag --oneline condenses the commit info into one line.

The flag --author="$(git config user.name)" only shows the commits for the current git user.

The flag --no-pager runs git without a pager. Without this flag each commit of the result will be opened in it's own pager instance, which might work depending on your pager.

The final result will look something like this:

efbd5d738b (origin/feature/A) commit feature A
297e83d9a6 (origin/feature/B) commit feature B
951f6638de (origin/test/A) commit test A
307d16741b (origin/master) latest master commit

Be aware that the list will contain master and develop branches if the given author is the latest commiter on these branches.

Skillzore
  • 748
  • 7
  • 21
  • Interestingly, this is very slow. I am assuming because there is actual communication with the remote. I would expect this to be lightning fast as long as you do a pull for the entire repository, wouldn't all of the necessary data be local? – gravidThoughts Feb 11 '20 at 17:40
  • 1
    Trying to look into the slow performance. Seems that it is the `--author` and the `-s` flags that are the main culprits. Without these flags the script is responsive, but the result takes a while to print out on bigger repos. Can't really find anything online to suggest why they are slow or any speedier alternative. Will look into maybe reporting the speed of these flags with git maintainers somewhere. The feeling I get is that they only suppress some output. They still seem to do all the processing for all branches and all diffs in the background. – Skillzore Feb 17 '20 at 08:30
  • My output included an error message for the checked-out branch on origin: `fatal: unrecognized argument: ->`. This was because `git branch -r` printed out `origin/HEAD -> origin/master` for that branch. I suppose you could avoid that by piping through `grep -v -- '->'`. – Noumenon Jul 09 '20 at 14:32
  • 3
    @Noumenon Me too. Fixed it with by adding format to the first term: `git branch -r --format='%(refname)' | ...` – Rhubarb Aug 11 '20 at 18:02
  • As for the most upvoted answer, this is not what OP wants. Btw, what OP wants is unobtainable by design in git. – Romain Valeri Apr 04 '22 at 13:18
15

Tried the answer from "SachinSunny" above, modified it to be

git for-each-ref --sort=authorname --format "%(authorname) %(refname)"

This gives the author and branch, both local and remote branches, ordered by "last author"

Yunhua
  • 151
  • 1
  • 4
5

As a slight condensation of SachinSunny's answer above, you can use the regular expression features of grep to accomplish a simpler command:

git branch --merged | grep -Pv "\*|master|dev" | xargs -n 1 git branch -d
Eric
  • 230
  • 3
  • 9
3

As for Git version 2.23 you can just type:

git branch

and it will give you the list the local list of branches. Remember that will also include master and others that you have merged. To filter these pipe the result with egrep -v "(^\*|master|development)

If you want to include the remotes you can do: git for-each-ref --format=' %(authorname) %09 %(refname)' --sort=authorname | egrep -v "(^\*|master|development)"| grep 'Your Name'

for deletion, pipe xargs git branch -D

Paulo Fidalgo
  • 21,709
  • 7
  • 99
  • 115
1

Pretty much the same command as the accepted answer but just more convenient with | while read

git for-each-ref --format='%(authorname),%(refname:lstrip=3),%(refname:short)' --sort=authorname | while IFS=',' read author branch remote_branch; do
  printf "%20s | %40s | %s\n" "$author" "$branch" "$remote_branch"
done

Output: enter image description here

mathpaquette
  • 406
  • 4
  • 16
0

This command useful for showing current branches

git ls-remote --heads origin

enter image description here This command is useful for getting an overview of the branches available in the remote repository without needing to clone the entire repository locally

-1

This will list current branches MERGED and UNMERGED but NOT necessarily created ONLY by you (which is not quite what you want but it can be useful esp if you know that there is only one branch not created by you - or else you can filter them out - so I'm posting it anyway), one branch per line, excluding master and main as well as (because it puts each one on a line) the current marked branch (*) (that is the indicator: it will print the branch itself):

$ git branch |tr -s ' '|sed -e 's/^ //g' -e 's/ /\n/g' |tr ' ' '\n'|grep -vE '^(master|main|\*)$'

To create a bash alias you might have:

$ alias git-ls-branches="git branch |tr -s ' '|sed -e 's/^ //g' -e 's/ /\n/g' |tr ' ' '\n'|grep -vE '^(master|main|\*)$'"

which you could also put in your .bashrc or equivalent file.

Again this will LIST BRANCHES CREATED BY OTHERS TOO but if you know there are no other branches you should be good ... sans any typos I might have made.

Pryftan
  • 192
  • 1
  • 6