162

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
gCoder12
  • 1,643
  • 2
  • 10
  • 5
  • Perhaps using API. So far no option to search code in specific branch. See [here](https://stackoverflow.com/questions/30221643/how-do-i-search-for-specific-code-in-a-specific-branch-in-a-github-repository) – AlikElzin-kilaka Oct 09 '18 at 09:53

5 Answers5

122

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).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 2
    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. WIN – cgmckeever May 01 '16 at 16:31
  • As @cgmckeever 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" – jmorris0x0 Jun 23 '16 at 19:47
  • actually, if you fork, and then push your branch into a new repo, it is, as that new repo gets indexes – cgmckeever Jun 24 '16 at 19:52
  • @cgmckeever 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) – VonC Jun 24 '16 at 19:54
  • GitHub does not index `master`, it does indexation on the **DEFAULT** branch which could be a release branch behind master/devel branch... – Mizux Feb 07 '19 at 09:29
  • @Mizux Thank you. I have edited the answer accordingly. Do you have any reference documenting that policy? – VonC Feb 07 '19 at 11:31
  • 1
    just my own experiment when we change the default branch here https://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 branch – Mizux Feb 07 '19 at 20:16
28

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)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
dhilt
  • 18,707
  • 8
  • 70
  • 85
18

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 VScode 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.

victor
  • 217
  • 2
  • 5
7

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

Nisarg Shah
  • 14,151
  • 6
  • 34
  • 55
Simon_Weaver
  • 140,023
  • 84
  • 646
  • 689
1

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.

tinystone
  • 369
  • 3
  • 6