Bastien
Bastien

Reputation: 3087

Identify the version (SHA) of the last time a specific file was committed

I have a big project with multiple scripts and files all maintained with GIT. In all those files, there is a document on which I want to print the commit version (SHA) associated with this file. My Google search gave me:

git rev-parse --short HEAD

which gives me the HEAD of my project. However, if my document wasn't included in my last committed change, this version is not the one I want. This other stackoverflow answer proposed :

git log \-- c.rmd

but nothing output from this command in the console.

So, is there a way to output the latest commit SHA associated to one specific file/script? To make it more visual, I want a command that would output a3 from the specific tree when called for file c.rmd.

|
L__commit 4 - files a.r, b.r - SHA a4
|
L__commit 3 - files c.rmd - SHA a3
|
L__commit 2 - files a.r - SHA a2 
|
L__commit 1 - files a.r, b.r, c.rmd - SHA a1

At the end, I want that version number to be automatically printed in a RMarkdown document in R. So the solution could be either pure git or from an specific R package.

Upvotes: 0

Views: 137

Answers (1)

Romain Valeri
Romain Valeri

Reputation: 21908

To find the SHA of the last commit where file path/to/myfile.txt has been modified, go for

git rev-list --all -1 -- path/to/myfile.txt

For the short SHA :

git log --all -1 --pretty=format:"%h" -- path/to/myfile.txt

Upvotes: 3

Related Questions