normski
normski

Reputation: 1647

GitHub: searching through older versions of files

I know that using GitHub I can search through all the current versions of my files in a repo. However, I would also like to search through the older versions of my repo files. For example, say, I used to have a function called get_info() in my code, but deleted it several versions ago, is it possible to search for get_info and find the code. If it is not possible using GitHub, is it possible from the git command line?

EDIT

Thanks to @Mark Longair for showing how this can be done from the git command line. If it's not possible in GitHub it would be a great feature to have.

Upvotes: 153

Views: 70583

Answers (4)

Kreidol
Kreidol

Reputation: 396

This is possible in the Github web UI. Github may download the file if it's too big or unsupported, may open it in a new tab if your browser can render the file, or may display it in-line. Here's two ways:

  1. Go to the repo and choose the branch you want to view history for

Method 1

  1. Navigate to the folder your file is in.
  2. Click on 'History', currently on the right side, upper area. Underlines the 'History' button on the github web UI–that does not look like a link until you hover over the text.
  3. From here, click on... what I'm just going to call the 'code paper' icon. It's currently positioned to the right of the commit sha button and has a label of, 'View at this point in the history.' Arrow points at the 'code paper' icon on the github web UI.
  4. Click "View Raw" (If you skipped step 2, drill in until you get to the file you need) to get at the code you need.

Method 2

  1. Click on the 'nnn commits' text. Currently on the right side. Arrow points at the '113 commits' link in the github web UI–that does not look like a link until you hover over the text.
  2. Click on the 'code' button. This one currently looks like two carets '<>' and has an label of, 'Browse the repository at this point in the history.' Arrow points at the 'code' button with the left and right caret imagery in the github web UI.
  3. Drill in and find the file you need, to get at the code.

Method 1 seems more efficient when I know what file I need, but not which commit to go back to. Method 2 is faster when I know exactly which time window or commit to look at, but maybe not where the file lives.


Disclaimer: UIs change frequently.

Upvotes: 3

Jamaine Roseborough
Jamaine Roseborough

Reputation: 42

To answer the initial question of viewing files from your repositories history directly from the GitHub online GUI, take the following steps. In your repo:

  1. View previous commits. View previous commits.
  2. Select the branch ID for a commit that corresponds to the point in history you wish to view. Select the branch ID
  3. In the top left, select Browse Files to view all files at that point in time. Select Browse Files

Upvotes: 0

Mikel
Mikel

Reputation: 6222

git log -G'your text' -p --all

Note the following from the docs;

[...] consider a commit with the following diff in the same file:

+    return frotz(nitfol, two->ptr, 1, 0);
...
-    hit = frotz(nitfol, mf2.ptr, 1, 0);

While git log -G"frotz\(nitfol" will show this commit, git log -S"frotz\(nitfol" --pickaxe-regex will not (because the number of occurrences of that string did not change).

Kudos both Mark and Cascabel for pointing in the right direction

Upvotes: 16

Mark Longair
Mark Longair

Reputation: 467071

Currently, I don't believe it's possible to search within the complete history of a repository's code on the github website - the closest is just searching within the current code of a repository with the "code search" option on this page.

However, from the command line, you can find any commits that introduced or removed lines mentioning get_info with the -S option to git log. e.g.:

git log -Sget_info -p

(n.b. there should be no space between -S and the search term)

(also note: to search for more than one word, surround in '):

git log -S'get info' -p

So, at a minimum that should find the commit where the function was first introduced and the one that removed it. I added the -p so you can also see the patches - if lots of commits introduced changes that mentioned the function that may be helpful. If the function was only on another branch it might also be useful to use --all to search all branches.

Jefromi points out in a comment below that git 1.7.4 will introduce the -G option as an alternative - this change is summarized in a recent blog post from Junio Hamano (git maintainer): http://gitster.livejournal.com/48191.html

Upvotes: 170

Related Questions