Searching code in a specific GitHub branch
Asked Answered
C

5

183

I am trying to search some code in a branch in a GitHub repository.

However this indicates that I can only search the default branch.

Is there a way I can search code in non-default branch in my GitHub repository?

Cubature answered 8/8, 2015 at 9:36 Comment(1)
Perhaps using API. So far no option to search code in specific branch. See hereDoody
L
132

Not from the GitHub web interface itself, as mentioned in "How can I search for a commit message on GitHub?": only the default branch (generally master) is indexed.

Your best bet is to clone the repository, and there, search in all branches (with git log -S for instance).

Lashaunda answered 8/8, 2015 at 9:40 Comment(7)
Ive been needing this for a while, and this post gave me a lightbulb. Fork the code, in the settings of your repo, set the branch to what you want. WINTinstone
As @Tinstone has no doubt discovered, you can not search a forked repo, even if you set the default branch to the branch you want to search. "Sorry, forked repositories are not currently searchable"Doubleripper
actually, if you fork, and then push your branch into a new repo, it is, as that new repo gets indexesTinstone
@Tinstone fork in on the server side (GitHub). You don't need to fork. You need to clone, and then push to a new empty remote GitHub repo. Then yes, that new repo will be indexed (only its master branch though)Lashaunda
GitHub does not index master, it does indexation on the DEFAULT branch which could be a release branch behind master/devel branch...Retrieval
@Retrieval Thank you. I have edited the answer accordingly. Do you have any reference documenting that policy?Lashaunda
just my own experiment when we change the default branch here github.com/google/or-tools since we use master as a devel branch but we want user to see last stable release... some API are on master not on default (stable branch) and the contrary, search only found api symbols in stable branchRetrieval
D
48

If you want to search in a specific branch, first set the branch by selecting it on the project main page. Then, hit the period (.) key on your keyboard. This will open up the project in an online VS Code editor. From there, you can use the sidebar on the left to search code or view files directly. Preferably using the Search (magnifying glass-icon) functionality to search across the whole project.

More details on GitHub Codespaces and using the web-based editor can be found here.

Doorbell answered 20/2, 2023 at 21:14 Comment(2)
this should be the preferred answer, since the OP obviously wants to search onlineZibeline
Doesn't work for me in a corporate github (url isn't github.com/..., it's github.someCorpName.com)Ovariectomy
W
29

Using console, I would search for specific code in a specific branch with the following

git grep 'my_search_text' my_branch
  • my_branch should be available locally
  • git grep can perform pattern matching of high complexity (see the documentation)
Waterresistant answered 7/1, 2020 at 6:45 Comment(0)
W
8

If you know the filename you can use Find File.

enter image description here

enter image description here

https://github.com/angular/angular/find/6.1.x

Wayland answered 14/8, 2018 at 1:38 Comment(3)
oh boy - they really need to do something about HighDPI screenshots :-/ sorry!Wayland
Not too helpful if we wanted just search a specific file, could already do that in any ide.Shaggy
@Shaggy when I'm looking for something I usually don't have the repo locally (trying to diagnose a bug or something in someone elses code), but yes point taken :-)Wayland
A
7

With a GitHub account, you can use GitHub online editor (github.dev 1, hotkey: open a repo page and press . on the keyboard) to open the branch of a repository and search for your keywords.

💡 Example: how to find "useMeta" keyword in github.com/nuxt/vue-meta next branch?

This keyword is expected to be not in the default master branch, but in next branch. Here is how you can find it in the desired branch:

1 open the repo page and choose the branch. eg https://github.com/nuxt/vue-meta/tree/next

2 start GitHub online editor by press .

don't forget to log in GitHub first.

the online code editor url looks like https://github.dev/nuxt/vue-meta/tree/next

3 enter your keyword "useMeta" in the Search function on the code editor sidebar

4 check the result: this keyword exists in next branch ✅

5 in the repo page, choose master branch, enter online code editor by press ., search for the keyword with Search function on the sidebar, and you see that the keyword is not found in that branch.

Algeria answered 19/7, 2023 at 1:34 Comment(1)
Working exactly as explained.Vulcanize

© 2022 - 2024 — McMap. All rights reserved.