xxxxxxxxxx
# Resets index to former commit; replace '56e05fced' with your commit code
git reset 56e05fced
# Moves pointer back to previous HEAD
git reset --soft HEAD@{1}
git commit -m "Revert to 56e05fced"
# Updates working copy to reflect the new commit
git reset --hard
# Push your changes to respective branch
git push -f
xxxxxxxxxx
//git use a new commit to replace an old commit,commit moves foward not backward
git revert <commit hash>
//Git goes back one spot on the log,undone this commit and go backward one commit:
git reset HEAD~1
//Git looks for and rolls back to a certain file:
git checkout commit-hash-here -- file/location/and/name
xxxxxxxxxx
git reset --hard branch_name #Reverts all modified files to last commit on branch
xxxxxxxxxx
# DO NOT USE git reset unless you want to lose all commits done after the reset.
# If you want to just undo all the changes of a previous commit use git revert.
# EXAMPLE:
# Add files and commits$
$ touch alpha.html
$ git add . && git commit -m "1st git commit: 1 file"
$ touch beta.html
$ git add . && git commit -m "2nd git commit: 2 files"
$ touch charlie.html
$ git add . && git commit -m "3rd git commit: 3 files"
$ touch delta.html
$ git add . && git commit -m "4th git commit: 4 files"
$ touch edison.html
$ git add . && git commit -m "5th git commit: 5 files"
# Get commit information
$ git reflog
(HEAD -> master)
d846aa8 HEAD@{0}: commit: 5th git commit: 5 files
0c59891 HEAD@{1}: commit: 4th git commit: 4 files
4945db2 HEAD@{2}: commit: 3rd git commit: 3 files
defc4eb HEAD@{3}: commit: 2nd git commit: 2 files
2938ee3 HEAD@{4}: commit: 1st git commit: 1 file
# Revert changes from 3rd commit: This will remove the charlie.html addition.
$ git revert 4945db2
# If you use git reset here you would remove the 3rd commit changes as well as the 4th and 5th.
xxxxxxxxxx
# Revert is the command to rollback the commits.
git revert 2h3h23233
# push after change
git push
xxxxxxxxxx
Lots of complicated and dangerous answers here, but it's actually easy:
git revert --no-commit 0766c053..HEAD
git commit
This will revert everything from the HEAD back to the commit hash, meaning it will recreate that commit state in the working tree as if every commit after 0766c053 had been walked back. You can then commit the current tree, and it will create a brand new commit essentially equivalent to the commit you "reverted" to.
(The --no-commit flag lets git revert all the commits at once- otherwise you'll be prompted for a message for each commit in the range, littering your history with unnecessary new commits.)
This is a safe and easy way to rollback to a previous state. No history is destroyed, so it can be used for commits that have already been made public.