I'm working on one particular branch in my git repo and notice a piece of code that I thought I changed. Perhaps the change is on another branch that hasn't been merged into master
or the current branch on which I am working. How do I search the whole repository, including all branches, for a particular change to one source code file?
Asked
Active
Viewed 371 times
3

Code-Apprentice
- 81,660
- 23
- 145
- 268
-
1There may well be a more elegant solution, but you could just grep the output of `git log --all --stat`? – Oliver Charlesworth Nov 01 '14 at 19:06
1 Answers
6
You can try and use the pickaxe (-S
) or regexp (-G
) options of git log
.
git log --all -Schange -- path/to/change
(replace change by a keyword you know is representing your particular change)
See "How to grep (search) committed code in the git history?"
As I mentioned:
this looks for differences that introduce or remove an instance of
<string>
.
It usually means "revisions where you added or removed line with 'change
'".
Add the --all
in order to search in all branches.
To get the branch(es) those commits are part on, you can use:
git branch --contains SHA1
(as I mentioned in "How to list branches that contain a given commit?")
-
Okay, that's definitely helpful. However, it only shows the commits in the current branch. My primary problem is that the change my not occur on the current branch, if I actually committed it at all. How can I search every branch in my repo? – Code-Apprentice Nov 01 '14 at 19:14
-
@Code-Apprentice that would be the `--all` option. I have edited the answer. – VonC Nov 01 '14 at 19:57
-
One last thing: this gives me the commit message, including the SHA hash. Now how do I get the branch(es) this commit is on? – Code-Apprentice Nov 01 '14 at 20:03
-
@Code-Apprentice I have edited the answer (referencing an older answer of mine: http://stackoverflow.com/a/1419637/6309) – VonC Nov 01 '14 at 20:07
-
Thanks. I tried asking this as a new question, but it was so short that it didn't pass the quality standards filter. – Code-Apprentice Nov 01 '14 at 20:08
-
@Code-Apprentice no problem: that remains in the same context as your original question, so I put the answer in the same place. – VonC Nov 01 '14 at 20:08
-
Finally, how do I make `git log` just give me the hash so I can do this all in one line (i.e. pass the hash directly to `git branch --contains`) – Code-Apprentice Nov 01 '14 at 20:14
-
@Code-Apprentice actually, maybe adding `--decorate` to the `git log` command would be enough to show the branches? (as in http://mislav.uniqpath.com/2010/07/git-tips/ – VonC Nov 01 '14 at 20:23
-
From what I can tell `--decorate` only adds a branch name to the head commit of the branch. – Code-Apprentice Nov 03 '14 at 02:27
-
-
@KansaiRobot `git log --all -Schange -- path/to/change` mentioned in the answer is a concrete example. Other concrete examples: http://www.philandstuff.com/2014/02/09/git-pickaxe.html, https://stackoverflow.com/a/1340245/6309, https://gitfu.wordpress.com/2008/06/03/the-pickaxe-finding-changes-was-never-easier/, https://remireuvekamp.nl/blog/the-git-pickaxe.html – VonC Nov 09 '18 at 07:16