CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > How to > Easily Delete Last N Commits In Git

Easily Delete Last N Commits In Git

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Published: August 31, 2020 ยท 2 min read ยท 0 Comments
Share: X in ๐Ÿ”—
git change remote url

Are you looking for a way to delete the last N number of commits from your Git repository? If yes then you’ve come to the right place! In this blog post, I’ll show you how you can easily delete last n commit in Git using the command git reset

The “git reset” command allows us to discard commits, moving the branch pointer to a previous commit and destroying the commits in the process. This means that the specified commits will be permanently deleted from the repository.

Disclaimer

It is always suggested to use this command with caution to avoid affecting the commit history of your repository.

Delete commits using git reset

To delete the last N commits using the “git reset” command, you can use the following command:

git reset HEAD~[N]

Replace “[N]” with the number of commits you want to remove, starting from the last commit. For example, to remove the last two commits, you would use “git reset HEAD~2”. This command will delete the specified commits permanently from the commit history.

Example:

git reset HEAD~2

In the above command, 2 is the number of the last commits you want to remove. You can increase or decrease this value as per your need.

Note

While pushing to remote branch if you get any error you can use -f along with the push command. Eg: git push -f origin branch_name

If you want to do the same for multiple branches then you can do it manually for other branches as well.

Related Posts:

  • How to Delete Last Commit Git: A Step-by-Step Guide
  • Print Git Branch Name in Jenkins Pipeline
  • How To Print Git Commit Message In Jenkins Pipeline
  • MySQL Commands for Developers
  • Edit last commit message in Git
  • Scrollable ResultSet in JDBC
Tags:git
Was this article helpful?
โ† Previous ArticleHow to Delete Last Commit Git: A Step-by-Step Guide
Next Article โ†’Clean Up Jenkins Home Directory

Leave a Comment Cancel reply

You must be logged in to post a comment.

Recent Posts

  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
  • Complete Guide to JaCoCo: How to Measure Java Code Coverage Accurately
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
ยฉ 2026 CoderSathi. All rights reserved. Privacy Policy ยท Sitemap