Git Fundamentals - Complete Interview Guide for Developers

Learn Git fundamentals including version control, repositories, commits, branching, merging, remotes, and essential Git commands for software engineering interviews.

Introduction

Git is the world's most popular Distributed Version Control System (DVCS). It helps developers track source code changes, collaborate with teams, maintain version history, and safely manage software releases.

Today, Git is used by almost every software company, including Google, Microsoft, Amazon, Netflix, IBM, Oracle, and Meta.

Whether you're a Java Developer, Full Stack Engineer, DevOps Engineer, or Cloud Engineer, Git is one of the most important tools you'll use every day.


Learning Objectives

After completing this chapter, you will understand:

  • What Git is
  • Why Git is important
  • Version Control Systems
  • Git Architecture
  • Repository Types
  • Git Workflow
  • Commits
  • Branches
  • Merging
  • Remote Repositories
  • GitHub Basics
  • Essential Git Commands
  • Best Practices

What is Git?

Git is a Distributed Version Control System (DVCS) that tracks changes made to source code and enables multiple developers to collaborate efficiently.

Git allows you to:

  • Track code changes
  • Collaborate with teams
  • Maintain complete project history
  • Roll back to previous versions
  • Create feature branches
  • Merge code safely
  • Resolve conflicts

Why Do We Need Git?

Without Git:

  • Code can be overwritten
  • Difficult to collaborate
  • No history of changes
  • Hard to rollback
  • Manual backups

With Git:

  • Complete history
  • Easy collaboration
  • Safe branching
  • Version tracking
  • Quick recovery

Version Control System (VCS)

A Version Control System stores every change made to project files over time.

Benefits:

  • History Tracking
  • Team Collaboration
  • Backup
  • Recovery
  • Branching
  • Merging
  • Code Review

Types of Version Control

Local Version Control

Developer
    │
Local History

Examples

  • Manual Copies

Centralized Version Control

Developer A
      │
Developer B
      │
Central Server
      │
Developer C

Examples

  • SVN
  • CVS

Distributed Version Control

Developer A
      │
Local Repository
      │
Remote Repository
      │
Developer B

Examples

  • Git
  • Mercurial

Git vs SVN

Git SVN
Distributed Centralized
Fast Slower
Offline Support Limited
Local History Central History
Easy Branching Expensive Branching

Git Architecture

Git has four major areas.

Working Directory
        │
        ▼
Staging Area
        │
        ▼
Local Repository
        │
        ▼
Remote Repository

Working Directory

The Working Directory contains your current project files.

This is where developers create and modify files.


Staging Area

The Staging Area (Index) stores selected changes before creating a commit.

Command

git add .

Local Repository

Stores commits on your local machine.

Command

git commit -m "Initial Commit"

Remote Repository

Stores code on remote servers.

Examples

  • GitHub
  • GitLab
  • Bitbucket
  • Azure DevOps

Git Workflow

Edit Code
     │
git add
     │
git commit
     │
git push
     │
Remote Repository

Installing Git

Windows

https://git-scm.com

Linux

sudo apt install git

macOS

brew install git

Verify Installation

git --version

Git Configuration

Configure Username

git config --global user.name "Venugopal Reddy"

Configure Email

git config --global user.email "[email protected]"

View Configuration

git config --list

Creating a Repository

Initialize

git init

Clone Existing Repository

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

Repository Structure

Project
│
├── .git
├── src
├── pom.xml
├── README.md
└── .gitignore

The .git folder stores all Git metadata.


Git Lifecycle

Modified
     │
git add
     │
Staged
     │
git commit
     │
Committed
     │
git push
     │
Remote

Checking Repository Status

git status

Shows

  • Modified files
  • New files
  • Deleted files
  • Staged files

Adding Files

Single File

git add App.java

All Files

git add .

Creating Commits

git commit -m "Added login feature"

Best Practices

  • Small commits
  • Clear messages
  • One logical change per commit

Viewing Commit History

git log

Compact

git log --oneline

Graph

git log --graph

Viewing Changes

Working Directory

git diff

Staged Changes

git diff --staged

Branches

A branch is an independent line of development.

Default Branch

main

Create Branch

git branch feature/login

List Branches

git branch

Switch Branch

git checkout feature/login

or

git switch feature/login

Branch Workflow

main
 │
 ├───────────────┐
 │               │
 ▼               ▼
Feature A     Feature B
 │               │
 └──────Merge────┘
        │
      main

Merging

Merge combines changes from one branch into another.

git merge feature/login

Types

  • Fast Forward
  • Three-Way Merge

Remote Repository

Add Remote

git remote add origin URL

View Remote

git remote -v

Push Changes

git push origin main

Pull Changes

git pull origin main

Fetch Changes

git fetch origin

Difference:

  • fetch downloads changes only.
  • pull downloads and merges changes.

Clone Repository

git clone URL

Creates

  • Local repository
  • Working directory
  • Remote connection

Git Ignore

.gitignore prevents unnecessary files from being committed.

Example

target/
*.class
*.log
.idea/
.vscode/
.env

Undo Changes

Discard Working Directory Changes

git restore file.txt

Unstage File

git restore --staged file.txt

Remove Files

git rm file.txt

Rename Files

git mv old.java new.java

Tags

Tags mark important releases.

Create Tag

git tag v1.0

List Tags

git tag

Common Git Commands

Command Purpose
git init Initialize repository
git clone Clone repository
git status Check status
git add Stage changes
git commit Commit changes
git log View history
git diff View changes
git branch Manage branches
git switch Switch branch
git merge Merge branches
git fetch Download changes
git pull Download and merge
git push Upload changes
git tag Create tag
git remote Manage remotes

Basic Git Workflow in a Team

Clone Repository
        │
Create Branch
        │
Write Code
        │
git add
        │
git commit
        │
git push
        │
Pull Request
        │
Code Review
        │
Merge
        │
Deploy

Best Practices

  • Commit frequently
  • Write meaningful commit messages
  • Pull before pushing
  • Use feature branches
  • Never commit secrets
  • Keep .gitignore updated
  • Review changes before committing
  • Sync with the remote repository regularly
  • Use tags for releases

Interview Summary

After completing this chapter, you should understand:

  • Git Basics
  • Version Control
  • Git Architecture
  • Git Workflow
  • Working Directory
  • Staging Area
  • Local Repository
  • Remote Repository
  • Branches
  • Merging
  • Clone
  • Push
  • Pull
  • Fetch
  • Git Ignore
  • Tags
  • Essential Git Commands