Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract commit file from commit id in git?

Tags:

git

command

I want to extract the commit file before the commit and the after the commit only using one commit id. Is there any way to extract this?

commit 471d4d60dc21fbccb8c6b4616a00238c245f78f6
Author: Gilles Sadowski <[email protected]>
Date:   Mon Oct 28 01:51:42 2019 +0100

MATH-1500 (unit tests).
src/test/ ... linalg/FieldLUDecompositionTest.java

commit 9988a5b3c4779eadfad9ea0c4925f5b327317814
Author: Gilles Sadowski <[email protected]>
Date:   Sun Oct 27 15:11:56 2019 +0100

Code upgraded following MATH-1500.
src/main/... nonstiff/AdamsNordsieckTransformer.java

In this case i want to extract file name "FieldLUDecompositionTest" before the commit and after the commit only using one commit id which is "471d4d60dc21fbccb8c6b4616a00238c245f78f6"

like image 714
DDDam Avatar asked Sep 18 '26 01:09

DDDam


1 Answers

The proper way to extract/restore a file from a commit is, with Git 2.23+ (August 2019) to use git restore (less confusing than git checkout)

git restore <SHA1>

To restore only one specific file from that old commit:

git restore <SHA1> -- path/to/file

That would update only your working tree by default.


Getting the previous SHA1 is easy:

471d4d60dc21fbccb8c6b4616a00238c245f78f6~

Git the next SHA1 is more complex: see "How do I find the next commit in git? (child/children of ref)":

git log --format=%H --reverse --ancestry-path ${1:-HEAD}..${2:\"$(git rev-parse --abbrev-ref HEAD)\"} | head -1

More in torek's answer)

git rev-list --topo-order --ancestry-path --reverse <id1>...<id2> | head -1

So you can make a script which will get the parent and child of a given commit, and use git restore to get/see the files of those commits.

like image 116
VonC Avatar answered Sep 20 '26 17:09

VonC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!