A very common task performed by developers while coding is undoing a commit.
This is a common mistake that prompts developers to turn to Google for answers! In this article, I will be teaching you how to undo these mistakes with Git.
There are four common scenarios where I think developers perform this task:
Undoing local changes that have not yet been committed
If you have made changes that you don’t like, and they have not been committed yet, do the following:

You can also use another command checkout to achieve the same thing, so:

Undoing a Commit (That Has Been Pushed) to a remote branch/repository
Each commit has a commit hash (A sequence of 7 random characters) that looks something like this – 224bc7a, in order to start the process of uncommiting changes that have been pushed, do the following

This will output the following; on the left you can see the commit hashes followed by the commit messages.

To undo a specific commit use:

This will make a new commit that is the opposite of the existing commit, reverting the file(s) to their previous state as if it was never changed. If you use the git log –oneline command again you should see a new hash and commit message that says “Reverting <previous commit message>”. The –no-edit option prevents git from asking you to enter a commit message. If you don’t add that option, you’ll end up in the VIM text editor. To exit VIM, press `:` to enter command mode, then `q` for quit, and finally hit Return (Mac) or Enter (Windows)
The last step is to push your changes to the remote branch:

Undoing a Commit (That Has Not Been Pushed)
To undo a commit that has not been pushed to a remote repository, use the reset command. Note that the reset command should be used if the commits only exist locally. If not, use the revert command, that way the history of undoing your commit is preserved. The command below also works if you want to undo the last commit you have locally:

A great hack is to add a number to the end of `~` to undo multiple commits. For example, to undo the last 2 commits – run git reset –soft HEAD~2.
Undoing Multiple Commits (That Has Been Pushed)
To undo multiple commits that are in a remote repository, you can use a cool command called rebase, which allows you to interactively pick what commits you want to keep or discard. You just have to give rebase a starting point. Use the git log –oneline command to check for the hash you want to use as a starting point.

In the example below, hash 58c2736 is the starting point.


In the vim editor, you can edit what commits you want to pick or discard. By default, all commit hashes have the word pick before the hash number, however, if you would like to delete a commit you press the `i` key on your keyboard which allows you to edit the file and change `pick` to `d`. To save changes, use `:wq` to close the vim editor.

Conclusion
Now you know how to undo a git commit using different techniques depending on the situation that presents itself.
In the next and final article in this series, we will be looking at 10 GitHub Tricks you should know. These commands I will be sharing will 10x your git skills.
In the final article, you also get a chance to grab a FREE GIT CHEATSHEET you can refer to anytime you get stuck.
Reminders
Create a bookmark folder in your bookmark bar. Left-Click on the Bookmark Bar -> Click Add Folder -> Name it `Git Series` -> Add this article to the folder
You can always message me on @mycodinghabits on Instagram or email me [email protected] if you have questions.
Useful Links
https://shecancode.io/blog/get-good-at-git-with-10-useful-commands-for-common-task
https://shecancode.io/blog/how-to-raise-a-pull-request-pr-on-github
https://shecancode.io/blog/how-to-resolve-a-merge-conflict-when-using-git