Optimize Your CI/CD Pipeline
Get instant insights into your CI/CD performance and costs. Reduce build times by up to 45% and save on infrastructure costs.
Introduction
Deleting Git commits is one of the frequent operations that may be encountered in version control management. Whether it will be removing a recent commit or an older one, opportunities should be taken to understand how to make this commitment safely. Here, I take you through different scenarios for deleting commits either locally or from the remote repository.
So, I want you to pay close attention in order not to have issues in teamwork and project integrity as a whole. Let's get started with the required techniques.
Steps we will cover in this article:
Deleting Local Commits
Let's see how to remove local commits effectively, having a couple of scenarios: undoing the latest commit keeping the changes or traveling back in time to delete specific older commits.
Deleting the Most Recent Commit Without Losing Changes
If you want to undo your last commit but not lose the changes, here's what you run:
git reset --soft HEAD~1
This command moves the HEAD of your current branch back one commit but saves your work so that you can continue editing the files. You may now make any changes you want to the code. When you're happy, you just commit as normal.
Delete Old or Specific Commits
To remove a particular commit from your history, you will use interactive rebase. You will need to determine which commit you want to eliminate, first, using:
git log
Initiating rebase from the commit just before you want to delete:
git rebase -i HEAD~N
Remove or modify the line starting with the commit you want to drop in your editor. Once saved and closed, Git will go ahead and make the changes, skipping the commit you specified. Most probably during this process, there will be some conflicts. Resolve them and then continue the rebase using:
git rebase --continue
After that, if you have pushed your work remotely, make sure to force push your changes. Always inform your team before doing so!
Interactive Rebase to Delete Specific Commits
Now, in this section, let me walk you through the steps on how to use interactive rebase to remove certain commits from your history. That would be one of the effective ways to keep your commit history clean and readable.
Step 1: Identify the Commit
Well, first you need to identify the commit you want to delete. You do this with the command:
git log
That will list recent commits for you. Note the hash of the one you want to remove, and the commit just before it.
Step 2: Start Interactive Rebase
To start rebasing, run the command with the hash of that commit which was right before the one you wanted to remove:
git rebase -i HEAD~N
Replace N with the number of commits you want to look at. For example, to see the last four commits, you would enter HEAD~4
.
Step 3. Modify Commit list
The editor will open with all selected commits. Find the line with commit you want to drop and either delete that line or change pick
to drop
. Then save and close the editor to proceed.
Step 4: Resolve Conflicts
If you encounter conflicts, Git will stop the rebase. Resolve the conflicts and then run:
git rebase --continue
Repeat until all conflicts are resolved and the rebase finishes.
Step 5: Force Push (If Necessary)
If the commits that you changed were already pushed to some remote repository you might need to force push:
git push --force
Remember to be careful with this, since you might end up overwriting the commit history of a remote and messing up all your collaborators. Always coordinate with your team regarding these particular changes.
Handling Remote Repositories
Here, we will see how to delete commits from a remote repository and what precaution one must take before proceeding. The deletion of a commit from a remote repository is a potentially hazardous operation since it requires rewriting the Git history of the target branch. Therefore, be cautious and make sure you clearly communicate with your collaborators before running any command that might break the integrity of the project.
Deleting the Most Recent Commit
If you have pushed a commit to the remote repository and later wish to remove it, then do the following:
Then, delete the commit locally by doing:
git reset --hard HEAD~1
This command deletes the most recent commit from your local repository and discards the changes from that commit. When that is done, full pushing to the remote repository needs to be forced to update it:
git push origin <branch-name> --force
Replace <branch-name>
with the name of the branch on which you're working.
Removing Unwanted OLD or Specific Commits
To revert an older or any specific commit which has already been pushed to the remote:
- First follow the interactive rebase steps to remove the commit locally.
- Once the commit has been removed, rerun the force push command to push this update to the remote repository:
git push origin <branch-name> --force
Best Practices
Be aware that force pushing can overwrite the history on the remote and breaks your teammates' work. It is important that you have an open line with your team members when this kind of operation is performed on shared branches. If other people depend on the commit you are trying to delete, consider using git revert
instead: It makes a new commit that undoes changes instead of erasing history.
Using Git Revert for Safer Changes
To offer a safer alternative, I'll describe how you could use the git revert
command to create a commit that is the opposite of an earlier commit. This method keeps the history intact but effectively undoes changes-a perfect approach when in collaborative environments, if I may say so. Unlike git reset
, which moves the commit history, git revert
creates a new commit that undoes the changes of the anchor commit.
How to Use Git Revert
Find the Commit: Find the commit that you'd like to revert using:
git log
Note the hash of the commit to which you want to revert back.
Revert the Commit: Execute git revert
followed by the commit hash that you found:
git revert <commit-hash>
This command simply creates a new commit that effectively reverses the changes of the given commit.
Push the Changes: If you are collaborating and have a remote repository, don't forget to push changes:
git push origin <branch-name>
Be sure to let your team know that this is a change because it will add a new commit and not remove history.
Conclusion
In short, learning the ways of commit deletion can make you a much better version controller. Be careful, especially in a shared repository, not to disturb your fellows. Whether using git reset
for local commits or git push --force
for remote ones, it's important to understand what exactly each of these will do. Remember also to communicate with your team and, where possible, use tools like git revert
to preserve a smooth workflow.