aryaglekshmi

AryaGG

Posted on November 14, 2024

.git
  • Git is a Version Control System that keep track of changes to your files & collaborate with others on project.

  • Git is a Content Addressable Filesystem - Git is a simple key-value data store. Git is like a special storage system where you can save any content (like files or data). When you save something in Git, it gives you a unique key (a SHA-1 hash - 40-character strings) for that content. You can use this key to retrieve the exact content you saved whenever you need it.

Imagine you have a library where every book is given a unique code. When you want to find a book, you just use its code to locate it. Similarly, in Git, you save a file, get a unique key, and use that key to get the file back whenever you need it.

  • git init is a command that initializes a new Git repository. When you run this command in a directory, it sets up the necessary files and structure for Git to start tracking changes in that directory.

Steps and Effects of Running git init

Image description

  • Creation of the .git Directory - Git creates a hidden .git directory in the root of your project. This directory contains all the metadata and object database for your repository.

  • Initialization of Default Files - Inside the .git directory, Git sets up various files and subdirectories needed to manage the repository. This includes configuration files, an object database, and references.

  • Setting Up the Initial Branch - Git sets up the initial branch, usually named main. This branch will be the default branch where your commits will be made initially.

  • After initializing a repository, you can use other Git commands like git add, git commit, git push, etc. Without initializing, these commands won’t work because Git doesn’t know where to store the version history.

  • By initializing a repository, you can later connect it to a remote repository (e.g., on GitHub) to collaborate with others. This allows multiple people to work on the same project simultaneously.

Imagine you’re starting a new group project for a school assignment. You create a folder on your computer for the project files. By running git init in that folder, you set up a system to keep track of every change you and your teammates make. This way, if someone accidentally deletes an important file or makes a mistake, you can easily revert to a previous version. It also helps you see who made which changes and when, making collaboration smoother and more organized.

What's inside .git

  • The core components of the .git directory are the objects, which are fundamental to how Git tracks changes.

Image description

$ tree .git

.git
├── config
├── HEAD
├── hooks
│   └── prepare-commit-msg.msample
├── objects
│   ├── info
│   └── pack
└── refs
    ├── heads
    └── tags

Enter fullscreen mode Exit fullscreen mode
  • We’ll also look at the .git directory structure after the commit. Let’s add a text file named gitText.txt and commit it. This process will return a unique ID for the commit.

Image description

  • Add the File to the Staging Area && Commit the File: The commit returns a unique ID (ad35fe3 in this case) that you can use to reference this specific commit.
C:\git-project>git add .
C:\git-project>git commit -m "added text file"
[master (root-commit) ad35fe3] added text file
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 gitText.txt
Enter fullscreen mode Exit fullscreen mode

.git file structure after the commit

  • After the commit, the file structure changed. Some more files were added and data was updated. We will check them one by one.

index - Staging area, The index file contains metadata information for staging. Once staged, it gathers all the data related to the added files. Think of it as a draft space where you can review and organize your changes.

Image description
HEAD - Tracks the current branch or commit. It updates automatically when you switch branches or checkout a commit.

Think of the HEAD file as a bookmark in a book. The bookmark tells you which page (branch or commit) you are currently on.

Image description

description - Gives a short description of the repository. A Git repository is like a digital storage space where all the files and their history for a project are kept. It helps developers track changes, collaborate with others, and revert to previous versions if needed.

Image description

config - Text file that contains your git configuration for the current repo. If you look into it, you would see some basic settings for you repo like the author, filemode etc.

Image description

COMMIT_EDITMSG - It contains the (last) commit message.

Image description

refs - In Git, refs (references) are pointers to commits, making it easier to manage your repository. They are stored in the .git/refs directory and help keep track of branches, tags, and remote branches. Each ref file contains the commit hash it points to, helping you navigate and manage your project’s history.

Image description

Image description

objects - The objects directory in Git stores data about your files and commits.

  • Git objects are the core components that Git uses to store and manage your project’s data. These objects are stored in a compressed format to save space and improve performance

Image description

1. Blob(Binary Large Object): Think of a blob as a snapshot of a file. Every time you save a file in Git, it creates a blob to store that version of the file. It saves only the file content.
2. Tree: A tree is like a folder. It keeps track of blobs (files) and other trees (subfolders). It helps Git understand the structure of your project.
3. Commit: It captures the state of your project at a specific moment. It includes information about what changes were made, who made them, and when.
4. Tag: A tag is a label you can put on a specific commit. It’s like bookmarking a particular version of your project, often used for marking releases.
Here’s how it works: Use the git log command to see all your commits.

Image description

Each commit has a unique ID called a SHA-1 hash, which is a 40-character string. For example, let’s take a commit with the ID 8d168052de87b8bd8b71211383cd17457f0e2267. Git takes the first two characters (8d) and creates a folder named 8d inside the objects directory. Inside this 8d folder, Git creates a file named 168052de87b8bd8b71211383cd17457f0e2267.

Image description

This file contains all the information about that specific commit. This structure helps Git organize and find commit data efficiently.

objects/
├── 8d/
│   └── 168052de87b8bd8b71211383cd17457f0e2267

Enter fullscreen mode Exit fullscreen mode

logs - Git logs track the changes made to your refs over time. They help you understand the sequence of commits and actions taken in your repository.

Image description

info - Additional configuration for your Git repository. The most important file here is exclude, which works like a .gitignore file but is specific to your local repository. You can use it to specify files and directories that Git should ignore, without sharing these rules with others. For example, if you want Git to ignore all .log files, you can add *.log to the info/exclude file. This helps keep your repository clean by excluding unnecessary files from being tracked.

Image description

hooks - It contain any scripts that can be run before/after git does anything. It is like a little helper to automate the tasks.
Client-side hooks - which run on your local machine and are triggered by actions like committing or merging
Server-side hooks - which run on the server where your repository is hosted and are triggered by actions like pushing commits (e.g., pre-receive to enforce rules before accepting commits).
To use a hook, you typically rename a sample script in the .git/hooks directory by removing the .sample extension and making it executable. This allows you to customize and automate your Git workflow effectively.

Image description

💖 💪 🙅 🚩
aryaglekshmi
AryaGG

Posted on November 14, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

.git
git .git

November 14, 2024