Git Commands - Spoon Feeding Tutorial

Git Commands – Spoon Feeding Notes

A beginner-friendly, step-by-step Git web tutorial for ItTechGenie.com.
Beginner Hands-on

What is Git?

Git is a Version Control System (VCS). It helps you track who changed what, when, and why. You can also create separate copies of code using branches and merge them later.

Real-world: In a team, Git prevents “last person overwrote my code” problem. Every change becomes a commit with a message and history.

Git basic terms (very simple)

  • Repository (repo): A folder tracked by Git.
  • Commit: A saved checkpoint of your changes.
  • Branch: A separate line of work (like “feature-login”).
  • Remote: Repo hosted online (e.g., GitHub / Azure Repos).

Diagram: Git keeps snapshots (commits)

Working Folder → Staging Area → Repository (Commits) (edit files) (git add) (git commit) Example: A.txt changed → A.txt staged → Commit "Added login validation"

Install + Setup

For Windows, install Git for Windows (it includes Git Bash). For Mac/Linux, Git often comes pre-installed or can be installed from package manager.

After installation, open Git Bash (Windows) or Terminal (Mac/Linux).

Step 1: Check Git version

git --version

Step 2: Set your name and email (must do once)

git config --global user.name "Gopi Suresh" git config --global user.email "youremail@example.com"

Step 3: Verify configuration

git config --global --list
Why this is needed? Git writes your name/email into commits to show who made the change.

First Repo in 10 Minutes (Beginner Demo)

We will create a new folder, initialize Git, then commit a file.

Step-by-step

# 1) Create a folder and go inside mkdir itg-git-demo cd itg-git-demo # 2) Initialize Git repository git init # 3) Create a file echo "Hello Git" > readme.txt # 4) Check status git status
What you will see: Git will show that readme.txt is an untracked file. It means: file exists but Git is not tracking it yet.

Diagram: What git init creates

itg-git-demo/ readme.txt .git/ ← hidden folder (Git database)
Working Folder readme.txt .git (Repository) objects refs / HEAD tracked by

Core Cycle: statusaddcommit

A) Check what changed

git status

B) Add file to staging (tell Git to include in next commit)

# Add one file git add readme.txt # Add all changed files git add .
Staging area is like “shopping cart”. You choose what to commit.

C) Commit (save checkpoint)

git commit -m "Added initial readme"

D) Quick diagram (Most important)

1) Edit file → Working Tree has changes 2) git add → Changes go to Staging 3) git commit → Changes saved as a Commit (History) Working Tree [git add] Staging [git commit] History
Common beginner mistake: You edited file but forgot to run git add. Then commit creates “nothing to commit”.

See History: log + show

A) View commit list

git log

B) One-line history (best for beginners)

git log --oneline --decorate --graph --all

C) View details of a commit

# Replace abc123 with real commit id from log git show abc123
Shortcut: Use git show HEAD to see latest commit.

Compare Changes: diff

diff shows what changed (line by line).

A) Compare working changes (not staged)

git diff

B) Compare staged changes

git diff --staged

C) Compare two commits

git diff abc123 def456
Use case: Before committing, run git diff to confirm you didn’t accidentally change something.

Branching (Work safely)

Branch is like making a new working line for a feature. Your main branch remains stable.

A) See branches

git branch

B) Create a new branch

git branch feature-login

C) Switch to branch

# New Git: git switch feature-login # Older Git: git checkout feature-login

D) Create + switch in one command

git switch -c feature-login

Diagram: Branch split

main: A --- B --- C \ feature-login: D --- E
Team rule: Always create feature branches. Don’t directly commit to main.

Merge (Including Conflicts)

A) Merge feature branch into main

# Go to main git switch main # Pull latest (if using remote) git pull # Merge feature git merge feature-login

Diagram: Merge

Before merge: main: A --- B --- C \ feature-login: D --- E After merge: main: A --- B --- C ------- M \ / feature-login: D --- E ------

B) If conflict happens (very common)

Conflict means Git cannot automatically decide which lines are correct. You must open the file, choose the correct code, then commit.
# 1) See conflict files git status # 2) Open the conflict file and fix it manually # Remove conflict markers: <<<<<<<, =======, >>>>>>> # 3) Add fixed file git add filename.ext # 4) Complete merge commit git commit -m "Merged feature-login into main"
Conflict markers example:
<<<<<<< HEAD Console.WriteLine("Main branch message"); ======= Console.WriteLine("Feature branch message"); >>>>>>> feature-login

Remote: clone, push, pull, fetch

A) Clone a remote repository

git clone https://github.com/ORG/REPO.git

B) Check remote URL

git remote -v

C) Push your branch

# First push (set upstream) git push -u origin feature-login # Next pushes git push

D) Pull changes (fetch + merge)

git pull

E) Fetch changes (only downloads, does not merge)

git fetch
Easy memory:
  • fetch = download updates only
  • pull = download + merge into your current branch
  • push = upload your commits to remote

Undo safely: restore, revert, reset

Undo commands can be risky. Use the safe order: restore → revert → reset.

A) Undo working changes (not staged)

# Discard changes in a file git restore readme.txt

B) Unstage file (remove from staging)

git restore --staged readme.txt

C) Revert a commit (safe for shared branches)

# Creates a NEW commit that reverses the changes git revert abc123

D) Reset (use carefully)

# Move HEAD back, keep changes in working tree: git reset --soft abc123 # Move HEAD back, keep changes unstaged: git reset --mixed abc123 # DANGEROUS: delete changes git reset --hard abc123
Best practice: For team/shared branches, prefer git revert.

Stash (Save work temporarily)

Use stash when you are working, but suddenly need to switch branches. Stash saves your changes in a temporary box.

# Save changes git stash # Save with message git stash push -m "WIP: login validation" # List stashes git stash list # Apply last stash (keep it in stash list) git stash apply # Apply and remove from stash list git stash pop # Drop a stash git stash drop stash@{0}
Use case: You are halfway in a feature, but urgent bug fix needs to be done on main.

Tags + Releases

Tags are used to mark a version like v1.0.

# List tags git tag # Create a tag git tag v1.0 # Create annotated tag (recommended) git tag -a v1.0 -m "Release v1.0" # Push tags to remote git push origin v1.0 git push --tags
Real-world: Production releases are tagged. Later you can checkout that exact code.

.gitignore (Must know)

.gitignore tells Git: “Do not track these files”. Example: build folders, logs, secrets, node_modules, bin/obj, etc.

Example .gitignore for .NET

bin/ obj/ *.user *.suo *.log appsettings.*.json
Important: If a file is already tracked, adding it to .gitignore will NOT remove it automatically. You must untrack it:
git rm --cached appsettings.Production.json git commit -m "Stop tracking secrets"

Real-world Workflow (Team Standard)

This is the safest workflow used in companies for GitHub/Azure Repos.
  1. Pull latest main
  2. Create a feature branch
  3. Commit small commits (each commit should represent 1 logical change)
  4. Push branch to remote
  5. Create Pull Request (PR)
  6. Review + Fix
  7. Merge PR
# 1) Get latest main git switch main git pull # 2) Create feature branch git switch -c feature-profile-page # 3) Work + commit git add . git commit -m "Added profile page UI" # 4) Push branch git push -u origin feature-profile-page

Diagram: PR workflow

Developer Branch → Pull Request → main branch (feature-xyz) (review) (stable)

Cheatsheet (Copy/Paste Ready)

# Check changes git status # Stage git add . git add file.txt # Commit git commit -m "message" # History git log --oneline --graph --decorate --all # Branch git branch git switch -c feature-x git switch main # Merge git merge feature-x # Remote git remote -v git push -u origin feature-x git pull git fetch # Undo git restore file.txt git restore --staged file.txt git revert <commit> git reset --hard <commit> # careful # Stash git stash git stash pop

Practice Tasks (Hands-on)

Task 1: Create repo + 2 commits

  1. Create folder practice1
  2. git init
  3. Create note.txt and commit
  4. Edit note.txt and commit again
  5. Show history using git log --oneline

Task 2: Branch + Merge

  1. Create branch feature-a
  2. Change file and commit on feature-a
  3. Switch to main
  4. Merge feature-a
  5. View graph: git log --graph --oneline

Task 3: Create a conflict and resolve it

  1. Create branch feature-conflict
  2. Change same line in note.txt in main and commit
  3. Change same line in feature-conflict and commit
  4. Try merge and resolve conflict
Don’t fear conflicts — every developer faces them. Just fix the file and commit.

FAQ + Common Errors

You likely forgot git add. Steps:
git status git add . git commit -m "your message"

You are running git command outside a repo folder. Solution:
  • Go to correct folder: cd your-folder
  • Or initialize: git init

Remote has changes you don’t have. Pull first, then push.
git pull git push
If conflict occurs, resolve it and commit.

For beginners and teams: use merge. It is safer and keeps full history. Use rebase only when your team policy allows and you understand it.

© 2026 ItTechGenie — Git Tutorials
Back to Top