Nir
Nir

Reputation: 1744

how to go go back in a different version of the code in Git

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,

Upvotes: 1

Views: 205

Answers (2)

egghese
egghese

Reputation: 2223

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.

Upvotes: 3

tftd
tftd

Reputation: 17042

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

Upvotes: 3

Related Questions