Everything About Git Part: 4

Returning to the old version:

When we are in development, it is often seen that we have to go back to the previous version. After developing the features one by one, there came a time when there was a problem that used to work properly in an earlier version but now it is not working. In that case, if you have tracked in Git, you can easily go back to that version of yours and check the code, if you want, you can also run your project. You will see your project as it was at that time.

We will now intentionally create a new variable called $newStudents inside the test.php file and commit another. After adding a new variable, the file will look like this:

1
2
3
4
5
<?php
$students=["Safan","Shail","Tortoiz"];
$newStudents=["Fuad","Rezaul","Rakib"]
echo "Hello This is my First Git Learn";
?>

Now I will save it, add it to the staging and commit it

1
git add test.php
1
git commit -m "Another New Variable $newStudents Added"

Now we want to make a commitment by dropping that students variable.

Now for some reason, you need to go back to the previous version, that is, you want to return the $ stu1dents variable. And for this, we have to check the log first. Let’s first check the log:

1
git log

Now you can see the logs as below

As you can see in the picture above, there are four committees in total. With the details, you can easily understand what was done in any committee by looking at the message of the committee. And with some encrypted unique commit ID. If you want, you can use them and go back to the previous versions.

However, there is a little complexity, and that is that the unique Id of each commit is much longer. Now if you want to make these IDs easier and shorter then you have to type the following command.

1
git log --oneline

Here are all the essentials that are nice and small. Now the shortcut unique commit ids here have also been shortened. Now we can use these short versions to go back to the previous desired version.

Suppose we want to return to ff15a04 this commit. Now your command to go back will be as follows:

1
git checkout ff15a04

The last one here is Commit ID. Remember your ID will be different here. Now if you run this command, your project will return to the previous version of this commit from the master branch. But of course, you have to keep track of everything while in the master branch. You cannot check-out if a file/folder is un-tracked or uncommitted. Now you will see the commit id instead of master in the command line. See also HEAD detached at your Commit ID.

Now check your test.php file and come back to the previous version. Since you now have an earlier version in your current working directory. But if you want to go back to the master branch, then you have to check-out again like this:

1
git checkout master