DEV Community

Discussion on: How to undo the last commit

Collapse
 
saschadev profile image
Der Sascha

For a quick revert you can use git reset HEAD^1 --hard and the you can commit it with additional changes. I thing this "prevents" the revert prefix in the git history. What do you think about it?

Collapse
 
isabelcmdcosta profile image
Isabel Costa

Thanks for the suggestion. I used that before, but in the last problems where I had to undo the commits and then push the changes, that was the solution that worked for me. Also, I didn't mind having a commit dedicated to the revert (with the prefix) on the git history. I think it is a possible solution too.

Collapse
 
pshchelo profile image
Pavlo Shchelokovskyy

As long as the commit you are 'undoing' is local and is not part of any remote branch (you have not push-ed it yet), or if this is a one-dev pet project and you are sure nobody is 'consuming' your remote branch (and you can push to it with --force) - git reset is OK.

The thing is - it rewrites history. So if you'll make another commit on top of it but the 'undone' commit is pushed already, the history of your local branch and your remote tracking branch will diverge - and you'd have to push it either to another branch, or with --force, rewriting the remote branch. And if the remote branch is rewritten, anyone who had cloned it won't be able to pull from it any more that easily etc...

Thus the general advise is not to rewrite history of remote branches, and that's what git revert is for.

Funny side note - once seen a commit starting with a word Revert repeated 6 times o_O

Collapse
 
isabelcmdcosta profile image
Isabel Costa

Great explanation! In my case, I thought revert was better because I pushed the previous changes before.