I am a git novice, and can't really understand the answers to similar questions. Sorry. I made a bad mistake many months ago and need to fetch a previous version and make it the current one. Since I couldn't figure out how to do this elegantly, I did it by brute force - downloading a zip file with the version I wanted then unzipping it and using commit and push to make it the latest version. If SHA is the identity of the version I want to "promote", exactly what is the git CLI command to accomplish this?
Asked
Active
Viewed 57 times
2 Answers
1
After cloning your repo, go to that directory and check SHA id from git log:
Try:
git log
Then:
# Resets index to former commit; replace '<commit_id>' with your commit code
git reset <commit_id>
# Moves pointer back to previous HEAD
git reset --soft HEAD@{1}
git commit -m "Revert to <commit_id>"
# Updates working copy to reflect the new commit
git reset --hard
For more info, visit this link: How to revert Git repository to a previous commit?

Ronan Boiteau
- 9,608
- 6
- 34
- 56

MD Ruhul Amin
- 4,386
- 1
- 22
- 37
-
I just put a comment on Answer 0, but it seems this does exactly what I wanted, bringing the last good file to the top, and leaving the rest of the "stack" alone - is that right? – dms489 Feb 17 '18 at 17:04
-
yeah, it is. @dms489. FYI, you can also start a new branch leaving the old one untouched by doing, `git checkout -b
`. And do what ever you want to do with ` `
1
- Use
git log
to get the SHA-1 hash of the commit you want to fetch. git reset --hard [your_commit_hash]
git push --force
Warning: this will completely erase all the commits you made since the commit you git reset
to!

Ronan Boiteau
- 9,608
- 6
- 34
- 56
-
Thanks for the quick reply. I don't mind losing all the intermediate versions, but would like to keep the latest in the bad series of events for reference. So I have 2 SHAs - SHA last_good and SHA_latest_bad. When I finish I'd like to have SHA_last_good on the top of the "stack" and SHA_latest_bad beneath it. Is that possible? – dms489 Feb 17 '18 at 16:52
-
In this case, you can use `git reset` and `git push --force` to restore the latest bad commit and then commit again your latest good commit using your brute force method. – Ronan Boiteau Feb 17 '18 at 17:03
-
You can save the bad commit history by putting a branch there. Intuitively, that's probably what you want to do anyway. – Mad Physicist Mar 03 '18 at 21:28