Is there a script to list git branches created by me?

GitBranchCode Cleanup

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

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.

Solution 2 - Git

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.

Solution 3 - Git

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"

Solution 4 - Git

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

Solution 5 - Git

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

Solution 6 - Git

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionhawkeyeView Question on Stackoverflow
Solution 1 - GitSachinSunnyView Answer on Stackoverflow
Solution 2 - GitSkillzoreView Answer on Stackoverflow
Solution 3 - GitYunhuaView Answer on Stackoverflow
Solution 4 - GitEricView Answer on Stackoverflow
Solution 5 - GitPaulo FidalgoView Answer on Stackoverflow
Solution 6 - GitmathpaquetteView Answer on Stackoverflow