Find a Git branch containing changes to a given file

I have 57 local branches. I know I made a change to a certain file in one of them, but I’m not sure which one. Is there some kind of command I can run to find which branches contain changes to a certain file?

  • How to merge code from branch1 to branch2 but excluding some commits
  • How do I preserve the timestamp if I split a commit via git rebase --interactive?
  • What is the git equivalent of Mercurial revsets?
  • Can we reclone a git repository from the existing local repository
  • How to use git (on windows) to contribute code to svn?
  • Git Post Catastrophe Team Workflow with Sencha Touch and Cordova
  • Can I sync a repo I've forked on GitHub without creating a commit in the parent?
  • JGit: Push to specific branch
  • Keep a git feature branch up to date with master branch by pulling directly from the master
  • How to change the git directory delimiter?
  • fix broken git history after merging other repo into subdir
  • Github commits using SSH key
  • 3 Solutions collect form web for “Find a Git branch containing changes to a given file”

    Find all branches which contain a change to FILENAME (even if before the (non-recorded) branch point)

    git log --all --format=%H $FILENAME | while read f; do git branch --contains $f; done | sort -u

    Manually inspect:

    gitk --all --date-order -- $FILENAME

    Find all changes to FILENAME not merged to master:

    git for-each-ref --format="%(refname:short)" refs/heads | grep -v master | while read br; do git cherry master $br | while read x h; do if [ "`git log -n 1 --format=%H $h -- $FILENAME`" = "$h" ]; then echo $br; fi; done; done | sort -u

    All you need is

    git log --all -- path/to/file/filename 

    If you want to know the branch right away you can also

    git log --all --format=%5 -- path/to/file/filename | xargs -I{} -n 1 echo {} found in && git branch --contains {}

    Further, if you had any renames, you may want to include --follow for the git log command.

    hope this helps.

    This is an inelegant brute-force method but I expect it should work. Make sure you’ve stashed any uncommitted changes first as it will switch which branch you are currently on.

    for branch in $(git for-each-ref --format="%(refname:short)" refs/heads); do
        git checkout $branch && git grep SOMETHING
    Git Baby is a git and github fan, let's start git clone.