How do I Delete Unpushed Git Commits?

Sometimes you might make mistakes in your Git commits and want to start fresh. Here’s how to remove unpushed commits in Git, focusing on two clear methods.

Table of Content

  • Using Soft Reset
  • Using Hard Reset
  • Discard Unpushed Commits Completely
  • Preserve Changes from Unpushed Commits

Using Soft Reset

  • Use this if you want to remove the commit itself but keep the changes you made.
  • Run `git reset –soft HEAD~<number>`, where `<number>` is the number of commits to removeD
git reset --soft HEAD~<number>

git reset –soft HEAD~2 , removes the last 2 commits

  • This removes the commits from your history, but the changes stay in your working directory. You can then stage them again and make a new commit with a better message.

Using Hard Reset

  • This removes the unwanted commits and also erases any changes introduced in those commits from your working directory.
  • Run `git reset –hard HEAD~<number>`, similar to the soft reset but with `–hard`.
git reset --hard HEAD~<number>

removes the last 2 commits permanently

  • This is permanent! Make sure you don’t need the commits or changes before using it.

Remember:

  • These commands only affect your local files, not anything pushed to a remote server.
  • Use `git log` to see your commit history if you’re unsure about the number of commits to remove.

By following these steps, you can effectively clean up your local Git history. Just remember to be cautious, especially with the hard reset!

Deleting unpushed Git commits involves different approaches based on your specific requirements. Here are two common scenarios and the corresponding solutions:

Discard Unpushed Commits Completely

If you want to completely remove unpushed commits from your local repository history, including any changes they introduced, you can use the git reset command.

  • Reset to a Specific Commit: Use git log to identify the commit you want to reset to. Then, use the following command to reset the branch to that commit, discarding all commits after it:
git reset --hard <commit_hash>

Replace <commit_hash> with the hash of the commit you want to reset to.

  • Force Push: After resetting, if you’ve already pushed the commits you want to remove, you’ll need to force push to update the remote repository:
git push origin <branch_name> --force

Replace <branch_name> with the name of your branch.

Preserve Changes from Unpushed Commits

If you want to keep the changes introduced by unpushed commits but remove them from the commit history, you can use git reset followed by git stash:

  • Stash Unpushed Changes: Use git stash to temporarily store the changes introduced by unpushed commits:
git stash
  • Reset Branch: Use git reset to reset the branch to a previous commit, discarding unpushed commits:
git reset --hard <commit_hash>
  • Apply Stashed Changes: Apply the stashed changes to restore the modifications:
git stash apply

Remember to review the changes applied by the stash to ensure they are as expected.

Note: Be cautious when using git reset --hard as it removes changes irreversibly. Only perform these actions if you’re certain you want to discard the unpushed commits. Additionally, if you’re collaborating with others, avoid force-pushing changes that may affect their work without proper communication.


Contact Us