Question

I have added lots of lines in lots of files in a project then I made a commit. Now I want to delete that commit and all changes with it. By that I mean I want to remove the commit and with it all changes of codes done with that commit.

Was it helpful?

Solution

You want to remove the last commit completely? Here you go, but be careful, you cannot undo this:

git reset HEAD^ --hard

If you want to be more careful, you can do in two steps:

git reset HEAD^     # reset HEAD to previous commit but without changing files
git status          # review the changes that were in the last commit
git diff --cached   # review the changes that were in the last commit
git reset --hard    # *really* undo

OTHER TIPS

The git reset method described by janos works fine, but completely obliterating a commit is rarely necessary and if you make it a habit it's easy to end up in a situation you can't handle.

Unless the reason to nuke the commit is because you accidentally committed a huge file or because there are legal restrictions, I'd suggest reverting the commit. This preserves the commit but deletes all its changes. In other words, the commit will still be in the history of the project (along with the commit that reverts it), but its changes will be gone.

git revert HEAD       # revert the currently checked out commit
git revert 123456abc  # revert the commit with id 123456abc
git revert master~    # revert the parent commit of the master branch

git reset --hard and other methods of rewriting the history of a branch are powerful and useful tools, but it's a good idea to learn its limitations and consequences.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top