13. Git Reset: Comprehensive Guide (Part - 2)

13. Git Reset: Comprehensive Guide (Part - 2)

Utility 2: Undo Commits At The Repository Level

We can use the option reset to undo commits at Repository Level.

Syntax: git reset

Let’s consider there are 5 Commits in our Local Repository.

Scenario 1: Discard/Ignore the Master

If we want to Discard/Ignore the Master i.e. the Latest Commit, then we have to make the Second Last Commit as the Master. Then only we can Discard/Ignore the Master. So, to Discard/Ignore the Latest Commit from the Repository we’ll use the command: git reset <Option mode> <Commit Id>

Commit Id: Whatever Commit Id that we will put in the command, that Commit Id will become HEAD. And the previous HEAD or whatever Commits will be there after that Commit Id will be removed from the Repository.

This means if we specify the Commit Id of the 2nd Last commit and then the commits after that (Previous HEAD) will be Ignored.

So, in this case, HEAD will be moved to the specific commit, and all the remaining recent commits will be deleted from the Repository.

For the commits, some files will be there in the Working Directory and also in the Staging Area. i.e. related to the commits some changes are there in the Working Directory and Staging Area.

So, if we want we can delete those corresponding files from the Working Directory and Staging Area. So, this thing is decided by the mode used in the command to discard the commit.

Mode: Mode will be going to decide whether the corresponding changes in the Working Directory and Staging Area to the discarded commits should be deleted or not.

Scenario 2: Discard / Ignore Only A Specific / Random Commit

Assume that we have Multiple Commits, and in that all the Commits if want to discard only a specific then it is not possible.

Various types of modes available :

So, the allowed values for the mode are as follows:

--mixed
--soft
--hard
--keep
--merge etc

-- mixed reset mode

It is the default mode, i.e. if we are not going to decide any mode then by default --mixed reset mode will be considered. Therefore the syntax for the command can be either of the two:

git reset --mixed commit Id

git reset commit Id

The mode is used to discard/remove commits from the both Repository and the Staging Area.

It means if any changes are there, so discard commits from the Remote/Local Repository and if any changes are there because of those discarded commits in the Staging Area, those changes also will be removed.

We can revert back the discarded commits back because the changes are available in the Working Directory.

Working:

Create a folder “project7” and move inside that folder.

We have to Initialize an Empty Local Repository for the Workspace then only the Version Control will be applicable.

We’ll create three files and write something in them. We’ll use the command echo for that.

Example:

echo "First line in a.txt" > a.txt

echo "First line in b.txt" > b.txt

echo "First line in c.txt" > c.txt

Now, we want to add these files to the Repository but in different commits, because to check the functionality of the reset command, multiple commits are required.

Therefore we’ll commit the files one by one.

First, we’ll add the file “a.txt” to the Staging Area.

After that commit those changes i.e. the file “a.txt” to the Local Repository.

we’ll add the file “b.txt” to the Staging Area.

After that commit those changes i.e. the file “b.txt” to the Local Repository.

We’ll add the file “c.txt” to the Staging Area.

After that commit those changes i.e. the file “c.txt” to the Local Repository.

Now, there are 3 Commits present in the Local Repository.

To confirm we can use the command git log --oneline

To delete and Revert the Last / Latest Commit

If we have to delete the Last / Latest Commit from the Repository then we’ll use the command git reset --mixed <2nd last Commit Id>

Example: In our case, we’ll use the command:

git reset --mixed fad54a3

After performing this operation the Commit with the Commit Id “fad54a3” i.e. the 2nd last Commit will become the HEAD. So, after that, whichever commit will be there, it’ll be removed.

Before that**,** we can check the number of files in the Working Directory and the Staging Area.

So, there are 3 files in both areas i.e. Working Directory and the Staging Area.

Now, delete the Last/Latest Commit from the Repository.

Now, there will be two commits available in the Repository. And the 2nd last Commit will become the HEAD.

The changes will also be deleted from the Staging Area as well but not from the Working Directory. To confirm we'll check the number of files as well as which files are available in the Staging Area. So, for that, we’ll use the command git ls-files. And to check the number of files as well as which files are available in the Working Directory we’ll use the command ls.

So, the file “c.txt” has been discarded/removed from the Staging Area. But all three files are present in the Working Directory.

To revert i.e. to add the discarded/removed files from the Repository and the Working Directory, then again we’ll have to add the discarded file (changes) to the Staging Area and then Commit again.

Add file “c.txt” to the Staging Area.

After that commit those changes i.e. the file “c.txt” to the Local Repository.

Now check the log of Git again using the command git log --oneline

Now, there will be 3 Commits again, along with the different Commit IDs.

--soft reset mode

It is the same as the --mixed mode except that it won’t touch the Working directory and the Staging Area.

Working:

Check the number of files in the Working directory and the Staging Area. We’ll use the command git ls-files to check the number of files as well as which files are available in the Staging Area. And to check the number of files as well as which files are available in the Working Directory we’ll use the command ls.

In both areas, we are having 3 files.

Now, Check the log of Git.

Delete the Last Two Recent Commits. So, for that, we’ll use the command git reset --soft HEAD~2

Now, check the log of Git.

There will be only one commit left in the Repository. And nothing will happen to the files in the Working directory and the Staging Area.

Check the number of files in the Working directory and the Staging Area. We’ll use the command git ls-files to check the number of files as well as which files are available in the Staging Area. And to check the number of files as well as which files are available in the Working Directory we’ll use the command ls.

--hard reset mode

It is the same as the --mixed mode except that changes will be removed permanently from everywhere (Local Repository, Staging Area and Working Directory).

This command is Dangerous as well as Destructive.

It is impossible to revert the changes and hence while using this command we have to take a bit of special care.

Working:

Create a file “d.txt” and write something in it.

Add the file “d.txt” to the Staging Area.

Now, commit the file to the Local Repository.

Now check the log of the Git

There will be three commits now in the Local Repository.

Check the number of files in the Working directory and the Staging Area. We’ll use the command git ls-files to check the number of files as well as which files are available in the Staging Area. And to check the number of files as well as which files are available in the Working Directory we’ll use the command ls.

There will be 4 files in both the Working directory and the Staging Area.

Now, to permanently delete the commit as well as the corresponding files in all the areas we’ll use the command

git reset --hard <commit Id>

The commits after that Commit Id which we have provided will be deleted permanently. And the Commit Id that we have provided in the command will become the HEAD.

Now, check the log of the git. The commit Id that we have provided in the command has become the HEAD.

Now, Check the number of files in the Working directory and the Staging Area. We’ll use the command git ls-files to check the number of files as well as which files are available in the Staging Area. And to check the number of files as well as which files are available in the Working Directory we’ll use the command ls.

Now, there will be only one file “a.txt” left in both the Working directory and the Staging Area. The other remaining files will be removed permanently.