We have a rather large git repo with many developers and I've been asked to investigate how a particular change got into our main branch. Because branches are just references to commits, and those references can move over time, I can't figure out how to determine what branch a developer was on when he/she made a particular commit. Given that commits don't "belong" to any particular branch, how might this be done? Is it even possible?
Asked
Active
Viewed 55 times
0
-
3It will ultimately depend on your workflow, as, like you already mentioned, git itself does not store any branch metadata. Some workflows include the use of local hooks to automatically prepend branch name to commit messages. Could be *one* way to have your info, but that's not retroactive, you won't be able to read this back in time. – Romain Valeri Dec 08 '21 at 16:22
-
1Do you suspect the branch still exists? And, why do you want to know this- does the branch name somehow help you? Side note, if you use PR's, your SCM tool may be able to look at the history of past completed PR's to find the one that brought it in, and it usually will tell you the branch name at the time. – TTT Dec 08 '21 at 17:50
-
1Why do you care? Suppose I made the commit on a branch I named `frink`, then I renamed it to `monty`. What's the difference between this commit and then the one I made later directly on branch name `flanders` which I then renamed `frink` which is the one you saw? – torek Dec 09 '21 at 00:50
-
I care because I need to know what branch developers where working on when they made a specific commit. There's lots of branching and merging going on and there are times when we need to determine how a specific change ended up in the main branch. We can investigate and find out what commit introduced a change but have no idea what branch it was on at the time. This turns the whole branching history into a useless commit graph that can't be meaningfully analyzed. It seems like this should be fundamental to any source control system. – Mike Collins Dec 09 '21 at 15:58
-
Does this answer your question? [Finding what branch a Git commit came from](https://stackoverflow.com/questions/2706797/finding-what-branch-a-git-commit-came-from) – Yosef-at-Panaya Dec 09 '21 at 20:26
1 Answers
0
You can list all the branches that contain a particular commit via git branch --contains <commit>
. If it's an older commit, this may not always be useful, as any branches that have diverged from the branch where the commit was originally made will also contain it. Here is an example of the usage of this command:
Another thing you can also do is you can list all the merge commits made since that commit:
You'll probably be able to get an idea as to where the commit came from by comparing the branches this commit is on, and the merges made into main since this commit

Alecto Irene Perez
- 10,321
- 23
- 46
-
This is helpful but still doesn't show me the complete branch history. I need to answer questions like "Where was the main branch on
? – Mike Collins Dec 09 '21 at 19:09 -
Oh. That's different from what you asked. You can do that with git log by filtering for specific dates. For example, to get all commits made on October 9th, you can run `git log --until 10/9/2021` and that'll list commits starting with October 9th – Alecto Irene Perez Dec 09 '21 at 23:25