0

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.

Romain Valeri
  • 19,645
  • 3
  • 36
  • 61
Bastien
  • 3,007
  • 20
  • 38
  • 2
    Note that if the file gets created by cloning the repository, that file is actually *in* the commit you've chosen there. Romain Valeri correctly identified that you want, not this commit, but rather some older commit. That is, you want Git to do its usual thing: start at the *last* commit and work backwards, one commit at a time. At each point, it should *compare* the given file with the copy in the *previous* commit. If the two don't match, you'd like the hash ID of the commit where the parent's copy differs. – torek Feb 03 '20 at 17:24

1 Answers1

3

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
Romain Valeri
  • 19,645
  • 3
  • 36
  • 61
  • for package you can use `gitr` or if you git is ssh enabled then `system(git_command)` will also work – Priya Feb 03 '20 at 15:11
  • it works, thanks! Do you know if `git rev-list` as an option similar to the `--short` of `git rev-parse`? – Bastien Feb 03 '20 at 15:13