2

I would like to restore my local git repository to a particular date. By restoring, I would like to keep all the commits and history which occurred till that date and time.

I am all okay to loose changes and commits which were performed after that date and time. (Assuming Jan 1st, 2014 11:30 AM)

Is this possible ?

Mir S Mehdi
  • 1,482
  • 3
  • 19
  • 32
  • 3
    This is literally one of the most basic things you can do with version control software. Have you considered reading a git tutorial? – Cairnarvon Jan 20 '14 at 00:51
  • @Cairnarvon I did use `git reset --hard`, but looks like it resets only the branch. I want to restore all branches as well at the same time. I tried looking git tutorial, but did not find much to restore the complete repo itself. Can you please advice – Mir S Mehdi Jan 20 '14 at 01:00
  • Do you have so many branches that you actually need to automate this? You should just be able to examine the reflog for each branch and figure out where the branch was at the end of that date. – user229044 Jan 20 '14 at 01:10
  • I asked a similar [question](http://stackoverflow.com/q/19030192/1860929) some time back, check if that helps – Anshul Goyal Jan 20 '14 at 11:35

2 Answers2

3
git reset --hard 'master@{2014-01-01 11:30:00}'

That'll reset your master branch to the closest commit before that time.

Gary Fixler
  • 5,632
  • 2
  • 23
  • 39
1

To update all of your refs, you can use git for-each-ref which can generate the command to run on each ref, then pipe the output of that to your shell.

git for-each-ref --shell \
    --format="git update-ref %(refname) %(refname)@{2014-01-01 11:30:00}" | sh

Any time you run a command like this, make sure you have a backup so you can restore the repository if something goes wrong. Also, try it once without the pipe to sh to see the commands that will be executed and make sure they look reasonable.

Brian Campbell
  • 322,767
  • 57
  • 360
  • 340