How to find out what the extracted file committed

When I look at a file with git checkout $commit $filename , and I forget $commit , but remember $filename , how do I know what $commit ?

+4
source share
7 answers

At first the answer is not git. Check the history of shell commands. Well, if you didnโ€™t use the shell with the command history, then you donโ€™t ...

Git answer. Usually you cannot find THE $ commit. Typically, the same content could be part of many commits, and I donโ€™t think that git keeps a log of what you checked for one file (it keeps a log of previous HEAD values)

Here is the brute force of script git-find-by-contents. Call it your $ filename as a parameter, and it will show you all the commits where this file was included. As the name says, he is looking for content. That way, it will find files with any name if the content matches.

 #! /bin/sh tmpdir=/tmp/$(basename $0) mkdir $tmpdir 2>/dev/null rm $tmpdir/* 2>/dev/null hash=$(git hash-object $1) echo "finding $hash" allrevs=$(git rev-list --all) # well, nearly all revs, we could still check the log if we have # dangling commits and we could include the index to be perfect... for rev in $allrevs do git ls-tree --full-tree -r $rev >$tmpdir/$rev done cd $tmpdir grep $hash * rm -r $tmpdir 

I would not be surprised if there is a more elegant way, but it worked for me a couple of times in such situations.

EDIT: here comes a more technical version of the same problem: What commit does this blob have?

+2
source

I do not think you can. Git simply loads this version of the file into the index and your working directory. There is no link to which version he downloaded.

If you do this often, you can write a script that could do this using git diff --cached and go through all the commits that changed this file.

+1
source

you can use

 git log <filename> 

to find out which latch you want to check

0
source

If you're lucky, you can try not git:

 history | grep "git checkout.*filename" 
0
source

Try this (untested;):

 $ for commit in $(git log --format=%h $filename); do > if diff <(git show $commit:$filename) $filename >/dev/null; then > echo $commit > fi > done 
0
source

Simple and elegant:

 $ git log -S"$(cat /path/to/file)" 

It only works if the content is unique, and then again the same for the hash comparison answers that were before.

It also displays only the first version that matches, and not all.

0
source

Here are the details of the script that I honed as an answer to a similar question , and here you can see it in action:

screenshot of git-ls-dir runs
(source: adamspiers.org )

0
source

All Articles