Explain mixed reset with an example in Git


Mixed reset will move the HEAD pointer to the commit specified. This is the default reset option in git. It also copies content of the commit snapshot to the staging area and not to the working directory. This will lead to overwriting the staging area. The working directory contents will be safe in this case. Let us understand this through an example.

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, if we perform a mixed reset the HEAD of the master branch to point to c1 commit then we will not lose the changes in the working directory. In our case the line with content D will remain intact in the working directory. But if the content D is staged, then the staged content will be lost as mixed mode replaces the staging area while keeping the working directory intact.

Step 1 − Create a repository add File1.txt with content- A, commit the changes as shown

$ 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) f12b5e8] A
1 file changed, 1 insertion(+)
create mode 100644 File1.txt

Step 2 − Do 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 a704cbd] 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 31aa99f] c
1 file changed, 1 insertion(+)

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master)
$ git log --oneline
31aa99f (HEAD -> master) c
a704cbd B
f12b5e8 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 mixed reset the HEAD two commits back HEAD~2, which is our first commit. When we do this type of reset git will replace the staging area only. So, the current changes in the file present in the working directory is unaffected. The file will contain all four lines with contents A,B,C,D

$ git reset --mixed HEAD~2
Unstaged changes after reset:
M File1.txt

$ 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 diff
diff --git a/File1.txt b/File1.txt
index f70f10e..b9b94a9 100644
--- a/File1.txt
+++ b/File1.txt
@@ -1 +1,4 @@
A
+B
+c
+D

After performing a mixed reset, the changes performed in the working directory are intact. The file will contain all four lines. However, the changes in staging area will be replaced by the snapshot of the contents of the first commit - Content A.

Updated on: 30-Apr-2021

179 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements