Saturday, 20 July 2019

Git: Soft reset


Soft reset moves specific commits to staging area.

Syntax
git reset --soft {Commit_ID}

To understand soft reset, lets first try to experiment by creating a git repository.

Step 1: Create a directory ‘reset_demo’ and execute ‘git init .’ command from ‘reset_demo’ directory.
$mkdir reset_demo
$
$cd reset_demo/
$
$git init .
Initialized empty Git repository in /Users/krishna/Documents/TechnicalDocuments/git/HelloWorld/reset_demo/.git/


Step 2: Create firstFile.txt and commit the changes.
$touch firstFile.txt
$git add firstFile.txt 
$git commit -m "Adding firstFile.txt to the repository"
[master (root-commit) 3bb1754] Adding firstFile.txt to the repository
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 firstFile.txt


Execute the command ‘git log --oneline' to see the snapshot id.
$git log --oneline
3bb1754 (HEAD -> master) Adding firstFile.txt to the repository


Step 3: Create secondFile.txt and commit the changes.
$touch secondFile.txt
$git add secondFile.txt 
$git commit -m "Adding secondFile.txt"
[master 8bc8380] Adding secondFile.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 secondFile.txt


Step 4: Similarly add thirdFile.txt and commit, fourthFile.txt and commit the changes.
$touch thirdFile.txt
$git add thirdFile.txt 
$git commit -m "Adding thirdFile.txt"
[master 045c62d] Adding thirdFile.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 thirdFile.txt
$
$touch fourthFile.txt
$git add fourthFile.txt 
$git commit -m "Adding fourthFile.txt"
[master 66c5ec9] Adding fourthFile.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 fourthFile.txt


Step 5: Execute the command ‘git log --oneline' to see the snapshot ids.
$git log --oneline
66c5ec9 (HEAD -> master) Adding fourthFile.txt
045c62d Adding thirdFile.txt
8bc8380 Adding secondFile.txt
3bb1754 Adding firstFile.txt to the repository


Now there are four files in working directory.
$git log --oneline
66c5ec9 (HEAD -> master) Adding fourthFile.txt
045c62d Adding thirdFile.txt
8bc8380 Adding secondFile.txt
3bb1754 Adding firstFile.txt to the repository

Let’s reset the changes in second commit ‘8bc8380’ by executing below statement.


git reset --soft 8bc8380
$git reset --soft 8bc8380 
$
$git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

 new file:   fourthFile.txt
 new file:   thirdFile.txt

$
$tree
.
├── firstFile.txt
├── fourthFile.txt
├── secondFile.txt
└── thirdFile.txt

0 directories, 4 files
$
$git log --oneline
8bc8380 (HEAD -> master) Adding secondFile.txt
3bb1754 Adding firstFile.txt to the repository

As you see the above output, when I did soft reset, HEAD is pointing to second snapshot and all the commits after second snapshot are moved to staging area.


Let’s commit  secondFile.txt and thirdFile.txt files.
$git add fourthFile.txt 
$git add thirdFile.txt 
$
$git commit -m "Adding thirdFile.txt and fourthFile.txt"
[master 23b44ea] Adding thirdFile.txt and fourthFile.txt
 2 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 fourthFile.txt
 create mode 100644 thirdFile.txt
$
$git status
On branch master
nothing to commit, working tree clean

Execute ‘git log --oneline' to see the status of snaposhots.

$git log --oneline
23b44ea (HEAD -> master) Adding thirdFile.txt and fourthFile.txt
8bc8380 Adding secondFile.txt
3bb1754 Adding firstFile.txt to the repository
Let’s update firstFile.txt with below content and commit.


firstFile.txt
Adding first line

$git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

 modified:   firstFile.txt

no changes added to commit (use "git add" and/or "git commit -a")
$
$git add firstFile.txt 
$git commit -m "Adding some information to firstFile.txt"
[master b442822] Adding some information to firstFile.txt
 1 file changed, 1 insertion(+)
$
$git log --oneline
b442822 (HEAD -> master) Adding some information to firstFile.txt
23b44ea Adding thirdFile.txt and fourthFile.txt
8bc8380 Adding secondFile.txt
3bb1754 Adding firstFile.txt to the repository

Let’s re update firstFile.txt and secondFile.txt like below.


firstFile.txt
Adding first line
Adding second line


secondFile.txt
Adding first line


Commit firstFile.txt and secondFile.txt.
$git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

 modified:   firstFile.txt
 modified:   secondFile.txt

no changes added to commit (use "git add" and/or "git commit -a")
$
$git add .
$
$git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

 modified:   firstFile.txt
 modified:   secondFile.txt

$
$git commit -m "Updated firstFile.txt and secondFile.txt"
[master 25a60a4] Updated firstFile.txt and secondFile.txt
 2 files changed, 2 insertions(+)


$git log --oneline
25a60a4 (HEAD -> master) Updated firstFile.txt and secondFile.txt
b442822 Adding some information to firstFile.txt
23b44ea Adding thirdFile.txt and fourthFile.txt
8bc8380 Adding secondFile.txt
3bb1754 Adding firstFile.txt to the repository


As you see the above output, there are 5 snapshots exist now. Let’s soft reset to first snapshot ‘3bb1754’
$git reset --soft 3bb1754 
$
$git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

 modified:   firstFile.txt
 new file:   fourthFile.txt
 new file:   secondFile.txt
 new file:   thirdFile.txt

$
$cat firstFile.txt 
Adding first line
Adding second line
$
$cat secondFile.txt 
Adding first line 
$
$git log --oneline
3bb1754 (HEAD -> master) Adding firstFile.txt to the repository


Let’s add and commit the changes.

$git add .
$
$git commit -m "Soft reset to first snapshot"
[master e6b1edc] Soft reset to first snapshot
 4 files changed, 3 insertions(+)
 create mode 100644 fourthFile.txt
 create mode 100644 secondFile.txt
 create mode 100644 thirdFile.txt
$
$git status
On branch master
nothing to commit, working tree clean
$
$git log --oneline
e6b1edc (HEAD -> master) Soft reset to first snapshot
3bb1754 Adding firstFile.txt to the repository


Previous                                                    Next                                                    Home

No comments:

Post a Comment