How to revert the last commit in sourcetree

No matter how good you are in git/sourcetree, Sooner or Later you will mistype the commit message or Commit the changes to the wrong branch. There are many situations where you want to revert/undo the last commit changes.

In most cases, user commit's changes to the wrong branch and later they realize the issue. There is one reset the change to undo commit changes in git/sourcetree. To resolve that you can revert commit in git/sourcetree.

How to revert the last commit in git/sourcetree

There are 5 main Git reset modes soft, hard, mixed, merged and keep. Amongst them, the most useful git reset command is soft and hard.

Soft Reset:
git reset --soft HEAD~1
To revert the last commit from the working branch/feature, use the command "$ git reset --soft HEAD~1". After execution of this command, you will find last committed changes as uncommitted modifications in your working branch. This changes you can recommit with the modification you want. --soft command will reset Head only, No changes in Index.

Hard Reset:
git reset --hard HEAD~1
If you use "$ git reset --hard HEAD~1" command, It will discard last committed changes and uncommitted changes available in the current branch. In case you accidentally execute this command, you can not recover/undo it back again.

Note: "git reset" command does not actually remove/delete the commit, it just removes the reference pointing to it. You can recover reverted commit by finding SHA-1 key using command "git reflog".

How to revert multiple commits:

git reset --hard 1be6b8a7
Be careful before using this command. You can also revert multiple revision using git reset command. Command undoes all the commits that came after the one you returned to.

Post a Comment

0 Comments