fbpx

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.

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

    You're invited! Join SheCanCode's Women in Tech Community

    Find a supportive network, opportunities, jobs & much more, so you can excel in your tech career.

    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...

    This website stores cookies on your computer. These cookies are used to improve your website and provide more personalized services to you, both on this website and through other media. To find out more about the cookies we use, see our Privacy Policy.