The following git steps will reset a code base to a specific commit, deleting all commits made since.
This is not considered git-best-practice as it “rewrites history” by deleting old commits and this can cause issues for collaborators who have pulled the commits you are deleting.
Knowing that, only apply this technique if you are sure that deleting commits is the outcome you want and understand the risks.
For other less destructive ways to revert git history, check out this thread: StackOverflow: How can I revert multiple Git commits?
Command summary
> git log --oneline
> git reset --hard <commit_hash>
> git push --force
Commands with explanations
See git history to get hash of the commit you want to revert to:
> git log --oneline
Reset to the hash of the commit you want to revert to:
> git reset --hard <commit_hash>
The above command:
- Resets the working directory, staging area, and commit history.
- Discards all changes in the working directory and staging area.
- Moves the branch pointer and HEAD to the specified commit, making it as if the specified commit is the latest commit.
- Irreversibly removes any commits and changes made after the specified commit.
Push the reset:
> git push --force
The above command:
- Pushes local changes to a remote repository
- Uses the
--force
flag, which does the push while ignoring checks that prevent overwriting commits on the remote repository