33.33 Git Recover File from Previous Commit
20210403
Recover or revert to the file README.md
from
a previous (unknown) commit. This is useful if you’ve noticed a file
has been perhaps accidentally removed, or you would like to restore
the file to a previous version.
First determine the latest commit of the file, assuming that is the
commit that you want to undo. Here we list only one commit (-n 1
or
--max-count=1
), i.e., the latest, from the current branch where
HEAD
refers to the main
(default) branch. The --
indicates that
the following arguments are not options but are paths, and indeed in
this example we identify a single file of interest, README.md
This has a hash of d6be583
which can also be used to identify this
commit.
We can save the long commit identifier to a shell variable:
Then using that commit identifier as saved in the variable you checkout the prior version of the file:
Or replace $rev
with the identifier:
Or simply the hash:
Your donation will support ongoing availability and give you access to the PDF version of this book. Desktop Survival Guides include Data Science, GNU/Linux, and MLHub. Books available on Amazon include Data Mining with Rattle and Essentials of Data Science. Popular open source software includes rattle, wajig, and mlhub. Hosted by Togaware, a pioneer of free and open source software since 1984. Copyright © 1995-2022 Graham.Williams@togaware.com Creative Commons Attribution-ShareAlike 4.0