How to remove the last N number of commits in git?

Are you looking for a way to remove 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 remove commits in Git using the following two commands:

  • git revert and
  • git reset

First, let’s discuss the difference between the two commands.

Difference between git revert and git reset

git revert

The “git revert” command allows us to undo the changes made in a specific commit, creating a new commit that reverses the changes. This means that the commit will still be present in the commit history, but the changes made in it will be undone.

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.

Now that we’ve covered the differences between the two commands, let’s dive into how to use them to remove commits from your repository.

Disclaimer

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

Remove commits using git revert

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

git revert 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 revert HEAD~2”. This command will create a new commit that undoes the changes made in the specified commit.

Remove 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 and permanently remove them 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.

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 and permanently remove them from the commit history.

It is important to note that both the “git revert” and “git reset” commands should be used with caution, as they can affect the commit history of your repository. If you have already pushed the commits to a remote repository, you will need to use the “git push” command with the “–force” option to overwrite the remote repository with the new commit history.

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.

Conclusion

In this blog post, we learn that the commands “git revert” and “git reset” are powerful tools for removing commits from a Git repository. We also learn to use the “git revert” command to undo the changes made in a specific commit, and the “git reset” command to permanently delete commits from the repository.

Leave a Reply

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x