Git Commands - Spoon Feeding Tutorial
Git Commands – Spoon Feeding Notes
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.
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.
Step 1: Check Git version
git --versionStep 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 --listFirst 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 statusreadme.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)Core Cycle: status → add → commit
A) Check what changed
git statusB) Add file to staging (tell Git to include in next commit)
# Add one file
git add readme.txt
# Add all changed files
git add .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] Historygit add.
Then commit creates “nothing to commit”.
See History: log + show
A) View commit list
git logB) One-line history (best for beginners)
git log --oneline --decorate --graph --allC) View details of a commit
# Replace abc123 with real commit id from log
git show abc123git show HEAD to see latest commit.
Compare Changes: diff
diff shows what changed (line by line).
A) Compare working changes (not staged)
git diffB) Compare staged changes
git diff --stagedC) Compare two commits
git diff abc123 def456git 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 branchB) Create a new branch
git branch feature-loginC) Switch to branch
# New Git:
git switch feature-login
# Older Git:
git checkout feature-loginD) Create + switch in one command
git switch -c feature-loginDiagram: Branch split
main: A --- B --- C
\
feature-login: D --- EMerge (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-loginDiagram: 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)
# 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"<<<<<<< HEAD
Console.WriteLine("Main branch message");
=======
Console.WriteLine("Feature branch message");
>>>>>>> feature-loginRemote: clone, push, pull, fetch
A) Clone a remote repository
git clone https://github.com/ORG/REPO.gitB) Check remote URL
git remote -vC) Push your branch
# First push (set upstream)
git push -u origin feature-login
# Next pushes
git pushD) Pull changes (fetch + merge)
git pullE) Fetch changes (only downloads, does not merge)
git fetchfetch= download updates onlypull= download + merge into your current branchpush= upload your commits to remote
Undo safely: restore, revert, reset
A) Undo working changes (not staged)
# Discard changes in a file
git restore readme.txtB) Unstage file (remove from staging)
git restore --staged readme.txtC) Revert a commit (safe for shared branches)
# Creates a NEW commit that reverses the changes
git revert abc123D) 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 abc123git 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}.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.*.jsongit rm --cached appsettings.Production.json
git commit -m "Stop tracking secrets"Real-world Workflow (Team Standard)
- Pull latest main
- Create a feature branch
- Commit small commits (each commit should represent 1 logical change)
- Push branch to remote
- Create Pull Request (PR)
- Review + Fix
- 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-pageDiagram: 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 popPractice Tasks (Hands-on)
Task 1: Create repo + 2 commits
- Create folder
practice1 git init- Create
note.txtand commit - Edit
note.txtand commit again - Show history using
git log --oneline
Task 2: Branch + Merge
- Create branch
feature-a - Change file and commit on
feature-a - Switch to
main - Merge
feature-a - View graph:
git log --graph --oneline
Task 3: Create a conflict and resolve it
- Create branch
feature-conflict - Change same line in
note.txtin main and commit - Change same line in
feature-conflictand commit - Try merge and resolve conflict
FAQ + Common Errors
git add.
Steps:
git status
git add .
git commit -m "your message"- Go to correct folder:
cd your-folder - Or initialize:
git init
git pull
git push