简体   繁体   中英

How to restore my added but not committed files that was modified by git

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM