📚
Beginner
5 min readGit & Version Control
Git commands, workflows, and best practices
Git & Version Control Interview Questions
Master Git commands and workflows for effective version control.
1. Essential Git Commands
# Basic workflow
git init
git add .
git commit -m "message"
git push origin main
# Branching
git branch feature-name
git checkout feature-name
git checkout -b feature-name # Create and checkout
git branch -d feature-name # Delete
# Merging
git merge feature-branch
git merge --no-ff feature-branch # Create merge commit
# Rebasing
git rebase main # Rebase current branch onto main
git rebase -i HEAD~3 # Interactive rebase
# Stashing
git stash
git stash pop
git stash apply
git stash list
2. Merge vs Rebase
Merge: Creates a merge commit, preserves history
Rebase: Replays commits, creates linear history
Rule: Never rebase public branches
Rule: Never rebase public branches
3. Git Workflows
# Feature Branch Workflow
git checkout -b feature/login
# Make changes
git add .
git commit -m "Add login feature"
git push origin feature/login
# Create pull request
# Gitflow
# main: production
# develop: integration
# feature/*: new features
# hotfix/*: urgent fixes
# release/*: release preparation
Key Takeaways:
- Use branches for features
- Write clear commit messages
- Merge for public branches, rebase for local
- Use pull requests for code review
- Keep commits atomic and focused