There are situation when you mess up so bad you want to just go back and restart again. Fortunately If you are using Git then you can do it. We will be discussing ways in which git allows you to revert to previous commit
1.Git reset
Git rest moves your head to a specific commit. Given below are few example
// Move to previous commit and ignores any changes. Head points to the last commit in current branch git reset --hard HEAD
git reset
if used with hard destroys everything after the target commit. HEAD points to the target commit and every thing after it is lost
git reset
can also be used with --soft
option and --mixed
option as shown below
//moves head to specified commit.But unlike --hard you get to keep your files and they are staged git reset --soft <Commit id> //moves head to specified commit.But unlike --hard you get to keep your files and they are un staged git reset --mixed <Commit id>
2.Git revert
git revert
is a safe option where unlike git reset
you do not loose any git history. Git revert essentially picks the target commit inverts it and writes it in from of the current commit. Thus after revert you get additional commits instead of loosing some. Since loosing history can be a issue when the code in question is also proof of activity revert is a better choice most of the time
As you can see in image above git revert
does not destroy history rather it creates new one. After revert your project is same as it was on target commit.
3.Git checkout
Apart from reverting and resetting one might find checkout helpful specially in case where you need to branch out a feature flow from a specific commit.
git checkout -b <branch name> <commit id>
This will create a branch from specific commit and will allow you to carry out your parallel flow in new branch.
Hope this helps!!!