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, an essential skill on A Learning Path To Becoming a Data Scientist. Understanding version control is crucial for data scientists, as it helps manage changes in code and ensures smooth collaboration.
There are four common scenarios where I think developers perform this task:
- 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:
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 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
A commit is like a “save point” in your project’s version history. It records changes you’ve made to your code, along with a message describing those changes. Commits help you keep track of what’s been done and make it easy to review or roll back updates.
Making a commit involves a few basic steps:
- Add your changes to the staging area: git add .
- Create a commit: git commit -m “Describe your changes here”
- Push the commit to GitHub: git push origin main
Replace main
with your branch name if needed.
Deleting a commit requires using Git commands. If it’s your most recent commit, you can use this: git reset –hard HEAD~1
Be careful! This permanently deletes the commit. For older commits, use an interactive rebase: git rebase -i HEAD~N
Replace N
with the number of commits to review.
Reverting is a safer way to undo changes because it creates a new commit to “cancel out” a previous one without deleting it: git revert <commit-hash>
The <commit-hash>
is the unique identifier of the commit you want to revert. This approach is great for preserving history while fixing mistakes.
You can view commit history in two ways:
- On GitHub.com: Go to your repository, select the “Commits” tab, and view a list of all changes.
- In the Terminal: Use
git log
for a detailed view of your commits.
For a simpler output, trygit log --oneline
.
Tags mark specific commits as important (e.g., for software releases). Here’s how to create a tag:
git tag -a v1.0 -m “Version 1.0”
git push origin v1.0
Replace v1.0
with your tag name. Tags help identify significant milestones in your project.