git change remote url

Easily Delete Last N Commits In Git

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 --hard 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.


Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments