Restore to the previous version of a file in git

In my preceding blog, I have explained regarding “How to see all versions of a file in git”. Now, I will elaborate about “How to go back to previous version of a particular file. Here, we will consider the same example of “README.txt” file as it has 4 versions in the “sub-branch”. First see the below image which depicts all versions of that file.


Now, I will go back to the original version of the file which is version-1 with the help of below mentioned steps.

STEP 1 : Get the Revision number of particular version of a file to which you wish to go back.For this , Go to Repository->Visualize sub-branch History , where sub-branch is the name of the local branch.

Click on the oldest version of the file”README.txt” which is refer to as version-1 in the above SS.
Now, Right Click on the oldest version and Click on “Copy commit Summary”.

Content of this looks like below when you paste on your notepad.
68016ab (” README.txt file added”, 2017-05-27)
Here, 68016ab is Revision number .That’s the catch.

STEP 2 : Go to Tools->Add. Fill the details like name and command.
Command format is “git checkout $REVISION — $FILENAME

Click on Add.Once you click on “Add”. This file as per this revision number starting reflecting in Tools Section.

STEP 3 : Go to Tools->readme.txt

Click on Close. Below Screen will appear.



This file start reflecting in “Staged Changes(Will commit)” Section. As you can see clearly the difference of oldest version and latest version with red color and – (minus) sign . Minus sign shows that if you push this file to git , All data which are “Red Marked” will no more reflect in README.txt file.

STEP 4 : As usual, Click on Commit, then Click on Push. Once Push is completed, Below Screen will appear on the screen.

Once this is done, Check those changes in the git repository branch”sub-branch”.





Related posts