Topics Git Basic Git Commands
Back Sign up to track progress

How do you initialize a new Git repository?

A new Git repository is initialized using the:

git init

command.

This command creates a hidden:

.git

directory that stores:

  • commit history
  • branches
  • configuration
  • repository metadata

Steps to Initialize Repository

Step 1: Create Project Folder

mkdir my-project

Step 2: Move into Folder

cd my-project

Step 3: Initialize Git

git init

Output:

Initialized empty Git repository

What Happens Internally

Git creates:

  • staging area
  • local repository
  • branch references

Importance

Repository initialization is the first step for:

  • version control
  • team collaboration
  • code tracking

Real-World Usage

Every enterprise project such as:

  • Spring Boot applications
  • microservices
  • frontend projects

starts with repository initialization.


Explain the purpose of the git status command

The:

git status

command displays the current state of the working directory and staging area.

It helps developers understand:

  • modified files
  • staged files
  • untracked files
  • current branch status

Example

git status

Typical Output

On branch main
Changes not staged for commit
Untracked files

What git status Shows

Modified Files

Files changed after last commit.

Staged Files

Files added using:

git add

Untracked Files

Files not yet tracked by Git.

Branch Information

Current active branch.

Why git status is Important

Prevents Mistakes

Developers can review changes before commit.

Helps Workflow Management

Provides clear visibility into repository state.

Improves Collaboration

Ensures proper commit organization.

Real-World Usage

Developers use:

git status

very frequently during:

  • feature development
  • debugging
  • code review preparation

It is one of the most commonly used Git commands.


What does the git add command do?

The:

git add

command moves changes from the working directory to the staging area.

It tells Git:

"Prepare these changes for the next commit"

Examples

Add specific file:

git add App.java

Add all files:

git add .

Why Staging is Important

Git allows developers to:

  • selectively commit changes
  • organize commits properly
  • review modifications carefully

Workflow

Working Directory
        ↓
     git add
        ↓
   Staging Area

Benefits of git add

Better Commit Control

Developers choose exact changes.

Cleaner Commit History

Related changes stay grouped together.

Safer Development

Prevents accidental commits.

Real-World Importance

Staging helps teams maintain:

  • professional commit history
  • readable project evolution
  • organized feature tracking

It is a core concept in Git workflows.


How do you create a commit in Git?

A commit is created using the:

git commit

command.

A commit stores a snapshot of staged changes in the repository.

Steps to Create Commit

Step 1: Stage Changes

git add .

Step 2: Create Commit

git commit -m "Added login feature"

Meaning of -m

The:

-m

flag allows adding commit message directly.

Commit Message Best Practices

Good commit messages:

Fixed Kafka consumer issue
Added JWT authentication

Bad examples:

updated code

Why Commits are Important

Maintain Project History

Tracks all development changes.

Enable Rollbacks

Restore previous versions easily.

Improve Team Collaboration

Developers understand code evolution.

Real-World Usage

Professional teams create:

  • small commits
  • meaningful commits
  • feature-based commits

to maintain clean repository history.


What information is contained in a commit object in Git?

A Git commit object stores metadata about a specific project snapshot.

A commit contains:

  • commit ID
  • author details
  • timestamp
  • commit message
  • parent commit reference
  • snapshot reference

Main Components of Commit Object

Commit Hash

Unique SHA-1 identifier.

Example:

a1b2c3d4...

Author Information

Stores:

  • developer name
  • email

Timestamp

Records commit creation time.

Commit Message

Describes code changes.

Parent Commit

Links current commit to previous history.

Why Commit Objects are Important

Commit objects enable:

  • version tracking
  • branching
  • merging
  • rollback functionality

Real-World Importance

Git internally builds project history using linked commit objects, forming a complete development timeline.

This architecture enables Git’s:

  • fast operations
  • distributed nature
  • reliable history management

How does git push differ from git fetch?

Both:

git push

and

git fetch

are used for remote repository communication, but they serve different purposes.

git push

Uploads local commits to remote repository.

Example:

git push origin main

Purpose:

  • share local changes with team

git fetch

Downloads remote updates without merging them.

Example:

git fetch origin

Purpose:

  • check latest remote changes safely

Key Differences

CommandPurpose
git pushUpload local commits
git fetchDownload remote changes

Important Behavior

git push

Changes remote repository.

git fetch

Does NOT modify working files.

Why git fetch is Useful

Allows developers to:

  • inspect remote changes
  • avoid unexpected merge conflicts

Real-World Workflow

Typical workflow:

git fetch
git review changes
git merge
git push

Explain the purpose of the git pull command

The:

git pull

command downloads and merges changes from a remote repository into the current branch.

Internally:

git pull = git fetch + git merge

Example

git pull origin main

What git pull Does

Downloads Latest Changes

Fetches updates from remote repository.

Automatically Merges

Integrates updates into local branch.

Benefits

Keeps Local Repository Updated

Developers stay synchronized.

Supports Team Collaboration

Multiple developers can work together efficiently.

Potential Issues

Sometimes:

merge conflicts

may occur if multiple developers modify same code sections.

Real-World Usage

Developers commonly use:

git pull

before:

  • starting work
  • pushing code
  • creating pull requests

Describe the use of git branch command

The:

git branch

command is used to:

  • create branches
  • list branches
  • delete branches

It helps developers manage parallel development.

Examples

List branches:

git branch

Create branch:

git branch feature-login

Delete branch:

git branch -d feature-login

Why Branches are Important

Branches enable:

  • feature isolation
  • bug fixing
  • experimentation
  • release management

Benefits

Parallel Development

Multiple developers work independently.

Safer Development

Main code remains stable.

Easier Collaboration

Teams organize features separately.

Real-World Workflow

Enterprise projects commonly use:

main
develop
feature/*
hotfix/*

branching strategies.


How do you switch branches using git checkout?

The:

git checkout

command switches from one branch to another.

Example

git checkout feature-login

After switching:

  • working directory updates
  • HEAD points to selected branch

Create and Switch Branch

git checkout -b feature-payment

This:

  • creates branch
  • switches immediately

Why Branch Switching is Important

Allows developers to:

  • work on multiple features
  • isolate code changes
  • test independently

Precautions

Before switching:

  • commit changes
    or
  • stash changes

to avoid data loss.

Modern Alternative

Git also supports:

git switch

for cleaner branch switching workflows.


What is the purpose of git merge?

The:

git merge

command combines changes from one branch into another.

It integrates feature development into main codebase.

Example Workflow

Switch to main:

git checkout main

Merge feature branch:

git merge feature-login

What Happens During Merge

Git combines:

  • commits
  • code changes
  • branch history

Possible Outcomes

Fast-Forward Merge

Simple branch advancement.

Three-Way Merge

Combines multiple histories.

Merge Conflicts

Occur when same code section modified differently.

Why Merging is Important

Merging enables:

  • collaborative development
  • feature integration
  • release management

Real-World Usage

Typical workflow:

feature branch
      ↓
 testing
      ↓
merge into main

Git merge is fundamental in:

  • agile development
  • CI/CD pipelines
  • microservices projects
  • enterprise software development
Done reading this topic? Sign up free to track your progress.
Sign Up to Track