Sunday 21 July 2019

git: Hard reset: Move the changes to trash


Hard reset moves the changes to trash.

Syntax
git reset --hard {snapshot_id}

Let’s try to understand it by an example.

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.

firstFile.txt
Adding first line

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


Step 3: Add secondFile.txt and commit.

secondFile.txt
Adding first line

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

Step 4: Add thirdFile.txt and commit.

thirdFile.txt
Adding first line

$git add thirdFile.txt 
$
$git commit -m "Adding thirdFile.txt"
[master d4c0321] Adding thirdFile.txt
 1 file changed, 1 insertion(+)
 create mode 100644 thirdFile.txt
$
$git log --oneline
d4c0321 (HEAD -> master) Adding thirdFile.txt
53f2a95 Adding secondFile.txt
312d64b Adding firstFile.txt

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


firstFile.txt
Adding first line
Adding second line


thirdFile.txt
Adding first line
Adding second line


Commit the changes.
$git add firstFile.txt 
$git add thirdFile.txt 
$
$git commit -m "Updated firstFile.txt and thirdFile.txt"
[master 73d5195] Updated firstFile.txt and thirdFile.txt
 2 files changed, 2 insertions(+)

Execute 'git log --oneline' to see the snapshots.
$git log --oneline
73d5195 (HEAD -> master) Updated firstFile.txt and thirdFile.txt
d4c0321 Adding thirdFile.txt
53f2a95 Adding secondFile.txt
312d64b Adding firstFile.txt


When you see the files in current git repo, these are like below.
$tree
.
├── firstFile.txt
├── secondFile.txt
└── thirdFile.txt

0 directories, 3 files

Step 6: Let’s do hard reset to second snapshot ‘53f2a95’.

git reset --hard 53f2a95
$git reset --hard 53f2a95
HEAD is now at 53f2a95 Adding secondFile.txt


Just check the directory, you can observe that thirdFile.txt is removed from working directory.
$tree
.
├── firstFile.txt
└── secondFile.txt

0 directories, 2 files


Execute 'git log --oneline' to see all the snapshots.
$git log --oneline
53f2a95 (HEAD -> master) Adding secondFile.txt
312d64b Adding firstFile.txt


Open firstFile.txt, you can observe that the change done after the second snapshot are removed.

$cat firstFile.txt 
Adding first line

Previous                                                    Next                                                    Home

No comments:

Post a Comment