Backing Out Changes in Git Repository

Making changes in Git repository and bringing back to the original stage is called as backing out changes in Git repository.

When you did any changes to the files and later if you do not want to make changes to the files in the Git Repository, you can bring those changes back through reset command.

  • I am starting with my project folder, do ls , I have my starter-web-1/ repository. check all the folders inside my repository by passing command ls. you can see the files that I have currently in my Git Repository.
  • check git status, Git tells us that the working directory is clean.
BC-cd-ls-gitstatus

  • Now let us move to the level1-file.txt by doing cd level1-file.txt and do ls once again.you can see level1-file.txt and hence open that file you can see the current state of the file that it is having only one line content in it.
    BC-cdlevelone-ls

Since my working directory is clean and hence this version of the file is currently in the Git repository. Actually, this is the committed version of the file. So am going to add some more text to the level1-file.txt file. it is nothing but am going to modify the file.

Let's go to hipster Ipsum website and copy some dummy text and paste it into the level1-file.txt file and then save and exit.
BC-adding-text-levelone

  • So now we have three lines in that file.this is actually a part of this file.
  • Now you can check git status. thus we have our modified file. let's go ahead and add this file to the staging area.
    BC-modifiedfile-git-status
  • Use command git add level1-file.txt and press enter and then check git status. So we still have our modified file.
    BC-levelone-gitstatus

If you do not want to keep the changes and really do not want them to be staged and this file is not ready to be committed yet. So follow the instructions which are there in the command git status.

To back out the changes from the staging area or unstaging area, you can use the command
git reset HEAD level1-file.txt.

The above command unstaged the file from the staging area, check git status so that the file has been moved back into the changes not to be committed state, which is nothing but a working directory state.
BC-git-reset-gitstatus

So now open the file and observe that the file has unchanged, and it is still in the working directory as the three lines, If I do not want the changes that I last committed, hence I will follow the git status Instructions.
BC-levelone-gitstatus-again

  • I can discard the changes in my current working directory by using command git checkout , hence I will pass the command as git checkout -- level1-file.txt.
    BC-checkout-git-status
  • Now do git status, you can see that we are back to the clean working directory and if you open the file level1-file.txt it has come back to the original state as it is having only one line inside it.
    BC-changed-notepad-content

Git Pull Operation

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions