In the last tutorial we learnt about creating the Git repository, we talked about the .git folder which was created inside the repository when we executed the git init command. If you can’t see the folder, it is probably because your hidden folders are not visible (use ls -a in such a case). Let see it through the Git Bash.
Open Git Bash
Navigate to our repository that we created in the last tutorial (Refer Git Repository)
Type ls -a
You can see that we have two folders inside the First Project repository. One of them is .git folder and the other is the one we cloned in the last tutorial. A .git folder is required to log every commit history and every other information required for your remote repository, version control, commits etc. These things are saved in different folders which have different meanings. Once the folder is created, open it and see the contents of the folder. They will look probably like this
What is Git Hooks?
Git hooks are the scripts that are executed before or after the events. These events can be any Git event including the common Git events like commit, push or receive. These scripts increases the productivity of the developer. A pre-commit script is the one which is executed before executing the commit event. This may include checking the spelling errors etc. Similarly a post-commit script is executed after the commit is done. You can enter the hooks folder to see the different scripts in it with pre commit script that we just talked about. You can change these scripts or update them according to you.
In the above image it can be seen with a flow diagram that a pre-commit is executed before commit, post-commit after the commit and so on.
Info folder in Git?
Info folder in git contains the exclude file inside it. As the name suggests, exclude file is used for excluding some specific patterns in the code that you don’t want Git to read or execute as to say. Remember that this file is local and personal to you and is not shared among the developers that clone your project. If there is something that should be ignored by all of the developers in the project, then that comes under the .gitignore about which we will talk in the later tutorials.
Objects folder in Git?
Objects folder is a very important folder in the .git directory. In Git, everything is saved in objects folder as a hash value. By everything I mean every commit, every tree or every file that you create is saved in this directory. With every object there is a hash value linked to it, through which Git knows where is what. The folders are created accordingly. For example, you can see that as soon as you initialise your Git repository, the object folder is empty. Now, let say I create a new file called harish.txt
Now look at the object folder. It now has 2 folders info and pack but they are empty. Let’s write something into the file and commit it and see the changes.
1.Type the following command
echo “this is my first file” > harish.txt
Note: This way we can write anything to the file through Git Bash only. You can also use a text editor about which we will learn in later tutorials.
2.Add your file to the staging area by this command
git add harish.txt
4.Commit the changes by this command
git commit -m “Committing my Changes”
Note: Do not focus on any of the thing like adding to staging area etc for now. We will discuss all this in the later tutorials and clear the concepts.
Look inside the objects folder now. New folders have been created now.
These contain the hash value of the events you just did. Git tracks and recognizes everything by converting it to the hash value. The folders are named according to the hash value saved inside. As its popular use, hashing in Git is not used mainly as a security feature. Hashing in Git is used to be able to trust and create a stable data where collision does not occur. Hash acts as a medium for having your data in a form which can be converted and used to any other technology in the future. The code you save today can also be viewed years later due to this feature of Git. Go to the folders and see the hash value has been created.
This decreases the size of the folder by a factor of 255. The above image is of the 4b folder inside the objects folder.
What is Config Folder in Git.
Config file contains your configuration which have already been discussed in detail while setting up the credentials in Git. All the configuration you set for your project are saved permanently in this file like username, email etc. You can modify it once you set them but you don’t need to do it again and again as you start git. Once you edit the configuration setting, they are saved permanently. You can view them by typing the following command
Description in Git?
Description contains the data about the repositories which can be seen on GitWeb only. This file is none of a programmer’s use. It is just for seeing the repositories on GitWeb. You can learn about GitWeb here.
HEAD Folder in Git
Head file contains the reference to the branch we are currently working on. It is a symbolic reference to the branch and not the normal reference. The difference being that normal reference contains the Hash value as we saw in the above Objects folder. A symbolic reference is the reference to another normal reference. So in this case, Head refers to somewhere else which contains the hash value of the branch we are working on. Note here that the last branch you were working on may not be the latest commit or project. It points out to what you checked out latest.
To see the content of the Head file you can just type the following command
Press Enter and see that the answer is the master branch head in the ref folder.
So by now it is pretty clear how important this folder is for us and is required for us to work in the repository. But what would happen if I delete this folder manually? We will see it.
Deleting Dot Git Folder
By learning about the sub directories located inside the .git directory in the above section, it is quite clear that everything that we do in our project/repository is saved inside the .git directory. Deleting this folder will delete our entire repository. If you haven’t cloned it, all the version and history is erased from the Git. Though your source file will remain as it is. To delete the .git folder
1.Go to Git Bash
2.Delete the folder by typing the following command
rm -rf .git
This command deletes a folder.
After pressing enter, list all the directories to see if .git is deleted or not.
.git is not present in the list. By this we have deleted our .git folder and the entire work we saved in it. You can easily guess that all the work is saved in .git by looking at the size of the folder. It sometimes exceed 10GB of space.
Let’s see how Git now executes the command related to the repositories.
Try to commit a file (It does not matter whether you have a file for commit or not)
Git throws an error stating that the directory in which you are executing the command is not a repository.
This means that the repository is not available. You can also see that it mentions “or any of the parent directories” in parentheses. This is because when Git cannot find the .git folder in the current directory it searches for the same in the parent directory. Since all our files are specific to this particular repository, it will find nothing related to it in the parent directory. What can it find? Well, few things that are saved in the parent directory can be fetched even after deleting the repository.
You can look at it by listing the configuration settings.
git config –list (Refer Setting up credentials in Git)
This will display the list of configuration setting even after deleting the .git folder.
So, if you have not cloned your project, it is better not to delete the .git folder. Although, you can fetch few things saved inside parent directory. We will move on to our next tutorial now.