Scenario: I have added a block of code, and it has caused all backgrounds for all button elements on my site to turn green. I am not happy with the method used, and wish to remove the code completely so I can start from before this change was applied on this branch, and implement a better way of targeting the buttons, using another approach on the same branch (backtrack).
-
1Does this answer your question? [How do I revert a Git repository to a previous commit?](https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit) – TTT Feb 20 '22 at 18:47
-
1Sadly, the most popular duplicate for this question is locked and cannot be edited. That question title uses the word "revert" in the English context rather than the Git context. It's pretty clear that that question is asking for "reset", which, is cleared up in the accepted answer. – TTT Feb 20 '22 at 18:53
2 Answers
wish to remove the code completely
To backtrack, you have two choices. First, find (using git log
) the SHA of the commit you want to backtrack to, i.e. the last "good" commit before the "bad" commits started. Then either
git reset --hard <SHA>
or
git revert <SHA>..HEAD
The difference is that reset
rewrites history and throws away the subsequent commits, while revert
keeps the history and introduces new "undo" commits.

- 515,959
- 87
- 875
- 1,141
-
1Your answer is great, but I'm kind of surprised you answered this obvious(?) dup. :D – TTT Feb 20 '22 at 18:54
-
1@TTT I was just trying to supply a corrective to the other answer. Also, since I don't have the golden dupehammer for `git` tag, it is sometimes better just to answer than to wait around silently for more people to come along and close-vote. – matt Feb 20 '22 at 18:58
-
1Hehe. Not sure if you noticed, but that answer was posted by OP together with the question. I think a better solution in this case would probably be to delete the question... – TTT Feb 20 '22 at 19:00
-
1@TTT Also, the dupe I would generally use in this situation is my own https://stackoverflow.com/questions/3528245/whats-the-difference-between-git-reset-mixed-soft-and-hard/59675191?r=SearchResults&s=1|37.7914#59675191 but it doesn't apply quite correctly to the question as it was posed. – matt Feb 20 '22 at 19:01
-
3@TTT I didn't notice! Sheesh. Well in my view that makes that answer even more wrong than it already was. – matt Feb 20 '22 at 19:02
-
1Your regrets answer would probably be a good answer to hundreds of Git questions on SO. ;) – TTT Feb 20 '22 at 19:13
Solution: Reset the code to an earlier point
Steps to take:
Identify where you want to revert your code back to - git log
Next, copy the first 7 digits from the SHA commit you wish to jump back to using the branches commit history and SHA numbers - You only have to use the first 7 characters to apply the changes I am about to describe: 9bfc696
Now enter this command to reset the code - git reset 9bfc696
Your code is now at the earlier commit point, and the changes have been un-staged -
Unstaged changes after reset:
M green-buttons.css
A message similar to the above will appear in your terminal
Next, we want to see the older code reflected in our current working directory/session.
Type in this command to restore your working directory to the HEAD point of the repo - git checkout . HEAD
(Show me the current directory at the main point in the current workflow)
The code will now boot back into the earlier commit, and also remove those unstaged changes. It is important that you are clear you do not want to retain any of the code before carrying out this process.
To take greater precautions, and create a backup of your rogue changes, follow this process instead:
git checkout [working branch - this is the one we will run the deletion on]
git checkout -b [new backup branch name]
git checkout [working branch]
git log
git reset 9bfc696
git checkout . HEAD
Please do let me know if there is anything else that you feel should be added or removed from this Q&A style answer, I would be keen to make those changes.

- 63
- 7
-
2Just FYI, I can appreciate your attempt here, but there are multiple issues with this answer. Furthermore, the question is a dup so I don't think either the Q or A have much value here. That being said, please don't be discouraged. Git has a large learning curve, and just coming up with this answer puts you far beyond the average Git user already. ;) – TTT Feb 20 '22 at 19:17
-
With great power...
Thank you for your feedback on the answer above. – Dan Haddock Feb 21 '22 at 12:28