Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find a Git branch containing changes to a given file

Tags:

git

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?

like image 777
Dustin Avatar asked Jun 06 '11 21:06

Dustin


People also ask

What is GITK command?

Usage. Gitk is invoked similarly to git log . Executing the gitk command will launch the Gitk UI which will look similar to the following: The upper left pane displays the commits to the repository, with the latest on top. The lower right displays the list of files impacted by the selected commit.


2 Answers

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

FILENAME="<filename>" 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 
like image 69
Seth Robertson Avatar answered Sep 21 '22 21:09

Seth Robertson


All you need is

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

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

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.

like image 41
Adam Dymitruk Avatar answered Sep 18 '22 21:09

Adam Dymitruk