Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to list branches that contain a given commit?

How can I query git to find out which branches contain a given commit? gitk will usually list the branches, unless there are too many, in which case it just says "many (38)" or something like that. I need to know the full list, or at least whether certain branches contain the commit.

like image 411
Andrew Arnott Avatar asked Sep 14 '09 04:09

Andrew Arnott


People also ask

How do you find which branch a commit belongs to?

It is based on "Find merge commit which include a specific commit". Find when a commit was merged into one or more branches. Find the merge commit that brought COMMIT into the specified BRANCH(es). Specifically, look for the oldest commit on the first-parent history of BRANCH that contains the COMMIT as an ancestor.

How do you check if branch has commit?

Search the branch (say, feature ) with exact matching. You can also search in both local and remote branches (use -a ) or only in remote branches (use -r ). The grep here could also result in false positives if there are other branches containing the commit whose names also contain <branch-name> as a substring.

How can you display a list of files added in a specific commit?

To reduce the information and show only names of the files which committed, you simply can add --name-only or --name-status flag... These flags just show you the file names which are different from previous commits as you want...


1 Answers

From the git-branch manual page:

 git branch --contains <commit> 

Only list branches which contain the specified commit (HEAD if not specified). Implies --list.


 git branch -r --contains <commit> 

Lists remote tracking branches as well (as mentioned in user3941992's answer below) that is "local branches that have a direct relationship to a remote branch".


As noted by Carl Walsh, this applies only to the default refspec

fetch = +refs/heads/*:refs/remotes/origin/* 

If you need to include other ref namespace (pull request, Gerrit, ...), you need to add that new refspec, and fetch again:

git config --add remote.origin.fetch "+refs/pull/*/head:refs/remotes/origin/pr/*" git fetch git branch -r --contains <commit> 

See also this git ready article.

The --contains tag will figure out if a certain commit has been brought in yet into your branch. Perhaps you’ve got a commit SHA from a patch you thought you had applied, or you just want to check if commit for your favorite open source project that reduces memory usage by 75% is in yet.

$ git log -1 tests commit d590f2ac0635ec0053c4a7377bd929943d475297 Author: Nick Quaranto <[email protected]> Date:   Wed Apr 1 20:38:59 2009 -0400      Green all around, finally.  $ git branch --contains d590f2   tests * master 

Note: if the commit is on a remote tracking branch, add the -a option.
(as MichielB comments below)

git branch -a --contains <commit> 

MatrixFrog comments that it only shows which branches contain that exact commit.
If you want to know which branches contain an "equivalent" commit (i.e. which branches have cherry-picked that commit) that's git cherry:

Because git cherry compares the changeset rather than the commit id (sha1), you can use git cherry to find out if a commit you made locally has been applied <upstream> under a different commit id.
For example, this will happen if you’re feeding patches <upstream> via email rather than pushing or pulling commits directly.

           __*__*__*__*__> <upstream>           / fork-point           \__+__+__-__+__+__-__+__> <head> 

(Here, the commits marked '-' wouldn't show up with git cherry, meaning they are already present in <upstream>.)

like image 182
VonC Avatar answered Sep 23 '22 10:09

VonC