Mir S Mehdi
Mir S Mehdi

Reputation: 1530

Restoring a Git Repository to a particular date

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 ?

Upvotes: 2

Views: 3524

Answers (2)

Brian Campbell
Brian Campbell

Reputation: 333066

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.

Upvotes: 1

Gary Fixler
Gary Fixler

Reputation: 6048

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

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

Upvotes: 3

Related Questions