Search⌘ K
AI Features

The .git Folder and Snapshots Links

Explore how Git internally manages version control through the .git folder and snapshots. Understand the role of branch pointers, the structure of commit links, and how Git tracks changes to reconstruct files, helping you grasp Git's core functionality behind the scenes.

In this chapter, we’ll learn the following:

  • We’ll learn how Git manages snapshots.
  • We’ll learn how Git manages branch pointers.
  • We’ll learn the three stages of tracking changes.

We’ll start with the workings of the Git snapshots.

The .git folder

As we run git init in the current folder, Git creates a .git folder in the project directory. The .git folder contains everything that Git needs to maintain the repository.

Note: Please don’t change any files in this folder when you browse it. It’s automatically managed by Git software.

Remove the entire Git version history in a project

If we want to remove the entire Git version history for a project, we can use the rm command with the -f and -r switch to recursively remove the entire .git folder:

$ rm -fr .git

If this project folder is the only copy that contains the .git folder, our version history will be permanently lost. On the other hand, if we have backups or remote repositories for the project, we can restore the history.

Copy the project folder with version history

If we need to clone the project folder and preserve the version history, we can copy the project folder, which already includes the .git folder. If we instead pick the files and folders inside the project folder to copy, we need to also copy the hidden .git folder. In the terminal given below, try the following commands:

 $ git init
 $ ls -a
 $ cd .git
 $ ls -a

We move inside the .git folder by cd .git and then use the ls -a command again to see files inside the .git folder.

Terminal 1
Terminal
Loading...

Snapshots links

When we commit changes into a snapshot, Git saves the differences (more on the git diff command later). Each snapshot points to its parent. There are branch pointers pointing to the last commits for each link list. Given any snapshot, Git can trace the changes back to their root—that is, the first snapshot. Git can construct the files by applying the diff patches from its root snapshot. We can foresee that this design approach is suitable for source code and plain text files. It uses text patches to maintain the version changes. On the other hand, if we do version control with binary files, such as .psd format files, it needs to store the whole file for each commit.