How can I revert back to a Git commit?
Solution 1
Git commit only saves it to the stage, which is locally on your computer. Use Push to update it to a remote server (Like github).
Use git revert <ID>
to revert back to a previous commit. each commit has an identifying code.
See here for more details on revert
Solution 2
The above answer is not quite correct - git revert <ID>
does not set your repository to that commit -- git revert <ID>
creates a new commit that undoes the changes introduced by commit <ID>
. It's more or less a way to 'undo' a commit and save that undo in your history as a new commit.
If you want to set your branch to the state of a particular commit (as implied by the OP), you can use git reset <commit>
, or git reset --hard <commit>
The first option only updates the INDEX, leaving files in your working directory unchanged as if you had made the edits but not yet committed them. With the --hard option, it replaces the contents of your working directory with what was on <commit>
.
A note of warning that git reset
will alter history -- if I made several commits and then reset to the first commit, the subsequent commits will no longer be in the commit history. This can cause some serious headaches if any of those lost commits have been pushed to a public repository. Make sure you only use it to get rid of commits that haven't been pushed to another repository!
Related videos on Youtube

Sab
Updated on September 18, 2022Comments
-
Sab 1 day
I just started using Git on Windows and I have a small question. When I say
git commit
along with a message I realize that git commits the file. But where exactly does it store the commited file?Let's say I make some changes and want to revert back to my third commit using Git GUI. How exactly do I do that?
-
Kellen Stuart almost 4 yearsThe point of Git is that you don't need to know how Git is storing the backups and history. Git handles all of that for you
-
-
Omar over 4 yearsafter I reset how do I commit
Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded.
-
Gaurav Rajdeo almost 4 yearssince we change our code to older commit, in order to push it we should force push it with this command git push -f origin <branch_name>. Otherwise git will always prompt you to take the pull first and then push.
-
MikeW about 3 yearsgit push -f is NEVER a good thing to do !
-
Jeff P Chacko over 2 yearswrong answer. this only reverts this specific commit.
-
Asbjørn Ulsberg 12 monthsI disagree, @MikeW.
git push --force
is completely unproblematic if you're working in solo on a branch and want its history to be tidy and sensible, for instance as work on a pull request against a forked repository. It's also unproblematic if you have tight coordination with people you cooperate with, they just need to force-pull your changes before they continue work on their side.