I foolishly tinkered with my local file on my comp and realized I made too many edits which has ruined the application, therefore I want to start fresh all over again. The problem is, is that I can't recall all of the changes I made to manually return it to its original state. How do I take my most recent commit from my Repo, and use that to replace everything on my local file so that it can be in the exact same state before I began editing?
-
you need `git reset --hard
` – Seek Addo Mar 12 '17 at 23:25 -
Possible duplicate of [Hard reset of a single file](http://stackoverflow.com/questions/7147270/hard-reset-of-a-single-file) – Marcus Mar 13 '17 at 00:05
4 Answers
First, you need update your local code to the commit or branch that you want.
git fetch origin
git fetch origin branchOrCommitId
git reset --hard FETCH HEAD
Then if you want, you could create a new local branch to work in it.
git checkout -b nameBranch

- 315
- 2
- 16
You can revert back using log
or reflog
.
if you want to go back to specific commit.
Run in your terminal
git log
you will see a list having all the commits. Copy commit's hashcode where you want to revert back (min. 6 digits from the left)
and then run
git reset --hard hashcode
(e.g hashcode = 6e3969
)
PS: Use can reflog
instead of log
to go back to your activities as well as commits.
If you are newbie then do all these commands after switching to new branch.
git checkout -b new_branch
.
In the new branch you can do anything without any fear. If things goes fine then checkout to your original branch and do the same what you have done in new_branch
.
If anything goes wrong. Simply delete the new_branch
. again come to your original branch and checkout to a different branch.

- 178
- 1
- 8
-
This looks very useful, but as this answer is 3+ years old, I decided to double-check it & verify the syntax on the `hashcode = ...` argument. Looking in `man git-reset` I don't see a reference to a `hashcode` argument. Have I missed something? – May 07 '20 at 02:09
-
It still works. Heer hashcode meant `commit ID` in case of `git log`, `8 digit reference id` in case of `git reflog`. Do Checkout `man git-reflog`, `man git-log` and `man git-reset` for more details. – spyshiv May 11 '20 at 09:01
To exaclty get the most recent version from github, do:
git checkout HEAD -- <ruined-file-name>

- 922
- 6
- 21
git fetch origin master
git reset —hard FETCH_head
git clean -df
This is what ended up working for me. Thanks for all the recommendations.

- 115
- 4
- 14