Git is a distributed version control system that tracks file changes and enables collaborative development.
✔️ Distributed (DVCS)
✔️ Fast & local operations
✔️ Snapshot-based tracking
Git manages changes across three main stages.
- Working Directory → Where files are edited
- Staging Area (Index) → Prepared changes
- Repository (.git) → Permanent commit history
Files move through defined states during development.
Untracked → Modified → Staged → Committed
Check status:
git statusgit init
git add .
git commit -m "message"
git push origin mainDaily workflow:
- Pull latest changes
- Create branch
- Work & commit
- Push branch
- Open Pull Request
Branching allows parallel development without affecting the main codebase.
Create & switch:
git switch -b feature-loginList branches:
git branchDelete branch:
git branch -d feature-logingit merge feature✔️ Preserves history
✔️ Creates merge commit
git rebase main✔️ Cleaner linear history
❗ Rewrites commit history
👉 Never rebase shared public branches.
Structured branching model for release-based projects.
Branches:
- main → Production
- develop → Integration branch
- feature/* → New features
- release/* → Release preparation
- hotfix/* → Emergency fixes
Best for:
✔️ Large teams
✔️ Versioned releases
✔️ Enterprise projects
Simple workflow focused on continuous deployment.
Flow:
- Create branch from main
- Commit changes
- Open PR
- Review & merge
- Deploy
Best for:
✔️ CI/CD environments
✔️ Web applications
✔️ Small-medium teams
Developers commit small changes frequently to main (trunk).
- Short-lived branches
- Feature flags
- Continuous integration
Add remote:
git remote add origin https://github.com/user/repo.gitView remotes:
git remote -vgit remote set-url origin NEW_URLUsed when:
- Migrating repository
- Switching HTTPS to SSH
- Updating organization URL
| Command | Meaning |
|---|---|
| git fetch | Downloads changes only |
| git pull | Fetch + Merge |
Best practice:
git fetch
git merge origin/maingit reset --hard HEAD~1git revert <commit>👉 Use revert for shared branches.
Temporarily save work:
git stash
git stash popUseful when switching branches mid-work.
Password authentication is deprecated; use PAT instead.
Steps:
- Generate PAT from GitHub
- Use HTTPS repo URL
- Use PAT instead of password
Avoid embedding your Personal Access Token (PAT) directly inside the remote URL.
https://<TOKEN>@github.com/user/repo.gitThis may expose your token in:
.git/config- Terminal history
- Shared screenshots or logs
Set the remote without embedding the token:
git remote set-url origin https://github.com/user/repo.gitThen push normally:
git pushGit will prompt you for:
Username → your GitHub username
Password → your Personal Access Token (PAT)
Uses public-private key pair for secure access.
ssh-keygen -t ed25519 -C "your_email@example.com"eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519cat ~/.ssh/id_ed25519.pubUse SSH remote:
git@github.com:user/repo.git
✔️ More secure
View history:
git log --oneline --graph --allView changes:
git diff
git diff --stagedApply specific commit to another branch.
git cherry-pick <commit-hash>Keep history clean, work safely, and collaborate efficiently.
✔️ Make small, meaningful commits
✔️ Use feature branches (never commit directly to main)
✔️ Pull before pushing
✔️ Avoid rebasing shared/public branches
✔️ Use git revert instead of reset for public history
✔️ Never commit secrets or sensitive files
✔️ Use .gitignore properly
✔️ Prefer SSH over HTTPS + PAT
✔️ Delete merged branches
✔️ Protect the main branch with PR reviews