0

I have a few questions:

  1. How do I find/search for a specific file in all of my past commits? Let's say, I want to find the the file test.py in all of my past commits. I don't want it to search the regular information such as commit message, author, date, etc..

  2. Once I find this file, in step 1 above; How would I compare test.py in another commit to the same file in any other commit? I could use the git diff <commit_id> test.py <commit_id> test.py?

I am not talking about comparing working directory to staged or staged to last commit.

ok. So I know how to find files in past commits. For the results below I used, git --pretty=oneline card.py

But, how do I compare the file based on this output:

6c40f05c9 (HEAD -> master, origin/master) Added debugger.py and time-your-code.py
fa7219963 Python Generators
Biffen
  • 6,249
  • 6
  • 28
  • 36
Musicman
  • 49
  • 6
  • 2
    I think your first question is answered in https://stackoverflow.com/questions/372506/how-can-i-search-git-branches-for-a-file-or-directory ? – 9769953 Sep 04 '21 at 13:52
  • 3
    `git diff --help` shows, among others, usage like `git diff [] ... [--] [...]`, which is probably what you looking for. – 9769953 Sep 04 '21 at 13:53
  • How do I find a specific file in past commits? – Musicman Sep 04 '21 at 16:12
  • I know how to locate the file in past commits. git log test.py. Based on your syntax above, how would I compare the file based on commit ID or blob? – Musicman Sep 04 '21 at 16:38
  • @MattPaolini : 9769953 quoted the doc, when applied to your case it reads : `git diff fa7219963 6c40f05c9 -- card.py` – LeGEC Sep 07 '21 at 07:48
  • Also : do take time to read the docs for `git log`, which has a ton of options to display stuff from your repo. For example : the `-p` option adds the diff after each commit : `git log --oneline -p -- card.py` – LeGEC Sep 07 '21 at 07:49

1 Answers1

0

Comments already pointed out all the right steps, I'm merely consolidating into a single answer readers can use:

Say you want to compare your file card.py to historical revisions, you can use a 2-step method:

  1. Check which commits changed that file using git log --pretty=oneline card.py. Output should looke similar to:
570cf4c17d6b7a5a2dae7a57739a9ca07987631e Add something
e961dc9c5cef37cc94841fbddfbfa88471a329da Add something else
ab385ef517e9d6ee4ec27b4e8c318aa76bb8dc66 Revise something
a1421b529300a8279db3aec004f27359a4346f3b Revise something else
  1. Compare your existing version to an older one using git diff e961dc9c5cef37cc94841fbddfbfa88471a329da -- card.py
Daniel Trugman
  • 8,186
  • 20
  • 41