SAGARFIVE

1. Introduction

2. Git Basics

3.  Git Terminology

4.  Git Branching

5.  Git - Github

6.  Git Add (Git staging)

7.  Git Commit

8.  Github Advanced

9.  Git Remote

7.10 Git reset (Move/Rollback to old commit)

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  NAMECOMMIT MESSAGE
file-Afile-Commit-A
file-Bfile-Commit-B
file-Cfile-Commit-C

(ii) Try git log –oneline to get commit hash

File nameCommit messageCommit hash
file-Cfile-Commit-C257b6f8
file-Bfile-Commit-B327bce5
file-Afile-Commit-A0276631

(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 :

Observations:

1327bce5 (HEAD -> master) file-Commit-BHEAD is pointed to old commit/previous/selected/mentioned commit file-Commit-B
2Files :git statusFiles not deleted
But moved to staging area
(file-C)Needs to commit, then we can make new commit
3Get 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 :

Observations:

1327bce5 (HEAD -> master) file-Commit-BHEAD is pointed to old commit/previous/selected/mentioned commit file-Commit-B
2Files :git statusFiles not deleted
But moved to Working Directory
(file-C)Needs to add to staging area, then we can make new commit
3Get 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 :

Observations:

1327bce5 (HEAD -> master) file-Commit-BHEAD is pointed to old commit/previous/selected/mentioned commit file-Commit-B
2Files :git statusFiles deleted from project directory(file-C Deleted)
Working Tree is Clean
3Get 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