Git
Version Control
Git Commit
Branch Management
Software Development

How do I delete a commit from a branch?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Understanding Git Commits and Branches

Before diving into how you can delete a commit from a branch in Git, it's essential to understand the basic structure and purpose of commits and branches. Git is a distributed version control system that allows multiple developers to work on a project simultaneously without overwriting each other's changes. A commit in Git is essentially a snapshot of your entire repository at a certain point in time. A branch is a separate line of development that allows you to work on different features or fixes in parallel.

Reasons to Delete a Commit

While Git is designed to keep all commits for a history of changes, there are instances where you might want to delete a commit:

  • Reverting Erroneous Commits: If a commit introduces bugs or unintended changes, removing it might be necessary.
  • Confidential Information: Sensitive data like passwords or keys accidentally committed need removal.
  • Tidying Up History: During branch development or feature creation, some intermediate commits might be irrelevant to the project's history.

Methods to Delete a Commit

There are several methods to delete a commit, each suited to different scenarios.

1. Amend a Commit

If you wish to modify the most recent commit, you can use the --amend flag with the git commit command:

bash
git commit --amend

This will open your default text editor, allowing you to change the commit message or make amendments to the staged changes.

2. Using git reset

git reset is a versatile command used to undo commits:

  • Soft Reset: Moves the HEAD reference back to a previous commit but leaves the working directory and index as they were. Files will still be staged.
bash
  git reset --soft HEAD~1
  • Mixed Reset (Default): Resets the index without changing the working directory.
bash
  git reset HEAD~1
  • Hard Reset: Completely wipes out the commit, as well as any changes in the working directory.
bash
  git reset --hard HEAD~1

Warning: A hard reset is irreversible once performed. Use it with caution.

3. Using git revert

Instead of deleting a commit, you can apply a new commit that reverts changes made by a specific commit:

bash
git revert <commit-hash>

This command creates a new commit that reverses the changes of the specified commit.

4. Using git rebase

For interactive history rewriting, git rebase is powerful:

bash
git rebase -i HEAD~n

Replace n with the number of commits you want to view. This opens an interactive view:

  • Change pick to drop next to the commit you wish to remove.
  • Save and close the editor to apply changes.

Note: Rewriting history with rebase affects only local branches. Ensure collaborators are aware of these changes.

Considerations

  • Remote Branches: If the commit has been pushed to a remote branch, post-modification need a force push (git push --force). This can disrupt others' work, so proceed carefully and communicate with team members.
  • Collaborative Environment: Notify team members before modifying shared history to avoid inconsistencies.

Summary Table

MethodUse CaseCommand Example
Amend a CommitModify most recent commitgit commit --amend
Soft ResetRemove commit from history, keep changesgit reset --soft HEAD&#126;1
Hard ResetCompletely remove commit and changesgit reset --hard HEAD&#126;1
Revert a CommitCreate a new commit to undo changesgit revert <commit-hash>
Interactive RebaseRemove/alter past commits, rewrite historygit rebase -i HEAD&#126;n

Conclusion

Deleting or modifying commits requires a solid understanding of Git's mechanics and the consequences of each command. Whether you're working locally or collaborating with a team, choose the method that aligns with your goals while maintaining the integrity of your project history. Always back up your work before making irreversible changes.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.