Skip to main content

Command Palette

Search for a command to run...

git

Installing Git on Windows

  1. Open Google and search for: download Git

  2. Click on the official website git-scm.com

  3. The site supports multiple platforms: MacOS, Windows, Linux, etc.

  4. Click Download for Windows (64-bit if applicable)

  5. Once downloaded, navigate to the folder and double-click the installer.

  6. During installation:

    • Accept default settings unless otherwise needed

    • Select editor of choice

    • Leave default branches as-is (e.g., master/main)

    • Keep default Git behavior settings

    • Click Install

After a few moments, Git will be installed.

You can now open Git Bash or Git GUI to use Git on your Windows system.


Verifying Installation and Understanding Current Directory

Open Git Bash and type:

pwd

This command prints the present working directory, which tells where you are in your file system.


Create a New Project Directory

mkdir MyProject
cd MyProject

You’ve now created a new folder called MyProject and navigated into it.


Check Project Files

ls

This lists all files and directories inside the current folder. Initially, the folder will be empty.


Initialize a Git Repository

git init

This will create a .git directory inside MyProject. This folder contains all the metadata for version control. You might not see it unless hidden files are made visible.


Check Git Status

git status

This command shows:

  • Which branch you’re on

  • Whether you’ve made any commits

  • What changes are staged or not

  • If there are untracked files


Create a File and Add to Git

echo "Hello Git" > first.txt

This creates a file named first.txt.

To add this file to Git:

git add first.txt

To add all files:

git add .

Commit the File

Before committing, configure your Git identity:

git config --global user.name "Your Name"
git config --global user.email "you@example.com"

Then commit:

git commit -m "My first commit"

This saves the current snapshot of your files into the local repository.


View Commit Logs

git log

This displays commit history, including:

  • Commit hash

  • Author

  • Date/time

  • Commit message


Modify and Track Changes

Open the file and make a change (e.g., add a line).

echo "This is my second line" >> first.txt

Check status:

git status

Git will show first.txt as modified.

Stage and commit:

git add first.txt
git commit -m "Added second line to first.txt"

Git Log in Detail

git log --oneline

This gives a short summary view of your commits.

git log --stat

Shows file-level statistics per commit.


Summary of Key Git Commands

CommandPurpose
git initInitialize a new Git repo
git statusCheck repo status
git add <file>Stage a file
git add .Stage all changes
git commit -m "msg"Commit with message
git logShow commit history
git configConfigure user info
pwdShow current path
lsList files
mkdirCreate directory
cdChange directory
echoWrite text to file

Introduction to Git diff Command

The git diff command is a very important command in Git. In version control systems, we have different stages:

  • Working Directory

  • Staging Area

  • Local Repository

  • Remote Repository

Files are initially available in the working directory. Before committing these files to the local repository, they must be added to the staging area, and from there, committed to the local repository. Finally, files are pushed to the remote repository.

Files can exist in any of these stages: working directory, staging area, local repo, or remote repo.

Why Use git diff?

You may want to compare file content between:

  • Working Directory and Staging Area

  • Working Directory and Local Repository

  • Working Directory and Remote Repository

  • Staging and Local Repository

To perform these comparisons, we use the git diff command. It helps compare files in the version control system.


Practical Example Setup

We will work with three stages:

  1. Working Directory

  2. Staging Area

  3. Local Repository

Step 1: Create Project Folder and File

  • Create a new folder sample_project

  • Open Git Bash inside it

  • Use the command: vim index.txt

  • Add content: animals

  • Save and exit the file

Step 2: Initialize Repository

  • Run: git init

  • This creates an empty local Git repository.

Step 3: Add File to Staging

  • Use: git add index.txt or git add .

  • Check status: git status

  • The file is now in both the working directory and staging area.

Step 4: Modify File in Working Directory

  • Use: vim index.txt

  • Add another line: birds

  • Save and exit

Requirement 1: Compare Working Directory and Staging Area

Command:

git diff index.txt

Output Explanation

  • a/index.txt → Source = Staging

  • b/index.txt → Destination = Working Directory

Git treats staging as source and working directory as destination.

  • Hash values represent file content in each version (staging vs working)

  • File mode shows file type and permissions (e.g. 644)

  • --- a/index.txt → source file (staging)

  • +++ b/index.txt → destination file (working dir)

Symbols:

  • - line missing in source (staging)

  • + line added in destination (working directory)

  • space line unchanged

Example:

 animals
+birds

Requirement 2: Compare Working Directory and Last Commit

First, commit the staged file:

git commit -m "My first commit - file contains one line"

Now add a third line in the working directory:

vim index.txt
Add: fishes

Compare using:

git diff HEAD index.txt

Explanation:

  • HEAD refers to the last commit

  • +birds, +fishes = added in working directory

  • animals (with space) = unchanged


Requirement 3: Compare Staged Copy and Last Commit

Add working directory content to staging:

git add .

Now staging has all 3 lines: animals, birds, fishes.

Command:

git diff --staged HEAD index.txt

or

git diff --cached HEAD index.txt

Output:

  • animals → unchanged

  • +birds, +fishes → added in staging but not in last commit


Requirement 4: Compare Working Directory and Specific Commit

Add a fourth line: trees

vim index.txt

Now working directory has: animals, birds, fishes, trees

To compare with a specific commit:

  1. Get commit IDs:
git log --oneline

Example output:

8e0a5a2 Second commit
15a7b7d My first commit

Use:

git diff 15a7b7d index.txt

Explanation:

  • animals → unchanged

  • +birds, +fishes, +trees → added after the first commit


Requirement 5: Compare Specific Commit and Staging Area

Command:

git diff --staged 15a7b7d

Explanation:

  • animals → unchanged

  • +birds, +fishes → new lines in staging that didn’t exist in the first commit


Requirement 6: Compare Two Specific Commits

Get commit IDs:

git log --oneline

Use:

git diff 15a7b7d 8e0a5a2 index.txt

Explanation:

  • animals → unchanged

  • +birds, +fishes → added in second commit

  • - lines indicate content missing in older commit


Requirement 7: Compare Local Repository with Remote Repository

Assuming your branch is master, and remote is origin, use:

git diff master origin/master

This compares the local master branch with the remote origin/master.

  • What is git rm?

The git rm command is used to remove files from your working directory and/or from the Git index (staging area). It stages the removal so that it can be committed.


Why Use git rm?

You might need to:

  • Remove files accidentally tracked

  • Clean up unused or outdated files

  • Ensure deleted files are reflected in the next commit

Git provides options to control how and where files are removed.


Basic Syntax

git rm <filename>

This removes the file from both:

  • Working directory

  • Staging area (Git index)


Common Use Cases

1. Remove a file from working directory and stage its deletion

git rm index.txt
  • Deletes index.txt from disk

  • Stages the deletion for the next commit

2. Remove a file only from Git, but keep it in the local directory

git rm --cached index.txt
  • Keeps file in the local folder

  • Stops tracking it in Git (useful for .env, log files, etc.)


Other Options

OptionDescription
--cachedUntrack file but keep it on disk
-f or --forceForce removal of files even if they are modified
-rRecursively remove directories

Workflow Example

  1. Create a file:

     echo "Hello" > test.txt
     git add test.txt
     git commit -m "Add test file"
    
  2. Remove it with Git:

     git rm test.txt
     git commit -m "Remove test file"
    

Summary

  • Use git rm to stage file deletions

  • Use git rm --cached to stop tracking files without deleting them

  • Always commit after using git rm to record the deletion in Git history

🔹 1. git init

Initializes a new Git repository in your project directory.

Use: When starting a new project.

git init

🔹 2. git clone

Copies a remote Git repository to your local machine.

Use: To work on a shared codebase.

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

🔹 3. git status

Displays the state of the working directory and staging area.

Use: To check what’s staged, modified, or untracked.

git status

🔹 4. git add

Stages changes (new or modified files) to be committed.

Use: Before committing.

git add index.txt          # single file
git add .                  # all files

🔹 5. git commit

Records the staged changes in the local repository.

Use: To save a checkpoint of your work.

git commit -m "Added feature X"

🔹 6. git log

Shows commit history.

Use: To review past commits.

git log
git log --oneline          # summarized view

🔹 7. git diff

Shows file differences:

  • Working directory vs. staging

  • Staging vs. last commit

  • Between two commits or branches

git diff                   # unstaged changes
git diff --staged          # staged vs last commit
git diff commit1 commit2   # between commits

🔹 8. git rm

Removes a file from Git and optionally from disk.

git rm index.txt
git rm --cached .env       # untrack but keep locally

🔹 9. git mv

Moves or renames a file.

git mv old_name.txt new_name.txt

🔹 10. git reset

Unstages changes or resets commits.

git reset HEAD index.txt       # unstage file
git reset --hard HEAD~1        # reset to previous commit

🔹 11. git checkout

Used to switch branches or restore files.

git checkout main              # switch branch
git checkout -- index.txt      # restore file

🔹 12. git switch (modern alternative to checkout)

Used to switch branches cleanly.

git switch main

🔹 13. git restore (modern alternative to checkout -- <file>)

Used to discard local changes.

git restore index.txt
git restore --staged index.txt

🔹 14. git branch

Lists, creates, or deletes branches.

git branch                    # list branches
git branch dev                # create new branch
git branch -d dev             # delete branch

🔹 15. git merge

Combines changes from one branch into another.

git checkout main
git merge dev

🔹 16. git rebase

Reapplies commits on top of another base commit.

git rebase main

🔹 17. git stash

Temporarily saves changes without committing.

git stash                   # stash current changes
git stash apply             # apply last stash
git stash list              # list all stashes

🔹 18. git pull

Fetches and merges changes from remote to local branch.

git pull origin main

🔹 19. git fetch

Downloads changes from remote, but doesn’t merge.

git fetch origin

🔹 20. git push

Uploads local commits to the remote repository.

git push origin main

🔹 21. git tag

Tags a specific commit, often for release versions.

git tag v1.0
git push origin v1.0

🔹 22. git cherry-pick

Applies a specific commit to the current branch.

git cherry-pick <commit-id>

🔹 23. git revert

Reverts a commit by creating a new one that undoes it.

git revert <commit-id>

🔹 24. git config

Configures user information and Git behavior.

git config --global user.name "Aisalkyn"
git config --global user.email "you@example.com"

🔹 25. git clean

Removes untracked files.

git clean -f              # force delete untracked files
git clean -fd             # delete untracked files and directories

Bonus for DevOps

.gitignore

Used to ignore specific files or directories (e.g., logs, .env files).

node_modules/
*.log
.env

What is Branching in Git?

Let us first understand what branching is and why we need it, and then we’ll see practically how we can implement branching using Git commands.

Normally, when you create a file or project, you create several files and do several commits. By default, these files and commits are placed in the master branch. As soon as you create a local repository and do your first commit, the master branch is created. All files added to staging and committed go into the master branch.

That’s why master is the main branch. In any version control system, especially Git, the master branch contains all the main source code, the completed and production-ready code.

Why Do We Need Branches?

Now let us understand the need for other branches.

Let’s say we have a master branch with several commits (C1, C2, C3, etc.). A developer is working on feature one which must be developed on top of the existing project in the master branch.

If the developer directly works on the master branch, it may cause conflicts or code mess-ups. So instead of disturbing the master branch, we can create a separate branch for the feature.

This feature branch is created based on the master branch, so it inherits all files and commits from master at the time of creation. The developer then switches to this new branch, adds new files and commits for the feature development.

Once development is complete, the developer has two options:

  1. Push this branch code to the remote repository.

  2. Merge this code into the master branch.

Similarly, other developers working on different features can also create their own branches and work independently. This is called parallel development.

Benefits of Branching

  • No need to disturb the master branch.

  • Multiple developers can work in parallel.

  • Keeps the codebase clean and organized.

  • Each branch is isolated.

Key Concept of Isolation

Every branch in Git is independent or isolated. Whatever changes or commits are made in one branch will not affect other branches unless we merge them manually.

For example, if commit C4 and C5 are made after the new branch is created in the master, those commits will not appear in the new branch. Only the commits that existed at the time of branch creation are inherited.


Git Branching Commands

1. git init

Initializes a Git repository.

git init

2. Create File

touch a.txt

3. Add File to Staging

git add a.txt

4. Commit the File

git commit -m "a.txt"

5. View Branches

git branch

This shows available branches. * indicates the current active branch.

6. Create New Branch

git branch br1

Creates a new branch br1. To switch to it:

git checkout br1

Now br1 is the active branch.

7. Combine Create & Switch

Instead of running two commands (git branch and git checkout), we can run one:

git checkout -b br2

Creates and switches to branch br2.


Branch Behavior & Isolation – Practical Demo

Let’s understand commit inheritance and isolation.

Start in master branch:

git checkout master
touch b.txt c.txt
git add b.txt ; git commit -m "b.txt"
git add c.txt ; git commit -m "c.txt"

Check commits:

git log --oneline

Switch to br1:

git checkout br1

Files available: only a.txt. This is because only a.txt was in master at the time br1 was created.

Now Add New Files in br1

touch x.txt y.txt
git add x.txt ; git commit -m "x.txt"
git add y.txt ; git commit -m "y.txt"

Check files in br1: a.txt, x.txt, y.txt

Switch back to master:

git checkout master

Files: a.txt, b.txt, c.txt — no x.txt or y.txt.

Add File in Master After Branching

touch d.txt
git add d.txt ; git commit -m "d.txt"

Now master has: a.txt, b.txt, c.txt, d.txt

Switch back to br1:

git checkout br1

Files still: a.txt, x.txt, y.txt — no d.txt.

This demonstrates isolation. Changes in one branch do not impact the other unless merged.


Important Points

  • As soon as a branch is created, it inherits all commits and files from the current branch.

  • Further changes in either branch are isolated.

  • Once development in a branch is done, the branch can either:

    • Be merged into the master branch.

    • Or pushed to a remote repository independently.

  • This allows parallel development and helps maintain clean code in the master branch.


Summary

The Git branching concept allows:

  • Independent feature development

  • Clean and safe code base

  • Avoids breaking the main code

  • Easy collaboration between developers