Sometimes, you want to change some old commits or you want repo
to restore to that commit. You need to use “git reset” command
in those cases. Options of “git reset” are “—hard, --mixed
(default), --soft”.
Let’s create three files and commit it once by one. So we will
have below last three commits in our repo.
[varelite1@managed1 repo]$ git log
commit cfcecfa3f18fe91b7001b2bee1578e7e259a888f (HEAD -> master)
Author: varelite1 <varelit1@kb.com>
Date: Mon Jun 3 09:20:54 2024 +0530
v3
commit bd638b22a4dcd9ededaa7998d2b4f2a32b1426db
Author: varelite1 <varelit1@kb.com>
Date: Mon Jun 3 09:20:38 2024 +0530
v2
commit 00faba68c3470dd50d4f3892ef49fa02f329dac3
Author: varelite1 <varelit1@kb.com>
Date: Mon Jun 3 09:20:22 2024 +0530
v1
You can also see commits in one line using below command.
[varelite1@managed1 repo]$ git log --oneline
cfcecfa (HEAD -> master) v3
bd638b2 v2
00faba6 v1
a64a0aa Corrected the merge conflict
6cc74e8 code1 and code2 changed in master branch
c4e5ffd code1 and code2 changed
As we discussed, we have three types of reset. Let’s see a soft
reset first.
“git reset –soft <SHA1 hash>”
- Discard commit
- Keep changes in staged (index)
A soft rest to SHA1 hash will move the files or changes to
staged area so you need to just check and commit it. Let’s see
an example now. We will move the commit to v2 from above logs.
[varelite1@managed1 repo]$ git reset --soft bd638b22a4dcd9ededaa7998d2b4f2a32b1426db
[varelite1@managed1 repo]$ git status -s
A file3.txt
[varelite1@managed1 repo]$ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: file3.txt
[varelite1@managed1 repo]$ git log
commit bd638b22a4dcd9ededaa7998d2b4f2a32b1426db (HEAD -> master)
Author: varelite1 <varelit1@kb.com>
Date: Mon Jun 3 09:20:38 2024 +0530
v2
commit 00faba68c3470dd50d4f3892ef49fa02f329dac3
Author: varelite1 <varelit1@kb.com>
Date: Mon Jun 3 09:20:22 2024 +0530
v1
It is visible in logs that HEAD is now at v2 and files are in
staged. We can just commit it.
[varelite1@managed1 repo]$ git commit -m "v3"
[master a182017] v3
1 file changed, 1 insertion(+)
create mode 100644 file3.txt
[varelite1@managed1 repo]$ git status
On branch master
nothing to commit, working tree clean
Now let’s move to mixed one which is default one. Here only
difference between soft and mixed is that mixed reset unstaged
the changes and you need to again add files and commit it.
“git reset –mixed <SHA1 hash> (default)”
- Discard commit
- Discard changes in staging area (unstaged)
Let’s consider that we have added another file and we are at
v4. So we will reset it and see what we get.
[varelite1@managed1 repo]$ git reset --mixed 00faba68c3470dd50d4f3892ef49fa02f329dac3
[varelite1@managed1 repo]$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
file2.txt
file3.txt
file4.txt
nothing added to commit but untracked files present (use "git add" to track)
[varelite1@managed1 repo]$ git log
commit 00faba68c3470dd50d4f3892ef49fa02f329dac3 (HEAD -> master)
Author: varelite1 <varelit1@kb.com>
Date: Mon Jun 3 09:20:22 2024 +0530
v1
[varelite1@managed1 repo]$ git add .
[varelite1@managed1 repo]$ git commit -m "v4"
[master 98aede8] v4
3 files changed, 3 insertions(+)
create mode 100644 file2.txt
create mode 100644 file3.txt
create mode 100644 file4.txt
[varelite1@managed1 repo]$ git log
commit 98aede84318671e63ca43081fa71a97ff758643a (HEAD -> master)
Author: varelite1 <varelit1@kb.com>
Date: Mon Jun 3 09:49:24 2024 +0530
v4
Now we have the final one which is HARD reset.
“git reset –hard <SHA1 hash>”
- Discard commit
- Discard all changes
- Data lost
In this, git status will be okay and it means it has removed
everything and is most destructive option. You will see that
reset commit and all commits above that have been discarded.
Let’s consider we are at v4.
[varelite1@managed1 repo]$ git log
commit 98aede84318671e63ca43081fa71a97ff758643a (HEAD -> master)
Author: varelite1 <varelit1@kb.com>
Date: Mon Jun 3 09:49:24 2024 +0530
v4
commit 00faba68c3470dd50d4f3892ef49fa02f329dac3
Author: varelite1 <varelit1@kb.com>
Date: Mon Jun 3 09:20:22 2024 +0530
v1
Let’s reset it to v1.
[varelite1@managed1 repo]$ git reset --hard 00faba68c3470dd50d4f3892ef49fa02f329dac3
HEAD is now at 00faba6 v1
[varelite1@managed1 repo]$ git status
On branch master
nothing to commit, working tree clean
[varelite1@managed1 repo]$ git log
commit 00faba68c3470dd50d4f3892ef49fa02f329dac3 (HEAD -> master)
Author: varelite1 <varelit1@kb.com>
Date: Mon Jun 3 09:20:22 2024 +0530
v1
So you can see that we are at v1 and git status is clean. So we
have lost the commits and the data which are in v4.
Keep exploring. Continue reading on our blog.
Let's go through GIT