fbpx

EXPLORE OUR

How to undo a commit in GitHub

OMOTOLA SHOGUNLE

ARTICLE SUMMARY

When using GitHub, a very common task performed by developers while coding is undoing a commit. This is a common mistake which prompts developers to turn to Google for answers! In this 4th Guide on How to Get Good at Git & GitHub, Omotola will be teaching you how to undo a commit in GitHub. Whether you're reversing a commit pushed to a repository or managing multiple commits, these techniques will empower women in tech to navigate Git efficiently.

A very common task performed by Female 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
    • Undoing a Commit (That Has Been Pushed) to a remote branch/repository
    • Undoing a Commit (That Has Not Been Pushed)
    • Undoing Multiple Commits

    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:

    undo a commit in GitHub

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

    undo a commit in GitHub

    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

    undo a commit in GitHub

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

    undo a commit in GitHub

    To undo a specific commit use: 

    undo a commit in GitHub

    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:

    undo a commit in GitHub

    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:

    undo a commit in GitHub

    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.

    undo a commit in GitHub

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

    undo a commit in GitHub

    undo a commit in GitHub

    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.

    undo a commit in GitHub

    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 Women in Tech 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 mycodinghabits@gmail.com 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

    RELATED ARTICLES

    Regression analysis is one of the most fundamental concepts in machine learning. This article briefly introduces linear and logistic regression and how to implement them...
    This article provides practical strategies and insights to help you cope if you are suddenly laid off.
    As a developer, there are several websites that you may find useful and interesting. Here are a few recommendations.
    With over 700 programming languages in existence today, it can be a challenging task to know which programming language is right for your career. In...