Webthusiast
Webthusiast

Reputation: 1026

How to git-log all branches that contain a specific commit

git log accepts multiple branch names as parameters, and using --graph you get a nice tree-view of it. With --all I see the complete tree, with --branches I can even filter branch names.

I want to show the complete history of all (sub)branches of a specific branch. To that end, I want to know how can I git log all branches that contain some commit?

With git branch --contains, I can find all branches that contain a specific commit. So with some bash-wizardry, I could probably make up a command that does what I want. But I'm guessing that git should have a more direct way to get such a log.

Upvotes: 3

Views: 157

Answers (1)

Guildenstern
Guildenstern

Reputation: 3841

git-for-each-ref(1) is the plumbing [1] power tool for finding refs (like branches) which have some property, like which ones contain a branch or a commit.

git for-each-ref --contains=name --format='%(refname)' refs/heads/

That gives us the branches. Now we can feed it to git-log(1):

git for-each-ref --contains=name --format='%(refname)' refs/heads/ \
    | git log --stdin

That will of course give a lot of commits. We can confirm that we are only traversing those branches by checking with --no-walk since that turns off revision walking (finding all reachable commits):

... | git log --no-walk --stdin

The different parts

  1. Find out if git-log(1) supports what you are after by looking at the “Commit Limiting” section in the man page; if not then
  2. Use a plumbing tool to give you the revisions or the refs; then
  3. Pipe them to git-log(1) and end the git-log(1) invocation with --stdin
    • We could also do git log $(command) but with --stdin we avoid potential too-many-arguments errors
  4. git-rev-list(1) is the plumbing equivalent of git-log(1) which might be more appropriate if we simply want a list of commits for further processing. In this case: give me the list of commits which you get from traversing all branches which contains the given branch or commit
  5. Keep in mind that you can use options like --no-walk if you want to list only the given commits (this is just an example)

Avoiding Bash wizardry/complex pipelines

Modern Git plumbing tools sometimes provide us with simple enough output to construct simple pipelines (i.e. no coreutils intermediary steps) to achieve our task in a few straighforward steps.

Notes

  1. Plumbing commands in this context are git(1) tools that have (1) stable output and (2) are low-level enough to give parseable output for scripting

Upvotes: 0

Related Questions