How to Delete Local Commit in Git?

Deleting a local commit in Git can be done in a few ways, depending on whether the commit is the most recent one or if it is further back in the commit history. Here’s an article on how to delete a local commit in Git, tailored to whether you want to keep the changes made in the commit or discard them entirely:

Table of Content

  • Keeping the Changes (Soft Reset)
  • Discarding the Changes (Hard Reset)
  • Handling Pushed Commits
  • Deleting a Specific Commit (Using git reflog and cherry-pick)
  • Summary

Keeping the Changes (Soft Reset)

If you want to undo the commit but keep the changes in your working directory for further modification, you can use a soft reset:

git reset --soft HEAD~1
  • `HEAD~1` refers to the commit immediately before the latest one.
  • `–soft` moves the current branch back by one commit but leaves your files as they were before the commit. The changes from the commit will remain staged, allowing you to modify them as needed.

Discarding the Changes (Hard Reset)

If you want to completely remove the commit and all changes associated with it, you can perform a hard reset:

git reset --hard HEAD~1
  • `HEAD~1` again refers to the commit immediately before the latest one.
  • `–hard` resets your current branch’s HEAD to the previous commit and discards all changes in the working directory and staging area.

Note: Be cautious when using `–hard` as it will permanently delete your uncommitted changes. Always make sure to back up your work before performing a hard reset.

Handling Pushed Commits

If the commit has already been pushed to a remote repository, you will need to handle the remote history separately, typically involving a force push. This should be done with caution to avoid disrupting others’ work.

After performing the reset locally, force push to the remote repository:

git push origin HEAD --force

Warning: Force-pushing rewrites history on the remote branch, which can affect collaborators. Always communicate with your team before doing this.

Deleting a Specific Commit (Using git reflog and cherry-pick)

You can also use the reflog and cherry-pick commands to remove a specific commit.

  • Find the commit hash you want to revert to using git reflog:
git reflog
  • Reset to the commit just before the one you want to delete:
git reset --hard <commit_hash>
  • Cherry-pick the commits you want to keep:
git cherry-pick <commit_hash>

Summary

Soft Reset:

  git reset --soft HEAD~1
  • Keeps the changes in your working directory and staging area.

Hard Reset:

  git reset --hard HEAD~1
  • Discards all changes and resets your working directory and staging area.

git reset

Force Push (if commit was pushed):

  git push origin HEAD --force,
  • Rewrites remote history, should be used with caution.

By following these steps, you can effectively manage and delete local commits in your Git repository. Always be careful with commands that alter commit history, especially when working with remote repositories, to maintain a smooth workflow for your team.


Contact Us