Skip to main content

Command Palette

Search for a command to run...

How Does Git Store Commits and Track Changes?

Updated
3 min read

Git is an open-source version control software system that is widely used across the software development industry for tracking changes in source code during software development. It is highly valued for its ability to manage both linear and non-linear workflows, allowing developers to track the entire history of a project from its inception. This capability enables teams to collaborate more effectively by providing a comprehensive record of changes, facilitating the merging of different branches, and resolving conflicts that may arise during development. As a result, Git has become an essential tool for most software development companies, helping them maintain organized and efficient workflows while ensuring that all team members are aligned and working with the most up-to-date version of the project.

Whenever a git init command is executed in project git repository initialized with in that project structure. A empty and hidden .git folder created inside the current project directory which is responsible for tracking the project changes.

Inside of .git hidden directory there exists a directory called branches, hooks, info, objects and refs and files named config, description, FETCH_HEAD and HEAD.

Just after executing the git init command all the all the files and folder in the project directory marked as U indicating all the content within the project directory is can be tracked by the git version control software.

But, the U sign is for untracked. This is a feature of git, it doesn’t track all the files by itself. We have to specifically tell git to track that file or folder. For that the git add command get in work, it adds the specified file with git add to the tracking.
Example:

  • On running git add index.js the U sign in front of index.js is now becomes A indicating the file index.js is now added.

  • Whereas inside the .git hidden directory a new file named index created containing data not readable by human. It is like a staging area, where all files are tracked before committing, helping developers to efficiently track changes.

To save changes to the Git folder permanently, you need to use git commit. This command captures the current state of files that you've added to the staging area with git add. When you run git commit, Git saves the changes in the repository's history and creates a new commit with a unique ID. This ID helps you track the changes made at that time. You can also add a commit message to explain the changes, which is useful for understanding the commit later. For example, you might use git commit -m "Added new feature to index.js" to save the changes with a descriptive message. This step is important for keeping a detailed history of your project's development, allowing you to revisit or undo changes if needed.

  • The unique ID is now store in a heads directory inside a refs which indicating the HEAD of current project flow.

  • The commit details is written in a master directory of a logs directory having details like commit message, date and time, unique ID, name and email of developer committed.

  • An additional file is created in a .git's root directory named COMMIT_EDITMSG storing the message passed during the commit.

This are all the internal process occurs inside the .git directory whenever a changes are staged and committed using git. It also tracks all the branches made in a project progressing in parallel.