6

I am trying to inspect the history of a repository. I stopped at a point where I want to return the repository back to a specific commit. I searched and I did

git reset --hard 12345

I got the message that HEAD is now at 12345. I opened the folder to see the file I wanted to see but it didn't change!

I wanted to go back to 2009 when that file first created. However the file was in the new version (after editing it in 2010).

I checked the working directory but nothing there. I also ran

git clean -f -d

but nothing changed.

I don't know what should I do now. I want to go back in time to see how files were look like, but I want to go back to the whole repository.

jub0bs
  • 60,866
  • 25
  • 183
  • 186
Arwa
  • 63
  • 1
  • 1
  • 4
  • Welcome to Stack Overflow! You don't have to sign with your name since it automatically appears in the lower right corner of your post. A tip: If you indent lines by 4 spaces, they'll be marked as a code sample. You can also highlight the code and click the "code" button (with "{}" on it) – jub0bs Jan 30 '15 at 21:14
  • I've "+1"'d only because I love time travels. Sorry for offtopic :) – dk14 Jan 31 '15 at 03:26

3 Answers3

5

To be fair, I do not understand what happened to your repo after you did

git reset --hard 12345

What should be stressed is that you should usually avoid "git reset --hard" command. The risk is to lose any uncommitted change that you had and also end up with dangling commits. You would really use that command if you want to throw away all the commits that have been made after commit 12345. I invite you to read this thread about git reset --hard

In your case, I would actually have created a temporary branch "temp_branch" that would point towards the commit 12345. The command would be

git checkout -b temp_branch 12345

This will switch the repo to new branch "temp_branch", whose HEAD will be 12345. The creation of a new branch is not compulsory but I find it convenient if I need to make a change from that commit.

Community
  • 1
  • 1
iclman
  • 1,218
  • 9
  • 21
  • I haven't done any changes. What I am interested in is just investigating the history. What I really want is to get a copy from the repository at the time the commit was created and keep it for later analysis. So now, I need to return back the repository to its original state as well as getting a copy of the repository at that time. – Arwa Jan 31 '15 at 22:34
  • In that case, "git checkout" is the best option. It allow you to put your HEAD to the right commit. Basically, if you do "git checkout commitA", your repo will be at the state of commitA (provided you did not have any work ongoing. You can check that with "git status"). Then if you do a ""git checkout commitB", you repo will be at the state of commitB. In order to see where your HEAD is, you can do a "git log --oneline --graph --deco --all". If you want to come back to where your master is, you just need to do a "git checkout master". – iclman Feb 01 '15 at 12:47
  • Okay can I know what happened to the repo now, after I did "git reset hard". The situation is that I did "git reset --hard commitA" and commit A supposed to be in 2008. However, I did git log to see the commits history and I can see the commits starting from 2013. The original repo was started from 2015. I want to undo the reset operation. – Arwa Feb 01 '15 at 14:42
  • I would advise you to use the "git reflog" command to identify which operation were done on your repo. If the last action (Head@{0} was your "git reset --hard"), what you want is to get back to Head@{1}. In that case, you would want to do "git reset HEAD@{1}". I invite you to read http://stackoverflow.com/questions/5788037/recover-from-git-reset-hard – iclman Feb 01 '15 at 16:46
4

for me I did not stage files that is why git reset --hard was not working for me
I just ran the following command

git add . => to stage all files (don't ignore .)

git reset --hard => to reset all changes

Sultan Ali
  • 2,497
  • 28
  • 25
1

If all you want to do is look at the content at commit, don't use reset. reset is for moving branches around and you can get yourself into trouble that way. Just check it out, git checkout 12345. Then you can use checkout again to go back to where you were, git checkout master (or whatever branch).

As for your file not being changed, are you sure its different in that commit? Check with git diff 12345 branch_name -- filename.

Schwern
  • 153,029
  • 25
  • 195
  • 336