Introduction

Today I am here to tell you more about GIT. As you all know while we are working in a project and sometimes we need the previous file. So we need to undo the actions. So We will see how to undo the actions or changes we made. We do commit mistakes or we needed the previous file. Undo command will help us do this job.

We will Undo changes in the “working directory, staging index and also in the repository tree”. Let us see how GIT works. This three tree architecture is needed to perform our tasks while working in the project.

Undo Files In The Working Directory

If you are working in a project and you accidentally deleted a part of you file. Now you need that deleted part so git will help us do that thing. We deleted the part of the file inside our local machine or you can say working directory. Take a look on the below picture:

GIT status

Undo file

In the above picture I made some changes in my file. I tells you that changes are not staged to commit. As you all know about git status command. This command shows that there is a modification in the “second_file.txt”. As this change is in my working directory, I need to add it to the staging index. Before doing this We needed to see what is the change made in this file.

GIT diff

Take a closer look on the below picture. It will show you how to use git dill command and why we needed it.

Undo file

Now that do we have here?  I used git diff command. As you all know we use diff command to see the difference in the present and old file. The red colored line shows the previous text or file. While the green shows the latest file and you can see the difference by comparing both.

As you can see some of the text is missing. New we needed that change back. There is a command git checkout. It returns the file as saved in the repository, we will get the previous version back. Lets see how we do it?

GIT checkout

The command of git checkout is implemented in the below way.

Undo file

So what do we have here? You can see I gave the command “git checkout” followed by a pear of dashes and the name of file which we needed to undo. I used a pear of dashes because I am telling git to remain inside the current branch. Then I gave the name of the file. This is how we undo file. After doing this I checked the status and you can see there is nothing to commit. The working directory is empty. So if we check the file, the data will be back to it’s position.

This is how we undo changes in the working directory.

Undo File In The Staging Index

New we will see how to Undo file in our staging index or you can say how to unstage file. Take a closed look below:

Undo file

What do we have now? As you all see that I added a file named add.php in to my project. This file is in my working directory. I need to move it inside my staging index. As you all know how to do it.

undo file

In the above picture I added the file and checked the status by the help of two commands. Now see that My file is in staging index. I don’t want this, suppose I accidentally did this. So what I should do now? Suppose You wanted to add multiple file and accidentally added a file with them now you need that file back in the working directory.

Now how to unstage it? As you can see git is giving us a hint of how to unstage it. We will use the command “git reset HEAD example”. All you need to do that replace the example with your file name.  Take a closer look below

undo file

As you can see the file is back inside my working directory. This is how we undo file that is accidentally pushed into the staging index. You see it is so easy to do this.

Undo File In The Repository

This is a lot tricker. We only have the ability to change the most recent commit. How we shall do that? Lets have a closer look on the below pictures.

undo file

First I need to add the file into the repository tree. After adding this file we need to commit it and then check the status. After committing the file into the repository We will check the git log file. Look below

undo file

As you can see in the above picture there is a hexadecimal number of my latest commit. It also possess time and date.  Now we need to amend this commit. First I need to change something into my file. After changing check the status now.

undo file

As you can see in the above picture git tells change is not staged to commit. Now I will do commit it but this time with different method. Take a closer look below.

undo file

That new we have here? As you can see I added an additional thing “–amend”. This will amend the commit in the last commit. Now check the status. Take a closer look below.

undo file

As you can see the new commit appeared. If you notice the time and hexadecimal value both are changed. So when ever we change the commit these things will also change with it. This is how we will undo file.

There are some other ways to undo file in git. Let Me show you few other interesting things. Give the command “git log” and copy the first ten letters on the hexadecimal code of the latest commit. Now give command “git revert hexadecimal number”. Place the code in the defined portion and press enter. What will happen then? Take a closer look on the below picture.

undo file

In the above picture as you can see the notepad appeared This gave you your commit message, it provides you this so that you can make change easily. Save the change you make and git will show you the below image.

undo file

As you can see in the above image it tells one file is changed. Now let us see the log again.

undo file

As you can see It shows the revert commit message to us. And the hexadecimal code is also changed. This is also an interesting way to undo file.

Powerful Tool

Git possess a strong command to undo multiple files at a time. “git reset” is a very powerful command. It is also dangerous as will. This help us to control the git head pointer.  There are three options that are used to control the behavior of this command which are

a) Soft

It will only move the pointer. Change the position of the head or you can say point to a new or defined head. The command is  “git reset –soft hexadecimal number”. It is the safest of all three options that’s why it is called soft. Take a look on the following picture to see the working.

Undo file

So I checked the log, it shows the sequences of the commits and the HEAD commit is on the top. I wanted to change this. Look below

Undo file

What do we have here? As you can see I check where the HEAD pointer is pointing. So I wanted to reset it or you can say undo it on the previous version. Take a closer look on the below picture.

undo file

So as you all can see I applied the command soft and wrote the hexadecimal code of a previous commit. So what it did? It is pointing to this commit as the HEAD.

Undo file

As you can see now the HEAD pointer is changed. now it points on the commit I asked it to.

b) Mixed

It changes the pointer and also changes the staging index to match the repository. The command is “git reset –mixed hexadecimal number”. It only see difference in the staging index. Take a closer look on the below picture.

Undo file

As you see this is applied in the same way as the previous command. Take a look on the below picture.

Undo file

As you can see it also did the same thing.

c) Hard

It do everything at a time, change everything at once. The command is “git –hard”

Undo file

As you can see what hard do.

The last thing I wanted to tell you that how to clean up files. There are some files we need to delete, git do it for us. I added three random files in my project to make you understand. Take a closer look below.

undo file

As you can see I added three files and they are in my working directory. Now let’s clean this mess. Look below.

Undo file

Git clean does not work alone it needs so thing with it. As you see it is telling you.

Undo file

The “-n” asks you that you wanted to delete it or not. It does not work with out “-f” command as shown below.

Undo file

As it shows all the junk files are deleted.

Precautions

While working with the git always remember the structure other wise git want work or you command want work. Use git help if you can’t able to give proper command.

Conclusion

So what do we learned today? We learned how to undo file in three tree architecture by the help of git. We made the changes and then brought the previous file back to or directory.  We added file inside the staging index and pushed that back into the working directory. Same we did in the working directory. But the repository tree is a bit complex way to do this. If something is missing or you needed help do inform us.