Sunday 21 July 2019

git: Mixed reset


It is the default reset option. It moves the changes back to working directory.

Syntax
git reset --mixed {snapshot_id}

To understand mixed 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.


firstFile.txt
Adding first line

$git add firstFile.txt 
$
$git commit -m "Adding firstFile.txt"
[master (root-commit) c35bb21] Adding firstFile.txt
 1 file changed, 1 insertion(+)
 create mode 100644 firstFile.txt
$
$git log --oneline
c35bb21 (HEAD -> master) Adding firstFile.txt

Step 2: Create secondFile.txt and commit.


secondFile.txt
Adding first line

$git add secondFile.txt 
$
$git commit -m "Adding secondFile.txt"
[master d7051a9] Adding secondFile.txt
 1 file changed, 1 insertion(+)
 create mode 100644 secondFile.txt
$
$git log --oneline
d7051a9 (HEAD -> master) Adding secondFile.txt
c35bb21 Adding firstFile.txt


Step 3: Create thirdFile.txt and commit.

thirdFile.txt
Adding first line

$git add thirdFile.txt 
$
$git commit -m "Adding thirdFile.txt"
[master 20ae9ac] Adding thirdFile.txt
 1 file changed, 1 insertion(+)
 create mode 100644 thirdFile.txt
$
$git log --oneline
20ae9ac (HEAD -> master) Adding thirdFile.txt
d7051a9 Adding secondFile.txt
c35bb21 Adding firstFile.txt

Step 4: Update firstFile.txt and thirdFile.txt like below.


firstFile.txt
Adding first line
Adding second line


thirdFile.txt
Adding first line
Adding second 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
 modified:   thirdFile.txt

no changes added to commit (use "git add" and/or "git commit -a")
$
$git add firstFile.txt 
$git add thirdFile.txt 
$
$git commit -m "Updated firstFile.txt and thirdFile.txt"
[master b2525f8] Updated firstFile.txt and thirdFile.txt
 2 files changed, 2 insertions(+)


Execute ‘git log --oneline’ to see the snapshots.
$git log --oneline
b2525f8 (HEAD -> master) Updated firstFile.txt and thirdFile.txt
20ae9ac Adding thirdFile.txt
d7051a9 Adding secondFile.txt
c35bb21 Adding firstFile.txt


Let’s do mixed reset to first snapshot ‘c35bb21’ by executing below command.
$git reset --mixed c35bb21
Unstaged changes after reset:
M firstFile.txt


Execute the command ‘git status’.
$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

Untracked files:
  (use "git add <file>..." to include in what will be committed)

 secondFile.txt
 thirdFile.txt

no changes added to commit (use "git add" and/or "git commit -a")

As you see the output, firstFile.txt is in staging area and in modified state. Whereas secondFile.txt and thirdFile.txt are untracked.


Let’s see the diff fir firstFile.txt.
$git diff firstFile.txt 
diff --git a/firstFile.txt b/firstFile.txt
index e96d44a..409ff86 100644
--- a/firstFile.txt
+++ b/firstFile.txt
@@ -1 +1,2 @@
 Adding first line
+Adding second line


Execute 'git log --oneline', you can see that HEAD is pointing to first snapshot.

$git log --oneline
c35bb21 (HEAD -> master) Adding firstFile.txt


Previous                                                    Next                                                    Home

No comments:

Post a Comment