Software Development

Git: Going back to a specific date time and retrospective tagging

I’m a fairly happy Git user :-)  It’s and excellent product and a great improvement on CVS, SVN, SourceSafe, PVCS and other systems I have used in the past.

In my day to day working I’ve got into a pattern of using very simple commands like clone, commit, update, pull, push etc.

But then I got into the situation where my code was broken but I couldn’t pinpoint exactly what had gone wrong or when this happened. What I wanted to do was to roll the code back until I reached the time when the code was last working which would allow me to determine the exact cause of the failure.

The question was how to go back in history using just the date:

Turns out that this is really easy: Just use t and retrospective tagginghe command below:

git checkout master@{2009-07-27 13:37}

If this fails because the reflog doesn’t go back to that point in time then you need the command below:

git checkout `git rev-list -n 1 --before="2009-07-27 13:37" master`

Another useful thing to do is to be able to tag a build retrospectively. So for example in our scenario, let’s say you’ve found the last working version you might want to tag it.

That’s also pretty easy.

Use the command:

git log

to show a history of all the commits on the project.  Once you have found the commit at which you want to tag just use this command:

git tag -a v0.9 7fcea889f -m "Retrospective tag"

7fcea889f matches the start of the commit id.

You can then push them up using:

git push --tags origin master

Then you have a tag which will easily allow anyone to checkout that version of code.

Daniel Shaya

Daniel has been programming in Java since it was in beta. Working predominantly in the finance industry he has created real time trading and margin risk applications. He is currently a director at OpenHFT where we are building next generation Java low latency products.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Michael Russo
Michael Russo
9 years ago

You may want to check out “git bisect” as an assisted way of doing this type of “find the commit that broke the code” debugging. https://git-scm.com/book/en/v2/Git-Tools-Debugging-with-Git

Back to top button