Skip to main content

Command Palette

Search for a command to run...

How Git Works Internally:

Published
•3 min read•View as Markdown

What Is Git-

Git is a tool used to keep track of changes to files, especially the code of the projects. It is termed a distributed version control system because of its behaviour to allow multiple people to work on the same project.It was created by a person named Linus Torvalds in 2005 for the development of the Linux kernel.

Features Of Git-

  • Version Control System: Git tracks all changes to your project files, allowing you to revert to earlier versions if needed.

  • Repositories: The central place for storing and managing everything related to your project. There are two types:

    • Local Repository: A copy stored on your computer where you can work and make changes.

    • Remote Repository: Stored on a server, like GitHub, for sharing and collaborating with others.

  • Commits: Saved changes in Git, with the repository keeping a record of all your commits.

  • Branches: Allows you to work on new features or fixes separately. For instance, a "main" branch for standard code version and a "feature" branch to add new features.

  • Merging: Combines changes from different branches into the main branch, integrating your work into the overall project.

  • Cloning: Creates a full copy of a Git repository, transferring the entire project from a central server to your computer.

How Git Works-

The basic component on which git works is Linked List, where each commit acts as a node that points to its parent commit(s), creating a chain.

  • Commits as Nodes: In Git, each commit acts like a node in a linked list. This node holds information such as the creator, time of creation, and a reference to the files' state at that time.

  • Pointers to History: Each commit includes a pointer (a SHA-1 hash) to its previous commit. This setup lets Git follow the entire project's history from the latest commit back to the start.

  • Branches and HEAD:

    • A branch is a reference to a specific commit in this history.

    • The HEAD pointer shows which branch you are on, indicating the current commit.

  • Traversal: When you check your commit history (like with git log), Git moves through this linked structure, following the parent pointers from the latest commit to the earliest one.

All About .git Folder-

When you run git init, Git sets up a hidden folder called .git. This folder is the core of your project. If you remove this folder, you will lose your project's history, branches, and the ability to revert to previous states.

  • Key Component: The .git/objects folder is crucial in Git. Instead of saving just "changes," Git saves snapshots of files.

  • Unique File ID: When you commit a file, Git creates a unique ID using a SHA-1 Hash, which is a 40-character string like e5b69c....

  • Storage Method:

    • First 2 Characters: Used as the Folder Name.

    • Remaining 38 Characters: Used as the File Name.

How does pointers find your current node file position-

Git uses a file called HEAD, which conveys about your current position with respect to linked list. If you open .git/heads, it will contains a pointer which will conveys the hash id.

ref: refs/heads/main,

Git is informed that you are currently on the main branch. The branch itself (refs/heads/main) is another reference that stores the hash of the latest commit on that branch.

Revert vs Reset-

  • Use git revert <hash>: when you need to undo changes in a public or shared branch to maintain a consistent history for all team members. This safely cancels out the bad commit without disrupting others' work.

  • Use git reset --hard <hash>:when you are working on local, private changes that haven't been pushed to a remote server and you want to clean up your history as if the mistake never happened.