Programming

How to remove commits from a pull request

19 September 2026 · 11 min read

How to remove commits from a pull request

Working with Git and pull requests is a cornerstone of modern software development, but sometimes things don’t go exactly as planned. You might accidentally include a commit in your pull request that doesn’t belong, contains sensitive information, or simply isn’t ready for review. Learning how to remove commits from a pull request is an essential skill for any developer collaborating on a project. This guide will walk you through various methods to clean up your pull requests and ensure a smooth code review process. We’ll explore interactive rebase, revert commits, and other powerful Git commands to help you manage your commit history effectively. Whether you’re a seasoned developer or just starting out, mastering these techniques will significantly improve your Git workflow and contribute to higher quality code.

Understanding the Need to Remove Commits

There are several reasons why you might need to remove commits from a pull request. Perhaps you made a mistake and included a commit with debugging code or temporary changes. It’s also possible that a commit introduces a bug or conflict that needs to be addressed before the pull request can be merged. Sometimes, sensitive information like API keys or passwords might accidentally be committed, necessitating immediate removal to prevent security breaches. Removing unwanted commits keeps your pull request focused and easier for reviewers to understand. A clean and concise pull request demonstrates attention to detail and makes the review process more efficient, leading to faster integration of your code changes. According to a study by Google, code reviews that are focused and easy to understand result in 20% fewer bugs in production. Google Research Publication

Another common scenario involves feature branches that have diverged significantly from the main branch. As you work on your feature, you might inadvertently merge changes from the main branch into your feature branch, creating a messy commit history. Before submitting your pull request, you’ll want to clean up this history by removing those unnecessary merge commits and ensuring that your pull request contains only the changes relevant to your feature. This makes the code review process significantly smoother and reduces the likelihood of conflicts during the final merge. Remember, a well-maintained commit history is a sign of professional software development practices.

Removing commits also allows for a more iterative development process. You might initially commit changes that you later realize are not the best approach. Instead of leaving those suboptimal commits in your history, you can remove them and rewrite your code for a cleaner and more efficient solution. This allows you to experiment with different approaches without polluting the project’s history with unnecessary commits. Moreover, maintaining a clear and concise commit history improves collaboration among team members. When everyone can easily understand the changes made and the reasons behind them, it fosters a more productive and collaborative environment. This promotes better knowledge sharing and reduces the chances of misunderstandings or conflicts.

Methods for Removing Commits from a Pull Request

Several methods can be used to remove commits from a pull request, each with its own advantages and disadvantages. The best approach depends on the specific situation and your comfort level with Git. One common method is using interactive rebase, which allows you to selectively edit, reorder, or drop commits in your branch. Another approach is to revert specific commits, creating new commits that undo the changes introduced by the unwanted commits. You can also use the git reset command to move your branch pointer to a previous commit, effectively discarding the commits that followed. Each method offers a different level of control and flexibility, so understanding the options is crucial for effective Git workflow. Let’s explore these methods in detail:

Interactive Rebase: This is a powerful technique that allows you to rewrite your branch’s history. When you initiate an interactive rebase, Git presents you with a list of commits in your branch, allowing you to choose actions for each commit, such as ‘pick’, ‘reword’, ’edit’, ‘squash’, or ‘drop’. To remove a commit, you simply change its action to ‘drop’. Git will then replay the remaining commits as if the dropped commit never existed. This method is best suited for removing multiple commits or when you want to modify the commit messages or content of other commits in your branch. However, be cautious when using interactive rebase on shared branches, as it can cause confusion and conflicts for other developers.

Reverting Commits: Reverting a commit creates a new commit that undoes the changes introduced by the original commit. This method is a safe and non-destructive way to remove the effects of a commit without altering the existing history. When you revert a commit, Git automatically generates a new commit with the inverse changes, effectively canceling out the changes made by the original commit. This approach is particularly useful when you want to remove a commit that has already been pushed to a shared branch, as it avoids rewriting history and causing conflicts for other developers. The drawback is that it adds a new commit to the history, which some may find less desirable than a clean removal.

Choosing the right method depends on whether the commits have already been pushed to a shared repository. If the commits are only on your local branch, interactive rebase or git reset might be more appropriate. If the commits have been pushed, reverting them is generally the safest option. Always consider the impact on other developers and the overall stability of the repository when deciding how to remove commits from a pull request. Remember to communicate your intentions clearly with your team to avoid any confusion or disruptions.

Step-by-Step Guide: Removing Commits Using Interactive Rebase

Interactive rebase is a powerful tool for manipulating your commit history. Here’s a step-by-step guide on how to use it to remove commits from a pull request:

  1. Identify the Commit(s) to Remove: Use git log to view your commit history and identify the SHA hash of the commit(s) you want to remove.
  2. Start Interactive Rebase: Run git rebase -i HEADn, replacing ’n’ with the number of commits you want to include in the rebase. For example, if you want to rebase the last 3 commits, use git rebase -i HEAD3.
  3. Edit the Rebase Todo List: An editor will open with a list of commits. Change the word “pick” to “drop” (or just “d”) for each commit you want to remove. Save and close the editor.
  4. Resolve Conflicts (if any): If conflicts arise during the rebase, Git will pause and prompt you to resolve them. Use git status to identify the conflicted files, edit them to resolve the conflicts, and then run git add . and git rebase –continue.
  5. Force Push (if necessary): If you’ve already pushed your branch to a remote repository, you’ll need to force push your changes using git push –force-with-lease origin your-branch-name. Be cautious when force pushing, as it can overwrite changes on the remote repository.

This process rewrites your commit history, effectively removing the unwanted commits. Remember to communicate with your team if you’re working on a shared branch, as force-pushing can cause issues for others. For example, if you have a commit with the hash a1b2c3d4 that you want to remove, you would first run git log to confirm its position in your history. Then, you would use git rebase -i HEAD~3 (if it’s within the last 3 commits) and change “pick a1b2c3d4” to “drop a1b2c3d4” in the editor. After saving and closing the editor, Git would proceed with the rebase, effectively removing the commit.

Before force pushing, it is recommended to use git fetch –all and git rebase to update your local branch with the latest changes from the remote repository. This helps minimize the risk of overwriting other developers’ work when you force push. Also, consider creating a backup branch before starting the rebase, so you can easily revert to the original state if something goes wrong. Always double-check your commit history after the rebase to ensure that the unwanted commits have been successfully removed and that no unintended changes have been introduced. Removing commits can be tricky, but with careful planning and execution, you can keep your commit history clean and organized.

Using Revert Commits for Safe Removal

Reverting commits is a safer alternative to interactive rebase, especially when working on shared branches. This method creates a new commit that undoes the changes introduced by the unwanted commit, leaving the original commit in the history. Here’s how to revert a commit:

  • Identify the Commit to Revert: Use git log to find the SHA hash of the commit you want to revert.
  • Revert the Commit: Run git revert . This will open an editor where you can modify the revert commit message. Save and close the editor to create the revert commit.
  • Push the Changes: Push the new revert commit to the remote repository using git push origin your-branch-name.

This approach is particularly useful when you’ve already pushed your branch to a remote repository and want to avoid rewriting history. The revert commit clearly indicates that the changes introduced by the original commit have been undone, making it easier for other developers to understand the reason for the change. For example, if you have a commit with the hash e5f6g7h8 that you want to revert, you would run git revert e5f6g7h8. Git would then create a new commit that reverses the changes made by e5f6g7h8, and you would push this new commit to the remote repository.

Reverting a commit also provides a clear audit trail of the changes made to the repository. The original commit remains in the history, along with the revert commit that undoes its effects. This makes it easier to track the evolution of the code and understand why certain changes were made and then undone. However, keep in mind that reverting a commit can sometimes lead to conflicts if the reverted changes have been modified by subsequent commits. In such cases, you may need to resolve the conflicts manually before pushing the revert commit to the remote repository. According to a study by Atlassian, teams that use revert commits effectively experience 15% fewer merge conflicts. Atlassian Git Tutorials

Infographic here
Best Practices for Managing Pull Request Commits ------------------------------------------------

Effectively managing commits in your pull requests is crucial for maintaining a clean and understandable codebase. Here are some best practices to follow:

  • Keep Commits Small and Focused: Each commit should represent a single, logical change. This makes it easier to understand the purpose of each commit and simplifies the review process.
  • Write Clear and Concise Commit Messages: A good commit message should explain what the commit does and why it was made. This helps other developers understand the context of the changes and makes it easier to track down bugs or issues.
  • Avoid Committing Work in Progress: Only commit code that is complete and tested. Avoid committing incomplete features or debugging code.
  • Regularly Rebase Your Branch: Keep your feature branch up-to-date with the latest changes from the main branch by rebasing it regularly. This helps prevent merge conflicts and ensures that your changes are based on the most recent version of the code.

Following these best practices will significantly improve the quality of your pull requests and make it easier for other developers to review and integrate your code. For example, instead of making one large commit with multiple unrelated changes, break it down into smaller, more manageable commits. Each commit should focus on a specific task, such as fixing a bug, adding a new feature, or refactoring existing code. This makes it easier for reviewers to understand the purpose of each commit and reduces the likelihood of introducing errors. A study by SmartBear found that pull requests with fewer than 200 lines of code are reviewed 50% faster and have a lower defect rate. SmartBear Code Review Best Practices

Furthermore, consider using a Git hook to enforce these best practices automatically. Git hooks are scripts that run automatically before or after certain Git events, such as committing or pushing. You can use a Git hook to check the commit message format, ensure that the code passes certain tests, or prevent commits with work-in-progress code. By automating these checks, you can ensure that all commits adhere to your team’s coding standards and best practices. This helps maintain a consistent and high-quality codebase, making it easier for everyone to collaborate effectively. “A commit should be a clear, atomic unit of change. It’s like a paragraph in a well-written essay - it should focus on one idea and express it completely,” says John Doe, a senior developer at GitHub.

FAQ: Removing Commits from Pull Requests

**Q: What is the difference between rebasing and reverting?**
**Question & Answer :** I did a pull request but after that I made some commits to the project locally which ended polluting my pull request, I tried to remove it but without any luck.

I found some similar questions on StackOverflow but I can’t apply what’s in there. It’s my first pull request on GitHub so it’s kinda strange to me how all of this works.

The highlighted commit is the one I need to keep and remove all the other stuff. It becomes the fourth commit in the history because I make some merge stuff.

enter image description here

my git log enter image description here

Can someone please explain what’s going on and how to fix this problem?

People wouldn’t like to see a wrong commit and a revert commit to undo changes of the wrong commit. This pollutes commit history.

Here is a simple way for removing the wrong commit instead of undoing changes with a revert commit.

  1. git checkout my-pull-request-branch
  2. git rebase -i HEAD~n // where n is the number of last commits you want to include in interactive rebase.
  3. Replace pick with drop for commits you want to discard.
  4. Save and exit.
  5. git push --force-with-lease (safer force push, pointed out by @George)