Git Rollback(git reset) :
- The term reset stands for undoing changes. The git reset command is used to reset the changes.
- The git reset command has three core forms of invocation. These forms are as follows.
- (i) Soft
- (ii) Mixed
- (iii) Hard
- The term reset stands for undoing changes to mentioned specific commit from existing latest commit
- Let’s explain with example
(i) Creating project with 3 files with 3 commits
FILE NAME | COMMIT MESSAGE |
file-A | file-Commit-A |
file-B | file-Commit-B |
file-C | file-Commit-C |
(ii) Try git log –oneline to get commit hash
File name | Commit message | Commit hash |
file-C | file-Commit-C | 257b6f8 |
file-B | file-Commit-B | 327bce5 |
file-A | file-Commit-A | 0276631 |
(iii) git reset –soft :
Task : Reset changes to file-Commit-B commit from file-Commit-C commit
git reset –soft <file-Commit-B commit hash> |
git reset –soft 327bce5 |
Before :
After :
1 | 327bce5 (HEAD -> master) file-Commit-B | HEAD is pointed to old commit/previous/selected/mentioned commit file-Commit-B |
2 | Files :git status | Files not deleted But moved to staging area(file-C)Needs to commit, then we can make new commit |
3 | Get back(if we know commit hash possible) – file-Commit-C git reset –soft 257b6f8 | Head again points to file-Commit-C Files added to local repo (committed) As like previous one |
(iv) git reset –mixed :
Task : Reset changes to file-Commit-B commit from file-Commit-C commit
git reset –mixed <file-Commit-B commit hash> |
git reset –mixed 327bce5 |
Before :
After :
1 | 327bce5 (HEAD -> master) file-Commit-B | HEAD is pointed to old commit/previous/selected/mentioned commit file-Commit-B |
2 | Files :git status | Files not deleted But moved to Working Directory(file-C)Needs to add to staging area, then we can make new commit |
3 | Get back(if we know commit hash possible) – file-Commit-C git reset –mixed 257b6f8 | Head again points to file-Commit-C Files added to local repo (committed) As like previous one |
(v) git reset –hard :
Task : Reset changes to file-Commit-B commit from file-Commit-C commit
git reset –hard <file-Commit-B commit hash> |
git reset –hard 327bce5 |
Before :
After :
1 | 327bce5 (HEAD -> master) file-Commit-B | HEAD is pointed to old commit/previous/selected/mentioned commit file-Commit-B |
2 | Files :git status | Files deleted from project directory(file-C Deleted) Working Tree is Clean |
3 | Get back(if we know commit hash possible) – file-Commit-C git reset –hard 257b6f8 | Head again points to file-Commit-C Files added to local repo (committed) As like previous one |