Checkout old commit from git based on date

倾然丶 夕夏残阳落幕 提交于 2019-12-24 12:25:17

问题


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 makes grep 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!