1

I did a stupid thing; I did 'git fetch' on local repository that was not committed. So my files now overwritten by it. How I can restore my plight that was before this stupid mistake?

git_status_1

git_status_2

things that I've done:

  1. Modify some file on web-github-commit this (because I'm stupid)
  2. git add .
  3. git commit -m
  4. git push origin branchA -> that was rejected because of the commit I've done from website
  5. git reset --hard HEAD^ (because I'm really stupid)
  6. git push origin branchA -> rejected because remote contains the work that I do not have locally (same reason as 4)
  7. git fetch -> counting objects 5
  8. git reset --hard HEAD^ (this is the point when I was starting to think that I'm doing shit)
  9. git reset 'HEAD@{1}' -> my files modified (I'm sorry initially I was thinking that git fetch botched my life)
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
  • 1
    I was under the impression that `git fetch` would not affect the working tree... could you describe your situation in more detail? What does `git status` show? – Jon Skeet Apr 04 '18 at 21:13
  • Possibly duplicate of https://stackoverflow.com/questions/35591887/how-to-undo-git-fetch – CInvt Apr 04 '18 at 21:17
  • @DaisyShipton, images attached, thanks for prompt reaction! – stronk_kisik Apr 04 '18 at 21:19
  • 1
    Okay, so it shows those modifications - are those not just the changes you've made? It's still not really clear that the problem is. – Jon Skeet Apr 04 '18 at 21:23
  • 2
    `git fetch` does not affect the work tree. You did something else. – jsageryd Apr 04 '18 at 21:27
  • @DaisyShipton, full list of stupid things now listed! I'm already sorry for misleading title – stronk_kisik Apr 04 '18 at 21:39
  • So hang on, you *have* committed the changes? Are you just trying to get back to that commit? I suspect that `git reflog` is your friend at this point, so that you can check back to the commit you want, then merge in other changes. – Jon Skeet Apr 04 '18 at 21:46
  • @DaisySHipton, yes, i have my local commit in 'git reflog', on HEAD@{3} - should i just use another git reset? – stronk_kisik Apr 04 '18 at 21:52
  • @user9403409: That would probably be my first port of call, yes. – Jon Skeet Apr 04 '18 at 21:53
  • @DaisySHipton, you are my savior! Could you please make an answer so i could check you as right answer? – stronk_kisik Apr 04 '18 at 22:02

1 Answers1

1

Just performing a git fetch doesn't touch the working tree. (git pull would try, because that does a fetch followed by a merge, but that's a different matter.)

It looks like you have committed the changes though - but then confused yourself afterwards (which can be easy to do with git, certainly...)

Fortunately, git reflog is your friend: it will show you everything you've done, including the commit hashes along the way. Run it and find the commit you want to get back to, then use git reset --hard <commit> to get back there. You can then merge the fetched changes, or rebase, or whatever it is you want to do.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194