Olson CloudWorks 🚀

How can I delete the last n commits on GitHub and locally

September 19, 2026

📂 Categories: Programming
How can I delete the last n commits on GitHub and locally

Have you ever found yourself in a situation where you’ve made a series of commits to your Git repository, only to realize that some of those commits were a mistake? Perhaps you committed sensitive information, introduced a bug, or simply want to rewrite history for a cleaner project. Knowing how to delete the last n commits on GitHub and locally is a valuable skill for any developer using version control. This guide will walk you through the necessary steps to effectively and safely remove those unwanted commits, ensuring your repository remains clean and functional. We’ll cover both local and remote repositories, and also discuss the potential risks and best practices involved in rewriting Git history. Deleting commits requires caution, especially when collaborating with others, but with the right knowledge, it can be a powerful tool for maintaining a healthy project.

Understanding the Risks Before Deleting Commits

Before diving into the process of deleting commits, it’s crucial to understand the potential consequences. Rewriting history, especially on a shared repository, can lead to significant issues for collaborators. If others have already pulled your changes, force-pushing your altered history can create divergence and conflicts, making it difficult for them to integrate their work. According to Git documentation, force-pushing should be avoided whenever possible in collaborative environments. However, in situations where you’re the sole contributor or have a clear agreement with your team, rewriting history can be a viable option. Always communicate clearly with your team before making any changes to shared history.

Consider the impact on automated processes. If you’re using Continuous Integration/Continuous Deployment (CI/CD) pipelines, deleting commits that have already triggered builds or deployments can lead to unexpected results. Ensure your CI/CD configuration is aware of the changes you’re making and can handle potential rollbacks or redeployments. Additionally, if you’re deleting commits that contain security fixes, make sure those fixes are reapplied in a new commit to prevent vulnerabilities. It’s often best to create a new commit that reverts the changes introduced by the problematic commits, providing a clear and traceable history of the correction. This approach is often safer than deleting commits outright, especially in collaborative settings.

Deleting commits on GitHub also affects pull requests. If a pull request is based on a branch that has had its history rewritten, the pull request may become difficult to merge or may contain conflicts. Review your open pull requests after deleting commits to ensure they are still valid and can be merged correctly. The key takeaway here is to proceed with caution and fully understand the implications before altering the commit history. Weigh the benefits of deleting commits against the potential disruption it can cause. “With great power comes great responsibility,” as they say, and that certainly applies to rewriting Git history.

Deleting the Last N Commits Locally

Deleting the last n commits locally involves using Git’s interactive rebase feature. This powerful tool allows you to manipulate your commit history in various ways, including deleting, editing, and reordering commits. The process begins with identifying the commit you want to revert to. If you want to delete the last three commits, you’ll need to find the commit before those three. Once you have that commit’s hash or reference (e.g., HEAD~3), you can initiate the interactive rebase. This process ensures that your local repository reflects the desired state before you push any changes to the remote repository.

To initiate the interactive rebase, use the following command in your terminal: git rebase -i HEADn, where n is the number of commits you want to go back. For instance, to delete the last three commits, you would use git rebase -i HEAD3. This command will open your default text editor with a list of the commits you’re about to rebase. Each commit will be prefixed with the word “pick.” To delete a commit, simply change “pick” to “drop” (or just “d”). Save and close the editor, and Git will automatically remove the specified commits from your local history. This is where you can specify which commits to remove, edit, or squash as needed.

After the rebase is complete, double-check your local repository to ensure the correct commits have been removed. You can use the git log command to view your commit history and verify that the unwanted commits are no longer present. If you made a mistake during the rebase, you can use git rebase –abort to return to the state before the rebase. Once you’re satisfied with the changes, you’re ready to update your remote repository. Remember that this step requires a force push, which we’ll discuss in the next section. Here’s a featured snippet-optimized paragraph: To delete the last n commits locally in Git, use the command git rebase -i HEAD~n, replacing ’n’ with the number of commits to remove. In the interactive rebase editor, change “pick” to “drop” for each commit you want to delete, then save and close the file.

Updating GitHub with the Changes (Force Push)

After successfully deleting the commits locally, you need to update your GitHub repository to reflect the changes. This requires a force push, which overwrites the remote history with your local history. As mentioned earlier, force-pushing can be disruptive to collaborators, so exercise caution. Before force-pushing, ensure that you have communicated with your team and that everyone is aware of the changes you’re making. It’s also a good idea to take a backup of your remote branch before force-pushing, just in case something goes wrong. This can be achieved by creating a new branch from the current state of the remote branch.

To force-push your changes, use the command git push origin +<branch_name>, where <branch_name> is the name of the branch you’re pushing to (e.g., main or develop). The + symbol before the branch name indicates that you’re forcing the push. Alternatively, you can use the –force flag: git push origin <branch_name> –force. Both commands achieve the same result. After executing the force push, your remote repository will be updated with your local history, and the deleted commits will no longer be present. Verify the changes on GitHub to ensure everything is as expected.</branch_name></branch_name></branch_name>

Keep in mind that force-pushing can cause issues for anyone who has already cloned or pulled the branch you’ve modified. They will need to rebase their local branch onto the updated remote branch to avoid conflicts. This can be done using the command git pull –rebase origin <branch_name>. Communicate these steps to your collaborators to minimize disruption. Force-pushing is a powerful tool, but it should be used judiciously and with careful consideration of its potential impact. Always prioritize communication and collaboration to avoid unintended consequences. According to Stack Overflow, using git push –force-with-lease is a safer alternative to –force, as it prevents overwriting remote changes you haven’t seen. Best practices suggest exploring alternatives like revert commits before resorting to force-pushing.</branch_name>

Alternatives to Deleting Commits

While deleting commits might seem like the most straightforward solution, there are often safer and more collaborative alternatives. One such alternative is using git revert. The git revert command creates a new commit that undoes the changes introduced by a specific commit. This approach has several advantages over deleting commits. First, it preserves the original commit history, providing a clear and auditable record of all changes. Second, it avoids the need for force-pushing, minimizing disruption to collaborators. Third, it’s easily reversible; if you need to undo the revert, you can simply revert the revert commit.

To revert a commit, use the command git revert <commit_hash>, where <commit_hash> is the hash of the commit you want to undo. Git will automatically create a new commit with the reversed changes. You can then push this commit to the remote repository without needing to force-push. This approach is particularly useful when dealing with sensitive information or bugs that have already been deployed to production. Instead of rewriting history and potentially causing conflicts, you can simply revert the problematic commit and deploy the fix in a new commit. This ensures a smooth and traceable rollback process. Git revert is also generally a much safer option if other developers have based their work on the commits you want to remove.</commit_hash></commit_hash>

Another alternative is to use the “squash” option during an interactive rebase. Squashing allows you to combine multiple commits into a single commit, creating a cleaner and more concise history. This is useful when you have a series of small, related commits that could be more easily understood as a single unit. Squashing doesn’t delete the original commits, but it effectively hides them from the main history, providing a more streamlined view. Alternatives like these are crucial to understand when considering how to delete the last n commits on GitHub and locally. For more information on these alternatives, refer to the official Git documentation [^1^], Atlassian’s Git tutorials [^2^], and GitHub’s guides [^3^].

  • Consider using git revert to undo changes instead of deleting commits.
  • Squash commits to create a cleaner history without rewriting it entirely.
Infographic here
1. Identify the commit before the ones you want to delete. 2. Run `git rebase -i HEAD~n` (replace 'n' with the number of commits). 3. Change "pick" to "drop" for unwanted commits in the editor. 4. Save the file and verify the changes locally. 5. If necessary, force-push to update the remote repository (use with caution).
  • Always communicate with your team before rewriting shared history.
  • Back up your branch before force-pushing to avoid data loss.

FAQ

What happens if someone has already pulled the commits I want to delete?
If someone has already pulled the commits, force-pushing will cause them to have a divergent history. They will need to rebase their local branch onto the updated remote branch using `git pull --rebase origin `.
Is it safe to delete commits on the main branch?
Deleting commits on the main branch is generally not recommended, especially in collaborative environments. It's safer to use `git revert` to undo changes and avoid disrupting other developers.
How can I avoid accidentally committing sensitive information?
Use a `.gitignore` file to exclude sensitive files from being tracked by Git. Consider using tools like `git-secrets` to prevent committing secrets in the first place.
Deleting commits can be a useful technique for cleaning up your Git history, but it's essential to weigh the risks and benefits carefully. Understanding the potential consequences of rewriting history, especially in collaborative environments, is crucial for maintaining a healthy and productive workflow. By considering alternatives like `git revert` and squashing commits, you can often achieve the desired result without disrupting your team or jeopardizing your project. Remember to always communicate clearly with your collaborators and back up your work before making any significant changes to your Git history.

Now that you’re equipped with the knowledge of how to delete the last n commits on GitHub and locally, and understand the associated risks and alternatives, you can confidently manage your Git repository. Why not explore other advanced Git techniques, such as using Git bisect to find the commit that introduced a bug, or mastering Git submodules for managing dependencies? Keeping your Git skills sharp is an investment in your productivity and the health of your projects. Happy coding!

[^1^]: Git Documentation: [https://git-scm.com/docs](https://git-scm.com/docs) [^2^]: Atlassian Git Tutorials: [https://www.atlassian.com/git/tutorials](https://www.atlassian.com/git/tutorials) [^3^]: GitHub Guides: [https://guides.github.com/](https://guides.github.com/) Question & Answer :
I’m trying to delete the last two commits from one of my GitHub repositories. I’ve tried as suggested here: git push -f origin HEAD^^:master. It seems that it works, as the last two commits are removed.

Then I deleted them from my local repository with git rebase -i HEAD~2. I remove the lines that are related to those commits, and check with git log that they are correctly removed.

After that, I make some changes in my local repository, make a new commit, and push to GitHub. The problem is that, in my GitHub account, I have the previous two commits that I’ve tried to delete.

I think the problem is in my local repository, because if I clone my GitHub repository to my local and make some changes here, when I push a new commit those old commits aren’t pushed to GitHub.

To remove the last two commits locally I’d suggest using:

git reset --hard HEAD^^ 

Rebase is a completely different operation that won’t help you here.