Git is robust and provides a flexible branching model; it allows you to create as many branches as you like and search in all of your Git branches and entire repo. You can search for a commit or any particular string.
In this shot, you will learn two cool Git tips:
So, let’s dive in.
The very first question is, “Which tool do you need to accomplish this?”
In short - Zero
For searching in Git, you don’t need any external software or plugins. There are many powerful Git commands that you can use to explore with your repo.
Searching in your Git branches with a commit hash is an easy one.
This kind of search is useful in many cases, like when you want to make sure the vital commit is your main or master Git branch.
This search also confirms if you have merged your branch with the main brach.
Here is the Git command:
git branch --contains <commit>
Once you execute the command, it will list all the branch names in which this commit exists.
Many times you will want to search for a particular string, so this command will search in your Git repo (in all of your branches):
git rev-list --all | xargs git grep -F '<search_string>'
Instead of <search_string>
, you can use your search query. For example, I am searching for the word “password:”
git rev-list --all | xargs git grep -F 'password'
It will now search in all the git branches for the word “password” and display the console results.
Searching in Git is powerful --you can search anywhere in your Git repo. In this shot, you have learned two ways to search in Git repo: with commit hash and with free text. Both ways have their own advantages and use cases. I hope you have enjoyed this shot.