225

I want to find out who created a branch.

I am sort of able to do so with:

git branch -a | xargs -L 1 bash -c 'echo "$1 `git log --pretty=format:"%H %an" $1^..$1`"' _

However, this returns the last committer per branch, not necessarily the person who created the branch.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ofir Farchy
  • 7,657
  • 7
  • 38
  • 58
  • 13
    You can't get a branch author / creator in git. What you are doing here is get the author of the branch's tip. It will change as soon as someone pushes a new commit there. – sylvain.joyeux Aug 21 '12 at 13:01
  • 2
    Disclaimer : The implicit assumption in this question/answer is "in a workflow with **only one person per branch**". In other workflows where at least two coworkers share a branch, this information (branch creator) is not obtainable through git, and the answers above could lead someone into false assumptions. – Romain Valeri Jun 12 '19 at 19:40

13 Answers13

380

List remote Git branches by author sorted by committer date:

git for-each-ref --format='%(committerdate) %09 %(authorname) %09 %(refname)' --sort=committerdate
Marek R
  • 32,568
  • 6
  • 55
  • 140
DarVar
  • 16,882
  • 29
  • 97
  • 146
  • upvoted, as this worked fine for what I needed to know regarding branch creation. – hoffmanc Nov 05 '13 at 17:27
  • 1
    Does this sort ascending or descending? Is the author shown the creator or the last committer? – user1021726 Dec 16 '14 at 09:18
  • (cant edit after 5 mins) What I meant was if it's the branch creator that is shown or if its just the last committer – user1021726 Dec 16 '14 at 09:35
  • 24
    It's the last person to commit to that branch. Since a branch is just a pointer to a commit. – DarVar Jan 13 '15 at 16:00
  • 1
    but this prints tags too. – Ciasto piekarz Feb 24 '15 at 05:03
  • 7
    Should this work on Windows? I got "Input file specified two times." – Steve Chambers Mar 25 '15 at 11:32
  • 31
    With tabular formatting: `git for-each-ref --format='%(committerdate)%09%(authorname)%09%(refname)' | sort -k5n -k2M -k3n -k4n | grep remotes | awk -F "\t" '{ printf "%-32s %-27s %s\n", $1, $2, $3 }'` (got idea from http://stackoverflow.com/a/3631819 - may need to adjust numbers in the awk printf depending on max datetime/author name lengths) – Gary S. Weaver Apr 02 '15 at 13:38
  • 2
    add **| grep {{branch_name}}** to get author for _particular branch_ `git for-each-ref --format='%(committerdate) %09 %(authorname) %09 %(refname)' | grep -E {{branch_name}}` – Rajdeep Siddhapura Jul 24 '15 at 09:27
  • 22
    In case there has not been any commit on the branch and if it has been created from master, the "author of the branch" is completely unrelated to that branch but just the last committer on master. If you pull in changes from master and it's a fast-forward, it is again the last committer on master, who possibly never worked on that branch. – Gustave Aug 05 '15 at 15:55
  • If you also want the HEAD commit hash of each branch: `git for-each-ref --format='%(committerdate)%09%(authorname)%09%(objectname:short)%09%(refname)' | sort -k5n -k2M -k3n -k4n | grep --color=never remotes | perl -lwpe 's{refs/remotes/origin/}{}' | awk -F "\t" '{ printf "%-32s %-22s %-12s %s\n", $1, $2, $3, $4 }'` I also removed `refs/remotes/origin` from each line since that part is always the same (in my workflow anyway). – Noah Sussman Jan 27 '16 at 22:58
  • 5
    Nowadays there is a `--sort` option, so you can do this even shorter `git for-each-ref --format='%(committerdate) %09 %(authorname) %09 %(refname)' --sort=committerdate` – asmaier Aug 15 '16 at 10:14
  • 3
    I get the message "Input file specified two times" when I enter that command (on Windows 10) . . . – blaster Sep 19 '16 at 16:00
  • It works! You go further by using Notepad++, replace specific word(s) by 0 character for parsing plain text. – Vy Do Jan 10 '17 at 03:04
  • is there a way to put this in a git alias? I am getting syntax errors – BigBrownBear00 Jan 03 '18 at 16:51
  • 29
    While this answer and command is potentially useful, I feel it's a misleading answer to the question. This lists author names on all branches who are the last commiter on a branch. There are a *number* of circumstances under which that wouldn't actually be the *creator* of the branch. The real answer is that git simply does not store this information. – peabody Feb 05 '18 at 23:15
  • And since the OP asked how to find the creator of a single branch, here's an example of that: `git for-each-ref --format='%(committerdate) %09 %(authorname) %09 %(refname)' | sort -k5n -k2M -k3n -k4n | grep 'origin/MY_BRANCH_NAME'` – Mass Dot Net Jul 02 '18 at 14:21
  • 2
    @SteveChambers was stumped by the same "Input file specified two times" problem you had, but was able to execute it on the git bash (right click in your git repository folder > Git Bash here) – ZeitPolizei Aug 09 '19 at 10:12
  • 2
    this gives the last committer not the creator – Vikash May 21 '20 at 09:54
  • on windows use double quote `"` like: `git for-each-ref --format="%(committerdate) %09 %(authorname) %09 %(refname)" --sort=committerdate` – Alireza Fattahi Nov 13 '21 at 13:52
  • Tabular format: git for-each-ref --format='%(authordate) | %(creator) | %(refname)' --sort=-authordate | column -t -s '|' -o '|' – Ninad Feb 08 '23 at 13:59
79

A branch is nothing but a commit pointer. As such, it doesn't track metadata like "who created me." See for yourself. Try cat .git/refs/heads/<branch> in your repository.

That written, if you're really into tracking this information in your repository, check out branch descriptions. They allow you to attach arbitrary metadata to branches, locally at least.

Also DarVar's answer below is a very clever way to get at this information.

Community
  • 1
  • 1
Christopher
  • 42,720
  • 11
  • 81
  • 99
  • For me this was in .git/refs/heads/ "ref" was plural – Jeffrey LeCours Nov 06 '14 at 14:28
  • 11
    DarVar's answer, while probably useful to some, is not an accurate answer. It is simply a more succinct way of doing what the author of the original question was already doing (namely getting the name of the last commiter to a branch). – peabody Feb 05 '18 at 23:18
  • It is true that a branch is only pointer but it's not strictly true that it doesn't track who created it. Of course it does! Just not the head of the branch (unless the branch is only one commit). To find out who created the branch, find the author of the "first" commit. Finding the "first" commit is what is in some cases non-trivial since a branch doesn't keep track of which commit was "first". The first commit is simply the commit which has `git merge-base $BRANCH $BRANCHED` as parent. Where `$BRANCH` is the branch and `$BRANCHED` the branch of what it was branched from. – CervEd Sep 27 '21 at 07:33
  • 1
    no? The first commit does not need to be the person that created the branch. those are entirely separate things. Neither can you really find out what the "first" commit was as the branch might very well have several connections to its parent-branch. – ABaumstumpf Jun 27 '22 at 11:12
  • @CervEd you are incorrect. I can create thousand of branches to all existing commits in a repository - I created the branches, but I haven't touched the commits. – Moberg Aug 19 '22 at 06:02
  • @Moberg in such a situation there's no "first" commit – CervEd Aug 19 '22 at 16:13
  • @CervEd Exactly, that's my point. – Moberg Sep 07 '22 at 05:33
  • @Moberg right, so if there's no commit on a branch that is distinct, it's not really a branch in any sense where one would care about who "created" it – CervEd Sep 07 '22 at 08:05
65

I tweaked the previous answers by using the --sort flag and added some color/formatting:

git for-each-ref --format='%(color:cyan)%(authordate:format:%m/%d/%Y %I:%M %p)    %(align:25,left)%(color:yellow)%(authorname)%(end) %(color:reset)%(refname:strip=3)' --sort=authordate refs/remotes
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mike
  • 1,495
  • 15
  • 11
10

Adding to DarVar's answer:

git for-each-ref --format='%(committerdate) %09 %(authorname) %09 %(refname)' | sort -k5n -k2M -k3n -k4n | awk '{print $7 $8}'

P.S.: We used AWK to pretty print the author and the remote branch.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
suryakrupa
  • 3,852
  • 1
  • 25
  • 34
  • 3
    that only works if the author name is a single word. If it is first and last names separated, then you'll see the last name instead of the branch name... – Legato Mar 03 '16 at 12:59
  • Just remove `%(committerdate)` and you won't need anything else, and as bonus you won't have to deal with multiple-word author names: `git for-each-ref --format='%(authorname) %09 %(refname)'` – TerDale Oct 13 '22 at 15:49
  • I puzzled over the use of awk for a while and then realized the easiest thing to do is to replace it with "cut -d' ' -f7-". – Pete Oct 31 '22 at 16:58
8

You can find out who created a branch in your local repository by

git reflog --format=full

Example output:

commit e1dd940
Reflog: HEAD@{0} (a <a@none>)
Reflog message: checkout: moving from master to b2
Author: b <b.none>
Commit: b <b.none>
(...)

But this is probably useless as typically on your local repository only you create branches.

The information is stored at ./.git/logs/refs/heads/branch. Example content:

0000000000000000000000000000000000000000 e1dd9409c4ba60c28ad9e7e8a4b4c5ed783ba69b a <a@none> 1438788420 +0200   branch: Created from HEAD

The last commit in this example was from user "b" while the branch "b2" was created by user "a". If you change your username you can verify that git reflog takes the information from the log and does not use the local user.

I don't know about any possibility to transmit that local log information to a central repository.

Gustave
  • 3,359
  • 4
  • 31
  • 64
8
git for-each-ref --format='%(authorname) %09 -%(refname)' | sort
KhaledMohamedP
  • 5,000
  • 3
  • 28
  • 26
6

We can find out based upon authorname

git for-each-ref --format='%(authorname) %09 %(if)%(HEAD)%(then)*%(else)%(refname:short)%(end) %09 %(creatordate)' refs/remotes/ --sort=authorname DESC
Vikdor
  • 23,934
  • 10
  • 61
  • 84
tech2504
  • 947
  • 4
  • 19
  • 34
  • `git for-each-ref --format='%(authorname),%(creatordate:short),%(if)%(HEAD)%(then)*%(else)%(refname:short)%(end)' refs/remotes/ --sort=creatordate DESC > ../.csv` this helped me... – Parag Bangad Apr 22 '21 at 18:54
5

Assuming:

  1. branch was made from master
  2. hasn't been merged to master yet

 git log --format="%ae %an" master..<HERE_COMES_THE_BRANCH_NAME> | tail -1
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Trident D'Gao
  • 18,973
  • 19
  • 95
  • 159
1

I know this is not entirely the scope of the question, but if you find the need to filter only commits by a specific author, you can always pipe to grep :)

# lists all commits in chronological order that
# belong to the github account with
# username `MY_GITHUB_USERNAME` (obviously you
# would want to replace that with your github username,
# or the username you are trying to filter by)


git for-each-ref --format='%(committerdate) %09 %(authorname) %09 %(refname)' | sort -committerdate | grep 'MY_GITHUB_USERNAME'

happy coding! :)

avocadojesus
  • 231
  • 2
  • 10
1

Warning!

Note that these commands show the commit data where the branch is pointing, and so the commit author, that could be different than the branch author. Git doesn't store the branch creator, just the commit's one.

Viker
  • 3,183
  • 2
  • 25
  • 25
1

If you are using Azure Devops, it is possible to retrieve this information using the REST API :

$baseAzdoURI/_apis/git/repositories/$repositoryName/refs

Inside the returned json array, the creator name will be inside the [collectionElement].Creator.DisplayName

ex.

        {
        "name": "refs/heads/branchname",
        "objectId": "GUID",
        "creator": {
            "displayName": **"John Deere"**,
            "url": "$baseAzdoURI/_apis/Identities/GUID",
            "_links": {
                "avatar": {
                    "href": "$baseAzdoURI/_apis/GraphProfile/MemberAvatars/BASE64STUFF"
                }
            },
            "id": "guid",
            "uniqueName": "DOMAIN\\USERNAME",
            "imageUrl": "$baseAzdoURI/_api/_common/identityImage?id=guid",
            "descriptor": "BASE64STUFF"
        },
        "url": "$baseAzdoURI/GUID/_apis/git/repositories/GUID/refs?filter=heads%2FBRANCHNAME"
    },

ref: https://learn.microsoft.com/en-us/rest/api/azure/devops/git/refs/list?view=azure-devops-rest-7.0

P-L
  • 523
  • 4
  • 14
  • May I know how are you doing the setup for this service? I tried OAuth2.0 however with the authorization url, it shows `A potentially dangerous Request.Path value was detected from the client (&).` – Tommy Leong Jul 12 '23 at 09:00
  • Hello Tommy, I am using an AzDO PAT and creating a basic authorization header, ie. @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($azdoPAT)")) } Not sure if that answer your question, let me know if thats not the case :) – P-L Jul 12 '23 at 13:03
0

As far as I know, you may see if you are the creator of a branch only. This is indicated by the first row in .git/ref/heads/<branch>. If it ends with "Created from HEAD" you are the creator.

Kendor
  • 11
0

for those looking for a DESC ... this seems to work --sort=-

ty for the formatting, new to this ...my eyes are loosing some of it's bloodshot

git for-each-ref --format='%(color:cyan)%(authordate:format:%m/%d/%Y %I:%M %p)    %(align:25,left)%(color:yellow)%(authorname)%(end) %(color:reset)%(refname:strip=3)' --sort=-authordate refs/remotes

further ref: https://stackoverflow.com/a/5188364/10643471