I have a piece of code in another than my current branch in my git repo, and I am not sure which commit and which branch. How can I search in all files that I committed until now for a specific string (and afterwards show the surrounding of that line of code)?
This is also possible using git log . You can perform a search for a string, for example, a variable or method, and git log will give you the commits, adding or deleting the string from the history.
Looking up changes for a specific commit If you have the hash for a commit, you can use the git show command to display the changes for that single commit. The output is identical to each individual commit when using git log -p .
`git grep` command is used to search in the checkout branch and local files.
Use git grep
to locate the commit:
git grep "string" $(git rev-list --all)
The git rev-list --all
makes it search the entire history of the project.
That will give an output like this:
<commit>:<path>:<matched line>
Then you can use git branch --contains
to find out which branch the commit is on:
git branch --contains <commit>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With