- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Explain soft reset with an example in Git
Soft reset will move the HEAD pointer to the commit specified. This will not reset the staging area or the working directory.
Example
The diagram shows a file named File1.txt within the git repository. A, B, C and D represent lines that are added to the file. The diagram indicates that a commit is performed after adding each line A, B and C. c1 is the commit performed after adding line A, c2 is the commit after adding line B and C3 represents the commit after adding line C.
Now add line D. This change is available in the working directory and this change is staged but yet to be committed. Now let us perform a soft reset to make the HEAD of master branch point to the commit c1 commit. After the soft reset, HEAD will point to the commit c1 without changing anything in the staging area or the working area.
Step 1 − Create a repository and add File1.txt with content A. Commit the changes as shown in the below code snippet.
$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo $ git init Initialized empty Git repository in E:/tut_repo/.git/ $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ echo A>File1.txt $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ cat File1.txt A $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ git add . $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ git commit -m 'A' [master (root-commit) c2bd7e5] A 1 file changed, 1 insertion(+) create mode 100644 File1.txt
Step 2 − Perform 2 more commits with contents B and C as shown in the above diagram.
$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ echo B>>File1.txt $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ cat File1.txt A B $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ git add . $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ git commit -m 'B' [master 05cf92e] B 1 file changed, 1 insertion(+) $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ echo C>>File1.txt $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ cat File1.txt A B C $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ git add . $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ git commit -m 'C' [master 36361c2] C 1 file changed, 1 insertion(+) $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ git log --oneline 36361c2 (HEAD -> master) C 05cf92e B c2bd7e5 A
Step 3 − Make a change in the working directory to add content D and stage the changes.
$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ echo D>>File1.txt $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ cat File1.txt A B C D $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ git add . $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ git status -s M File1.txt
Step 4 − Now let’s perform a soft reset to make the HEAD point to the first commit. In other words, we have to move the head pointer two commits back. However, no changes will be done in the staging area and the working directory.
$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ git log --oneline 36361c2 (HEAD -> master) C 05cf92e B c2bd7e5 A $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ git reset --soft HEAD~2 $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ git log --oneline c2bd7e5 (HEAD -> master) A $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ git status -s M File1.txt $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ cat File1.txt A B C D $ git status On branch master Changes to be committed: (use "git restore --staged <file>..." to unstage) modified: File1.txt
As shown in the below diagram, when we perform a soft reset, Git simply moves the HEAD to the specified commit without impacting the Working and the Staging area. After performing the soft reset, our working directory will contain all four lines in the file. The staged change - Content D will also be intact in the Staging Area.