I have been looking for a solution for this since many weeks and it really drives me crazy.
What am I trying?
From an existing git project (it really doesn't matter which one), let's say for example the cpython project (https://github.com/python/cpython), I would like to get the branch name of a commit ID at that time, when it was committed.
Let me try to explain what I want:
Let's say during the development of a git project, the tree looks like this (basic graph):
----------------
o = commit
#xxxx = commit id
----------------
o (#abcf) <-- main
|
|
|
| o (#bcdg) <-- dev
| |
| |
| o (#bcdf) o (#cbcr) <-- featureA
| | |
| | o (#cbce)
| o (#bcde) |
| | o (#cbcd)
| | |
| o-----------─┘
| |
o-----------─┘
|
|
|
o (#abce)
|
|
|
o (#abca) <-- initial commit
So far so good:
We can see, of course with our eyes, at this time of the commits which commit belongs to which branch. For example commit "#bcde" belongs to the "dev" branch the same as for "#bcdf", and "#cbcd" belongs to the branch "featureA".
But the question here is: How do I get the branch name from these commits (#bcde, #bcdf, #cbcd)?
But also especially when the git graph during the development (what often the case is such as in the cpython project), you will find this advanced graph just like this:
o (#adfs) <-- main, dev
|
|
o (#aztr)
|
|
o (#awet) (Merge branch 'dev' into 'main') --─┐
| |
| o (#uisp)
| |
| |
| o (#ulip)
| |
| ┌-----------------------------------------─┘
| |
| /
o (#aukl) --─┐ (Merge branch 'dev' into 'main')
| |
| o (#dter)
| |
| |
| o (#dnmj) (Merge branch 'featureB' into 'dev') ---─┐
| | |
| | |
| o (#dwgf) o (#epof)
| | |
| | |
| o (#dolp) o (#elop)
| | |
| | |
| o (#dujh) o (#egol)
| | |
| | |
| o-------------------------------------------------─┘
| |
o (#abcf) |
| o (#bcbz) (Merge branch 'featureA' into 'dev') ---─┐
| | |
| | o (#cbcz)
| o (#bcdg) |
| | o (#cbcy)
| | |
| o (#bcdf) o (#cbcr)
| | |
| | o (#cbce)
| o (#bcde) |
| | o (#cbcd)
| | |
| o-------------------------------------------------─┘
| |
o-----------─┘
|
|
|
o (#abce)
|
|
|
o (#abca) <-- initial commit
As you can see, some new commits appeared, new merges, new "featureB", but mostly to note, that only the two branches "main" and "dev" can be seen. (Ok, sometimes you will still see the other branch features, but consider both cases where the branch name were deleted and not deleted)
So "#uisp" was in the "dev" branch, same for the branches "#ulip", "#dwgf", "#dolp, "#bcdf" and so on. While "#epof" was in branch "featureB", so for "#elop" and "#egol". And "#cbcz", "#cbcy", "#cbcr" etc. were in branch "featureA".
What do I want?
I would like, no matter if it is the basic graph or the advanced graph, to receive the branch name of a commit X to that time, when it was committed.
Example (let's say the command for achieving this is called 'magic-command'):
> git magic-command uisp
dev
> git magic-command dwgf
dev
> git magic-command elop
featureB
> git magic-command cbce
featureA
Of course now the question here is what branch name you will receive, if you would put the commit id "#aztr", "#adfs", "#dujh", "#dter", since it could be main, dev or even a feature name (or a combined form). Either it shows both or just really only the name "on that branch", I would be happy with both of it, so:
> git magic-command aztr
main (or "main, dev")
> git magic-command adfs
main (or "main, dev")
> git magic-command dujh
dev (or "dev, featureA")
> git magic-command dter
dev (or "dev, featureB", or even "dev, featureA, featureB")
What did I try?
I tried to use git log with its parameters, especially the "%d" in the pretty format, but this doesn't show the branch for each commit.
I also was thinking if the "symbolic-refs" somehow could help inside the ".git/refs" folder, since every commit the refs value should change during time, but I am not quite sure if this is the correct way, but actually I haven't seen any solution to show this ref changes inside a commit.
Is it possible to receive the branch name of a commit X to that time, when it was committed?
I hope you can help me with that problem.