How to Delete Commit From the Remote Repository in Git
- Understanding Commits in Git
- Deleting the Latest Commit
- Deleting a Specific Commit
- Interactive Rebase to Delete Multiple Commits
- Conclusion
- FAQ

When working with Git, it’s not uncommon to find yourself in a situation where you need to delete a commit from your remote repository. Whether it’s due to an error, sensitive information being pushed, or simply wanting to tidy up your project history, knowing how to do this effectively is crucial.
In this tutorial, we will walk you through the steps to delete a commit from a remote repository using the Git command line. By the end of this guide, you’ll have a solid understanding of the various methods available, ensuring you can manage your Git history with confidence.
Understanding Commits in Git
Before diving into the methods for deleting commits, it’s essential to grasp what a commit is in Git. A commit represents a snapshot of your project at a specific point in time. Each commit has a unique ID, allowing you to track changes and revert back if needed. However, sometimes you may need to remove a commit to maintain a clean and secure repository.
Deleting the Latest Commit
One of the most straightforward scenarios is deleting the most recent commit from your remote repository. This can be accomplished through a few simple commands. Let’s look at how to do this:
First, you need to remove the commit from your local branch. You can do this using the git reset
command. Here’s how:
git reset --hard HEAD~1
This command resets your current branch to the previous commit, effectively removing the latest commit from your local history. After executing this command, you’ll need to force push the changes to the remote repository:
git push origin HEAD --force
Output:
To <remote-repo-url>
+ <commit-id>...<new-commit-id> -> <branch-name> (forced update)
The first command resets your local branch, and the second command pushes those changes to the remote repository. Be cautious with the --force
option, as it can overwrite changes on the remote repository that others might have made. Always ensure you communicate with your team before using this command.
Deleting a Specific Commit
What if you need to remove a specific commit that isn’t the most recent? In this case, you can use the git revert
command to create a new commit that undoes the changes made by the specific commit you want to delete. This method is especially useful because it preserves the history of your project while effectively removing the unwanted changes. Here’s how:
First, identify the commit ID of the commit you want to delete. You can find this by using:
git log
Once you have the commit ID, you can revert it:
git revert <commit-id>
After reverting the commit, push the changes to the remote repository:
git push origin <branch-name>
Output:
[<branch-name> <new-commit-id>] Revert "<commit-message>"
1 file changed, 1 insertion(+), 1 deletion(-)
This command creates a new commit that undoes the changes made by the specified commit. You’ll notice that the history remains intact, which is great for tracking purposes. After pushing, your remote repository will reflect this change, and the unwanted commit will effectively be removed from the project.
Interactive Rebase to Delete Multiple Commits
If you need to delete multiple commits or make more complex changes, using an interactive rebase is your best bet. This method allows you to reorder, squash, or delete commits in your history. Here’s how to perform an interactive rebase:
Start by initiating the interactive rebase for the last few commits:
git rebase -i HEAD~n
Replace n
with the number of commits you want to review. This will open a text editor displaying a list of commits. You’ll see something like this:
pick <commit-id> Commit message 1
pick <commit-id> Commit message 2
pick <commit-id> Commit message 3
To delete a commit, simply change the word pick
to drop
next to the commit you want to remove. After saving and closing the editor, Git will reapply the remaining commits.
Finally, push your changes to the remote repository:
git push origin <branch-name> --force
Output:
To <remote-repo-url>
+ <commit-id>...<new-commit-id> -> <branch-name> (forced update)
Using interactive rebase is powerful but requires caution. Always ensure you have a backup of your branch before performing this operation, as it rewrites commit history.
Conclusion
Deleting a commit from a remote repository in Git is a task that can be accomplished in various ways, depending on your specific needs. Whether you are removing the latest commit, reverting a specific one, or using interactive rebase for multiple commits, understanding these methods will enhance your Git skills. Always remember to communicate with your team and ensure you are aware of the implications of force-pushing changes. With these tools at your disposal, you can maintain a clean and effective project history.
FAQ
-
Can I delete a commit without affecting others?
Yes, you can use thegit revert
command to create a new commit that undoes the changes without removing the commit from history. -
What happens if I force push after deleting a commit?
Force pushing overwrites the history on the remote repository, which can affect other collaborators. Always communicate before doing this. -
Is it safe to use
git reset --hard
?
Usinggit reset --hard
will permanently remove changes in your working directory. Ensure you have backups or are sure about the changes you want to discard. -
How can I see my commit history?
You can view your commit history by running thegit log
command in your terminal. -
What is the difference between
git reset
andgit revert
?
git reset
removes commits from history, whilegit revert
creates a new commit that undoes changes from a specific commit without altering history.
Abdul is a software engineer with an architect background and a passion for full-stack web development with eight years of professional experience in analysis, design, development, implementation, performance tuning, and implementation of business applications.
LinkedIn