1

I'm trying to go back to a different date on the branch i'm working on.( like 3 days ago) I've created a patch of the files i have changed so i don't mind if it will be override.

Thanks in advance,

Nir
  • 1,524
  • 6
  • 23
  • 41
  • do you know about git checkout ? – egghese Jul 20 '13 at 19:54
  • do you want to revert your code to that date or just do a `checkout` ? – tftd Jul 20 '13 at 19:55
  • I've tried the following[master +2 ~17 -0 !]> git checkout 'master@{2013-07-18 12:00 :00}' but it still had the changes from a later date. – Nir Jul 20 '13 at 19:56
  • @ttftd , just to do a checkout, i've created a patch of my code. – Nir Jul 20 '13 at 19:57
  • 3
    git log and git checkout, or git reset -soft – egghese Jul 20 '13 at 19:57
  • possible duplicate of [How can I switch my git repository to a particular commit](http://stackoverflow.com/questions/4940054/how-can-i-switch-my-git-repository-to-a-particular-commit), and [Git switching between commits](http://stackoverflow.com/questions/8772328/git-switching-between-commits). –  Jul 20 '13 at 20:18

2 Answers2

3

This is how you can checkout your code to a previous commit you've made back in time.

  1. git log to see all your past commits and choose the one you want to revert to
  2. copy the hash you want to revert to (i.e. aefd2efc660f4gb2fa2d7r1ef73b3z4e2b4498e5)
  3. git checkout aefd2efc660f4gb2fa2d7r1ef73b3z4e2b4498e5
tftd
  • 16,203
  • 11
  • 62
  • 106
3

You just need to see your git log and get the sha hash of the commit you want to got to and do a git checkout.

In your case, your git log need to focus on the time, where changes happen, like 3 days ago. So something like below will help you.

$git log --pretty=format:"%h - %an, %ar : %s"

will give something like this

$387820f - var_j, 25 hours ago : Rust Lessons: variables
$72a4abc - var_j, 25 hours ago : Rust Lessons: loops
$f272f95 - var_j, 25 hours ago : rust lessons: hello world added

And then,

$git checkout 387820f

this will detach head and move it to the particular state, so the working copy will also get updated accordingly.

And if you really want to go back in time, consider

$git reset --soft <commit_hash>

git reset --soft

Does not touch the index file nor the working tree at all (but resets the head to , just like all modes do). This leaves all your changed files "Changes to be committed", as git status would put it.

But be careful using git reset, it can cause damage. Read the doc and decide what you need.

egghese
  • 2,193
  • 16
  • 26