📌 1️⃣ What is Git?¶
Git is a distributed version control system used to track code changes and collaborate.
- Created by Linus Torvalds
- Official site: Git
- Popular hosting: GitHub, GitLab, Bitbucket
📌 2️⃣ Installation¶
Windows¶
Download from git-scm.com
Ubuntu¶
Verify¶
📌 3️⃣ Initial Setup (VERY IMPORTANT)¶
Check:
📌 4️⃣ Core Git Concepts¶
| Concept | Meaning |
|---|---|
| Working Directory | Your project files |
| Staging Area | Prepared changes |
| Repository (.git) | Git database |
| Commit | Snapshot |
| Branch | Separate line of development |
| Remote | Cloud repo |
📌 5️⃣ Basic Workflow (Daily Use)¶
Step 1: Initialize Repo¶
Step 2: Add Files¶
Step 3: Commit¶
Step 4: Check Status¶
Step 5: View History¶
📌 6️⃣ Connect to Remote (GitHub Example)¶
git remote add origin https://github.com/username/repo.git
git branch -M main
git push -u origin main
📌 7️⃣ Branching (Very Important for Real Projects)¶
Create Branch¶
Switch Branch¶
Merge Branch¶
📌 8️⃣ Merge Conflicts (Most Common Interview Question)¶
When two developers change same lines.
Resolve:¶
- Open file
- Fix conflict
- Add again
📌 9️⃣ Pull & Fetch¶
Better way:
📌 🔟 Rebase (Advanced)¶
Rebase keeps history clean.
⚠️ Never rebase shared branch.
📌 11️⃣ Stash (Save Work Temporarily)¶
📌 12️⃣ Undo Scenarios (VERY IMPORTANT)¶
Undo last commit (keep changes)¶
Undo and delete changes¶
Restore file¶
📌 13️⃣ Cherry Pick¶
Take commit from another branch:
📌 14️⃣ Tagging (Release Versioning)¶
📌 15️⃣ Git Workflow Models¶
1️⃣ Git Flow¶
- main
- develop
- feature/*
- release/*
- hotfix/*
2️⃣ GitHub Flow¶
- main
- feature branches
Most modern teams use GitHub Flow.
📌 16️⃣ Working with Forks¶
- Fork repo
- Clone fork
- Add upstream
git remote add upstream https://github.com/original/repo.git
git fetch upstream
git merge upstream/main
📌 17️⃣ Submodules¶
Add another repo inside project:
Update:
📌 18️⃣ Large Files (Git LFS)¶
📌 19️⃣ Fixing Mistakes & Recovery¶
Deleted branch?¶
Remove remote branch¶
📌 20️⃣ DevOps + CI/CD Usage¶
In GitHub Actions¶
Docker build from repo¶
Tag version automatically¶
📌 21️⃣ Real World Scenarios (Important)¶
🔥 Scenario 1: Force push?¶
⚠️ Dangerous. Avoid on shared branches.
🔥 Scenario 2: Remove sensitive file¶
Add to .gitignore
🔥 Scenario 3: Rename branch¶
🔥 Scenario 4: Squash commits¶
📌 22️⃣ .gitignore Example¶
📌 23️⃣ Git Best Practices¶
✔ Small commits ✔ Meaningful messages ✔ Pull before push ✔ Never commit secrets ✔ Use feature branches ✔ Protect main branch
📌 24️⃣ Interview Questions¶
- Difference between merge and rebase?
- What is detached HEAD?
- What is git reflog?
- Explain git stash internally?
- How git stores objects? (Blob, Tree, Commit)
📌 25️⃣ Internal Architecture (Deep Level)¶
Git stores objects in:
Types:
- Blob
- Tree
- Commit
- Tag
Git uses SHA-1 hash.
📌 26️⃣ Advanced Commands¶
📌 27️⃣ Enterprise Workflow (DevOps Engineer Level)¶
For your DevOps background:
- Feature branch
- PR
- Code review
- CI pipeline
- Merge
- Auto tag
- Docker build
- Deploy via Terraform
📌 28️⃣ Complete Real Project Flow¶
git clone repo
git checkout -b feature/payment
# code
git add .
git commit -m "Add payment API"
git push origin feature/payment
# create PR
# merge after review
git checkout main
git pull
🎯 Final Summary¶
If you master:
- Branching
- Rebase
- Conflict resolution
- Reflog recovery
- CI integration
- Tagging strategy
- Git internals
👉 You are production-ready Git user