How to delete a Git commit?
You've made a commit, everything seems perfect, but suddenly you realize that your latest commit contains an error. Perhaps you've accidentally included a file, forgotten a crucial change or introduced a major bug in the code. Now what? 🤔
Delete the commit we've just made, of course! But you still need to know how to do it 😉
Rest assured, Git is designed to handle this type of situation.
That's the whole point of a version manager: to enable you to go back and correct errors without affecting your entire project history. But when faced with several possible commands, such as reset
and revert
, choosing the right method can seem complicated.
In this article, we'll take a look at the different ways of deleting or undoing a commit in Git. You'll learn not only how to revert to the previous commit, but also how to restore the history to any previous commit, whether on your local repository or after pushing your changes online.
Ready to go? Let's get started! 😗
But what exactly is a Git commit?
A commit in Git represents a point in time at which a file is saved (also known as a snapshot).
Each commit has a unique SHA-1 identifier.
It's this identifier that's going to help us go backwards in git!
Before continuing, it's important to distinguish between two types of history:
- Local histories: which are only stored on your local computer;
- Shared logs: which are already published online on a repository (GitHub, for example).
How you go back will depend on whether you have already published your commit online or not.
Deleting a local commit before you've pushed it
If you haven't yet pushed your changes to the remote repository, deleting or cancelling a commit is relatively simple and safe. You can use the git reset
command to do this.
Going back with git reset
The git reset
command allows you to reset your repository to an earlier version. It has several options that modify the way Git manages files and commits. So don't make a mistake! 🥸
To delete the last commit, you can use the following command:
git reset --soft HEAD~1
This command allows you to go back one commit while preserving the changes made in between: you can then modify or re-commit the new changes made.
Deleting the last commit with git reset --hard
If you want to completely delete the last commit and all its modifications, you can use the --hard
option:
git reset --hard HEAD~1
This command deletes the last commit and all modifications made to it.
This command is irreversible.
Deleting a commit after pushing it
If you've already pushed your commit to a remote repository, deleting or undoing a commit becomes trickier, especially if other people are working on the same branch as you. 😕
Let's imagine Pablo working on a branch that has already integrated your modification: he's not going to be happy if you tell him that all his work is going to go away. But there you go.
Fortunately, there are a number of options available to limit the damage.
Use git reset and force update
Fortunately, most of the time, you're the only one working on your branch.
If this is your case, you can use the git reset
command as in the previous case, then force the update on the remote repository with git push --force
.
Here's how to do it:
Step 1: Revert with git reset
git reset --hard HEAD~1
Step 2: Push the change by forcing the update
git push --force
Warning: using
git push --force
can completely destroy your history on the remote repository, which can cause major problems for other developers working with you. Use this command only if you're sure it won't affect anyone else.
Using git revert
to undo a commit
If you don't want to modify the shared history and would rather go back cleanly while keeping the commits, you can use the git revert
command.
Unlike
reset
,revert
creates a new commit which undoes the changes made in the previous commit.
To undo the last commit
git revert HEAD
To undo a specific commit with the commit's SHA-1 identifier
git revert <commit-sha>
This method is safer for shared repositories, as it does not modify the existing history, but rather creates a new undo commit.
Handling multiple commits with git rebase
Sometimes you make an error not on a single commit... but on several! 🙄
Fortunately, there's also a command for deleting multiple commits at once: git rebase
.
This command will allow you to rewrite your history by modifying, deleting or merging the commits you specify.
Here's how to use git rebase
to delete multiple commits:
- Start by specifying the number of commits to be modified:
CONSOLEgit rebase -i HEAD~3
- A list of recent commits should appear. You can now specify whether you wish to replace (with the word
pick
) or delete (with the worddrop
) a commit next to the commit concerned. - Once you've made your changes, there's only one thing left to do: save and exit the editor! Git will then adjust the history according to the changes made.
The
git rebase
command is very powerful, but can be risky if used incorrectly. Use it with care, especially when working on a shared repository.
Tips for avoiding future problems
Now that we've seen how to delete a commit, let us give you a few tips to avoid problems in the future... 😬
For starters, work on branches all the time.
That's what they're for!
Next, use git revert
as a first step. It's much more interesting because it doesn't break the code of people you're also working with on the branch, unlike the git reset
and git push --force
commands.
Finally, make frequent backups to avoid having to come back to a commit that has recorded hundreds of modifications: it will be much easier to find your bug and rectify it.
Conclusion
Deleting a commit in Git is a common task, but one that needs to be performed with care, especially when working with other developers.
You can choose between git reset
to roll back the local history, git revert
to cleanly undo a commit, or git rebase
to rewrite the history in a more complex way.
Whichever method you choose, it's essential to understand the consequences and use these commands appropriately to avoid conflicts in the Git history.
If you'd like to learn more about Git, take a look at our comprehensive training course on Git and GitHub. You'll love it!