The .git Directory Explained

John Wachira Mar 24, 2022
The .git Directory Explained

In this article, we will introduce the Git folder .git. We will cover why Git creates the folder and what it contains.

What Is the .git Folder in Git

Git is a widely used version control system. A Git repository stores the changes you make in your project.

The .git directory stores all data in your repository. This folder can store anything from the information about commits to your repository’s email address.

You will also find a log that contains your commit history. With this log, you can restore your desired code version.

When you run the git init command to initialize an empty repository, Git creates the .git folder.

You can view the folder by running the command below.

ls -C .git

You can expect an output like below.

$ ls -C .git
COMMIT_EDITMSG  HEAD       config       hooks/  info/  objects/     refs/
FETCH_HEAD      ORIG_HEAD  description  index   logs/  packed-refs

Let’s further explore the contents of this directory.

  1. hooks/ - The hooks folder stores script files. Git executes these script files when you run commands like push and commit.
  2. objects/ - This folder contains Git’s object database.
  3. config - This is Git’s configuration file.
  4. refs/ - This folder contains data about branches and tags.
  5. HEAD - This file stores information about your master branch.
  6. index - This binary file contains the staging data.

You can see what the object database contains by running the command below.

ls -C .git/objects

You should get an output like the one below.

$ ls -C .git/objects
03/  24/  30/  77/  87/  ac/  b6/  c6/  e1/  ec/  info/
19/  29/  4b/  78/  8b/  b1/  c3/  d4/  e6/  f6/  pack/
1f/  2d/  67/  7b/  a6/  b2/  c4/  dd/  e9/  fa/

You can inquire database’s objects with the ls -C .git/objects/<dir> command.

Let’s explore the config file. Run the cat .git/config command, as shown below.

pc@JOHN MINGW64 ~/Git (main)
$ cat .git/config
[core]
        repositoryformatversion = 0
        filemode = false
        bare = false
        logallrefupdates = true
        symlinks = false
        ignorecase = true
[gui]
        wmstate = normal
        geometry = 893x435+208+208 175 196
[remote "origin"]
        url = https://github.com/Wachira11ke/Delftscopetech.git
        fetch = +refs/heads/*:refs/remotes/origin/*

The HEAD file references your master branch by default.

The .git folder is always hidden to prevent damage. If you delete the file, you cannot restore changes in your repository.

Author: John Wachira
John Wachira avatar John Wachira avatar

John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.

LinkedIn

Related Article - Git Directory