What is Git?

Git is a distributed version control system used to track changes in source code history during software development. It allows multiple developers to work on a project simultaneously. It tracks changes, supports collaboration, and ensures code integrity.

📌 Notes

  1. Created by: Linus Torvalds in 2005
  2. Type: Distributed version control system
  3. Purpose: Track code changes, coordinate work among developers
  4. Storage: Stores history in snapshots, not differences
  5. Speed: Fast performance for commits, branches, and merges
  6. Security: Cryptographic integrity (SHA-1 for commits)
  7. Flexibility: Supports branching, merging, and workflows
  8. Local-first: Most operations are done locally

🛠️ Common Use Cases

Use Case Description
Code Collaboration Multiple developers working on the same codebase
Version History Tracking and reverting code changes
Feature Branching Developing features in isolated branches
Release Management Managing releases using tags and branches
Open Source Contribution Forking, cloning, and submitting pull requests
Continuous Integration (CI/CD) Works with tools like Jenkins, GitHub Actions

🧭 Workflow

  1. Clone the remote repository → git clone <url>
  2. Make changes in the Working Directory
  3. Stage changes → git add .
  4. Commit changes → git commit -m "commit message"
  5. Push to remote → git push origin branch_name

What is the difference between Git and GitHub?

Git and GitHub are closely related but serve different purposes in the software development process.

Git vs GitHub

Feature Git GitHub
Type Distributed Version Control System Cloud-based Repository Hosting Service
Developed By Linus Torvalds GitHub, Inc. (now owned by Microsoft)
Purpose Track and manage source code history Host, share, review, and collaborate on code
Operation Local (runs on your system) Online (runs in the cloud)
Installation Yes (needs to be installed locally) No (web-based; optional GitHub CLI)
Authentication Not required for local use Requires GitHub account for full features
Main Benefit Offline version tracking Online collaboration and sharing

🛠️ Use Cases

✅ Git

✅ GitHub

🔄 Comparison Diagram

Git (Local) GitHub (Remote)
------------------ -------------------------
| Working Directory | | Hosted Repository |
| + Staging Area | | (UI, Issues, PRs, etc.)|
| + Local Repo | | |
------------------ -------------------------
| ^
git push git pull/clone
| |
v |
+------------------+ +--------------------+
| Tracks changes | <-- Git | Share & Collaborate| <-- GitHub
+------------------+ +--------------------+

What is a repository in Git?

A repository (or repo) in Git is a storage space where your project's files, folders, and the entire version history are saved and tracked. It can be local (on your machine) or remote (like on GitHub).

📌 Notes

Use Case Description
Project Backup Keeps full history of your project versions
Collaboration Allows multiple developers to work together
Change Tracking Helps in identifying when and who made code changes
Branching & Merging Enables parallel feature development
Rollback & Recovery Easily revert back to previous states or commits

WorkFlow Diagram

+------------------------+
| Working Directory |
| (Your Project Files) |
+------------------------+
|
git add
|
+------------------------+
| Staging Area (Index) |
+------------------------+
|
git commit
|
+------------------------+
| Local Repository |
| (.git folder) |
+------------------------+
|
git push/pull
|
+------------------------+
| Remote Repository |
| (GitHub, GitLab) |
+------------------------+

How to Create a Repository?

  1. Initialize a new repository: git init
  2. Clone an existing remote repository: git clone <repo_url>
  3. Check the repo status: git status

What is the difference between a local and remote repository?

In Git, repositories can be either local (on your computer) or remote (hosted on a server or platform like GitHub). Both work together to enable version control and collaboration. A local repository exists on your computer, while a remote repository is hosted on platforms like GitHub or GitLab.

📌 Notes

Aspect Local Repository Remote Repository
Location Stored on your local machine Stored on remote servers (e.g., GitHub)
Purpose Local development and version tracking Collaboration and backup
Internet Required Not needed for local operations Needed for pushing/pulling changes
Visibility Private by default Can be public or private
Creation git init or git clone Created on platforms like GitHub, GitLab
Sync Command git commit, git log git push, git pull, git fetch

🛠️ Use Cases

Local Repository

Remote Repository

# Initialize a local repository
git init

# Clone a remote repository to create a local one
git clone https://github.com/user/repo.git

# Make and commit changes locally
git add .
git commit -m "My update"

# Push local changes to the remote
git push origin main

# Pull remote changes to local
git pull origin main

How do you initialize a Git repository?

To start tracking a project with Git, you must initialize a repository using the git init command. This sets up the required structure for version control in your project directory.

Use Cases

Scenario Description
New Project Start version control for a new codebase
Existing Project Add Git tracking to existing files
Local Development Work locally before pushing to a remote repo
Offline Version History Use Git without internet access

Step 1. Create or enter your project directory

$ mkdir my-project && cd my-project

Step 2. Initialize the repository

$ git init

Step 3. Git creates a hidden .git directory

(.git contains config, refs, objects, etc.)

Step 4. Start adding and committing files

$ git add.

$ git commit -m "Initial commit"

Command Panel

How Do You Clone a Repository?

Cloning a repository means creating a copy of an existing remote Git repository (e.g., from GitHub, GitLab) to your local machine, including its full history and branches.

git clone <repository_url> To download a remote repository (with its code, commits, and branches) into a local working directory

🔄 Repository Cloning Flow

Step 1. Find the remote repository URL

e.g., https://github.com/user/project.git

Step 2. Clone it to your local system

$ git clone https://github.com/user/project.git

Step 3. Git creates a new folder named 'project'

and copies all code and commit history

Step 4. Enter the new project folder

$ cd project

How Do You Check the Status of Your Working Directory and Staging Area?

The command git status provides detailed information about the current state of your working directory and staging area. It shows which files are staged, modified, or untracked.

git status

Scenario Description
Before a commit Review which changes will be saved in the next commit
After modifying files See which files were changed or added
After git add Confirm what’s in the staging area
During troubleshooting Identify untracked or missing files

Status Flow Explanation

Your Project Directory
├── file1.cs ← modified but not staged
├── file2.cs ← added to staging (git add)
├── file3.cs ← untracked (new file)
└── .git/ ← Git metadata

Command

$ git status

Output

- file2.cs: changes staged for commit
- file1.cs: changes not staged
- file3.cs: untracked file

# See what's going on before committing
$ git status

# Output:
On branch main
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    modified:   file2.cs

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
    modified:   file1.cs

Untracked files:
  (use "git add <file>..." to include in what will be committed)
    file3.cs

How Do You Add Files to the Staging Area?

Before committing changes to a Git repository, you need to stage the files. Staging tells Git exactly which changes you want to include in the next commit.

git add <filename> # Add a specific file
git add . # Add all changes in the current directory
git add -A # Add all changes (tracked & untracked)

Scenario Description
Adding new files To start tracking untracked files
Updating modified files Prepare changed files for committing
Staging all changes at once Use git add . for convenience
Selective staging Stage only a few changes instead of all

Staging Flow

[Working Directory] ──► [Staging Area] ──► [Repository (Commit)]

Example

# Add one specific file

git add index.html

# Add multiple files

git add app.js styles.css

# Add all modified and untracked files

git add.

How Do You Commit Changes in Git?

Committing in Git is the process of saving staged changes to your local repository history with a descriptive message.

git commit -m "Your commit message"

Scenario Description
After staging files Save changes with a clear message
Documenting work Explain what the changes do
Breaking down tasks Make small, meaningful commits for easier tracking
Collaborative development Allow others to understand your contributions

Git Commit Flow

[Working Directory] ──► [Staging Area] ──► [Local Repository (Commit)]

Step 1. Modify file → index.html

Step 2. Stage it → git add index.html

Step 3. Commit it → git commit -m "Added homepage"

# Commit all staged changes with a message
git commit -m "Initial project setup"

# Commit with multi-line message
git commit -m "Add login feature" -m "Includes form validation and UI tweaks"

# View commit history
git log

Best Practices for Commit Messages

What Does git log Do?

The git log command shows the commit history of the current branch, displaying a list of all previous commits in reverse chronological order.

git log

Scenario Description
Reviewing change history See what changes were made and when
Finding specific commits Look up commits by message, author, or date
Troubleshooting Identify commits that may have introduced bugs
Collaboration Understand who made what changes
HP@DESKTOP- MINGW64 /d/demo-project (master)
$ git log
commit 9439085ea36fd3ab585556d5c6d7d3c8e3968695 (HEAD -> master)
Author: Raj Bhatt <rajbhatt******2***@gmail.com>
Date:   Sat May 31 19:48:03 2025 +0530

    added css and text file

commit 576648fe36ab8e2255d5c8d6dc94c89e11dbf854
Author: Raj Bhatt <rajbhatt******2***@gmail.com>
Date:   Sat May 31 19:45:16 2025 +0530

    added index.html

[HEAD] ──► Commit C ──► Commit B ──► Commit A
↑ ↑ ↑
git log shows each of these commits in reverse order

Conclusion

Understanding Git is essential for efficient and collaborative software development. From initializing a repository to committing and reviewing changes, these foundational commands provide the building blocks for effective version control.

By mastering the basics like git init, git add, git commit, and git log, you’ll be well-equipped to track your work, collaborate with others, and manage code confidently. Whether you're working solo or in a team, Git ensures your progress is always backed up, traceable, and easy to manage.

Thank you for taking the time to read this post. I hope it has provided you with a clearer understanding of Git and supports you in effectively preparing for Git-related interview questions.

Keep learning, keep practicing, and keep preparing to showcase your best in interviews.
Wishing you success in your coding journey and upcoming interviews!

Happy Coding.