Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find to which git commit a file belongs?

Tags:

git

Given a random file, is there a canonical method to determine from the command line whether the file belongs to a particular commit?

This is similar to stack overflow question find-out-which-git-commit-a-file-was-taken-from except that I wish to be able to use it in scripts and also not create a temporary branch.

like image 825
Xevious Avatar asked Jul 27 '16 19:07

Xevious


People also ask

How do I know which commit changed a file?

Find what file changed in a commit To find out which files changed in a given commit, use the git log --raw command. It's the fastest and simplest way to get insight into which files a commit affects.

How do I find a specific commit in git?

Looking up changes for a specific commit If you have the hash for a commit, you can use the git show command to display the changes for that single commit. The output is identical to each individual commit when using git log -p .

How do I find the path of a file in git?

While browsing your Git repository, start typing in the path control box to search for the file or folder you are looking for. The interface lists the results starting from your current folder followed by matching items from across the repo.

How do you check details of a commit?

`git log` command is used to view the commit history and display the necessary information of the git repository. This command displays the latest git commits information in chronological order, and the last commit will be displayed first.


1 Answers

Below is an excerpt of a script that I have been using for the purpose. I hacked it together using my limited git knowledge and pieces of other scripts that I have found on the web. It works well enough, but I often find that there are easier ways to do things in git than what I have learned by trial and error.

FILE=$1

# git hash for file
HASH=`git hash-object $FILE`

# git revisions for file
REVS=`git log --pretty=%H -- $FILE`

# check each revision for checksum match
for rev in $REVS; do
    POSSIBLE=`git ls-tree $rev $FILE | awk '{print $3}'`
    if [[ $HASH == $POSSIBLE ]]; then
        echo $rev
    fi
done
like image 138
Xevious Avatar answered Oct 10 '22 22:10

Xevious