Friday, 28 December 2018

GIT: How can I delete a commit in Git?

Git offers a couple of tools to delete (or undo) older commits. The first step, therefore, is to find out which tool is best for your scenario.

Restoring an old version of a project

Do you want to restore an older revision of your project - just as it was at a certain point in time?
In that case, the "git reset" command is what best suits your needs. Read more in our detailed post about how to reset to a previous revision.

Undoing only a specific commit

A different scenario is when you want to revert the effects of a certain commit - without deleting any commits that came afterwards.
This is a case for the "git revert" command. Interestingly, the command doesn't deleteanything; it creates a new commit that introduces changes which revert the effects of the specified commit. Our post explains the details of how to go about reverting commits.

Delete or change specific commits

Another use case might be to delete a commit "in the middle" of your history, without resetting your whole project to a previous revision.
In that case, we'll have to bring out the big guns: Git's "Interactive Rebase" tool is what we need here. Please note that this is not only the most powerful, but also the most dangerous of the three commands listed here. This is because it allows you to change your commit history quite drastically - which is a dangerous process.
Read more about Interactive Rebase, including what to watch out for.

0 comments:

Post a Comment