问题
I am trying to retrieve an old commit from git, due to incompatibility with another project (which shares this code). The git repo is OpenZWave.
I found a post on SO saying to issue command:
git checkout @{14.days.ago}
But that results in:
warning: Log for '' only goes back to Wed, 23 May 2018 08:02:05 -0400.
HEAD is now at 77a05ed... Update hs-ms100plus.xml
I can't figure out what's wrong - clearly there are older commits than today's date. What is wrong with this command? (And will that command result in my clone being replaced with code from 14 days ago)
回答1:
The syntax @{XXX.days.ago}
refers to the reflog, a local history kept by Git in addition to the history as shown by e.g. git log
. So, @{14.days.ago}
means "where HEAD
pointed to 14 days ago on this repository, on my computer", not "the commit made 14 days ago" (which was probably made on another repository on another computer).
You can instead:
- Get the commit id, with e.g.
git log --before 14.days.ago -1
- Checkout this commit:
git checkout
回答2:
Why it didn't work?
git checkout @{14.days.ago}
tries to restore local state of specific repository at the given time, based on reflog (the record of all commits HEAD pointed to in past). If this is a fresh clone, it won't work.
So, the nice syntax won't work and you need to find some commit done a year ago, and check it out by the hash. How to do that?
How to find a commit submitted a year ago?
I would just try finding the proper commit in gitk
, scrolling through a year of commits shouldn't be an issue in most cases. However, you could also grep the log, like this:
git log --date=iso | grep -C3 2017-05-
or, if you know there were commits submitted on the specific day, like this:
git log --date=iso | grep -C3 2017-05-23
Short explanation:
--date=iso
ensures that the date is displayed in an easily greppable way.-C3
option makesgrep
include 3 adjacent lines, up and down, so that for each commit we capture the line with hash and the first line of commit message.
来源:https://stackoverflow.com/questions/50488030/checkout-old-commit-from-git-based-on-date