What is .git folder and why is it hidden?


Git is currently the most popular version control system. A version control system records the changes made to our project codebase in a special kind of file system-based database. In Git, this database is known as a repository and its structure is inspired by the Linux file system. The repository maintains a history of the changes to our codebase.

The .git folder contains all information that is necessary for the project and all information relating commits, remote repository address, etc. It also contains a log that stores the commit history. This log can help you to roll back to the desired version of the code.

The following example creates an empty git repository.

$ git init

The hidden .git folder can be viewed using the following command in the git bash terminal −

$ ls −a

The output of the command is shown below −

./ ../ .git/

The contents of the .git folder can be viewed using the tree command in a Windows Operating System.

To switch from the git bash terminal to Windows terminal, type the cmd command.

$ cmd

The user is presented with the Windows terminal now. Navigate to the .git folder and use the tree command to inspect the contents of this folder.

>> cd .git //Navigate to the .git hidden folder
>> tree . // View contents of the current directory.

The output of the above commands can be seen in the following screenshot −

Following are a few important sub−directories and files in the .git folder −

  • hooks − This folder contains script files. Git hooks are the scripts that are executed before or after events like commit, push etc.

  • objects − This folder represents an object database of Git.

  • config − This is the local configuration file.

  • refs − This folder stores information about tags and branches.

  • HEAD − This file stores reference to the current branch. It points to the master branch by default.

  • index − This is a binary file and stores staging information

The .git folder will contain details of every single change made to the code base. All snapshots of the modifications will be recorded in this folder like a database, which makes it possible to undo the changes and rollback to the desired version of the code.

The .git folder is hidden to prevent accidental deletion or modification of the folder. The version history of the code base will be lost if this folder is deleted. This means, we will not be able to rollback changes made to the code in future.

Updated on: 20-Feb-2021

17K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements