Git Basics
Essential Git Commands
# Initialize a repository
git init
# Clone a repository
git clone https://github.com/vendor/module.git
# Check status
git status
# Add files to staging
git add file.php # Single file
git add . # All files
git add src/ # Directory
# Commit
git commit -m "Add product listing block"
# Push to remote
git push origin main
# Pull from remote
git pull origin main
# View log
git log --oneline
git log --graph --oneline --all
Git Configuration
# Set user info
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
# Set default branch name
git config --global init.defaultBranch main
# Set editor
git config --global core.editor "code --wait"
# Useful aliases
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.lg "log --oneline --graph --all"
.gitignore for Magento
# Magento specific
/pub/media/*
/pub/static/*
!/pub/static/.htaccess
/var/
/vendor/
/app/etc/env.php
# Generated
generated/
# IDE
.idea/
.vscode/
*.swp
*.swo
# OS
.DS_Store
Thumbs.db
# Node
node_modules/
# Composer
composer.lock
Stashing Changes
# Save changes temporarily
git stash
# List stashes
git stash list
# Apply most recent stash
git stash apply
# Apply and remove stash
git stash pop
# Drop specific stash
git stash drop stash@{0}
Key Takeaway
Git tracks changes to files. Always commit often with meaningful messages. Use .gitignore to exclude generated files and secrets.
Branching and Merging
Branch Operations
# List branches
git branch # Local branches
git branch -a # All branches (local + remote)
git branch -r # Remote branches only
# Create branch
git branch feature/product-api
# Switch branch
git checkout feature/product-api
git switch feature/product-api # Modern alternative
# Create and switch
git checkout -b feature/product-api
git switch -c feature/product-api
# Delete branch
git branch -d feature/product-api # Safe delete
git branch -D feature/product-api # Force delete
# Rename branch
git branch -m old-name new-name
Merging
# Merge feature into main
git checkout main
git merge feature/product-api
# Merge with no fast-forward (creates merge commit)
git merge --no-ff feature/product-api
# Abort merge
git merge --abort
# Merge conflict resolution
# 1. Edit conflicted files
# 2. Remove conflict markers
# 3. git add <file>
# 4. git commit
Rebasing
# Rebase feature onto main
git checkout feature/product-api
git rebase main
# Interactive rebase (rewrite last 3 commits)
git rebase -i HEAD~3
# Continue rebase after resolving conflicts
git rebase --continue
# Abort rebase
git rebase --abort
# Difference between merge and rebase:
# merge: creates merge commit, preserves history
# rebase: rewrites history, linear commit log
Merge vs Rebase
| Feature | Merge | Rebase |
|---|---|---|
| History | Preserved (branching) | Linear (no branches) |
| Merge commit | Yes | No |
| Safe for shared branches | Yes | No (rewrite history) |
| Use case | Feature branches | Local cleanup before merge |
Key Takeaway
Branches isolate features. Merge preserves history, rebase creates linear history. Never rebase shared branches. Use merge for public branches.
Git Flow and Commit Conventions
Git Flow for Magento
main (production)
|
+-- release/1.0.0
| |
| +-- (final testing, bug fixes)
| +-- merge to main
|
+-- develop (integration)
| |
| +-- feature/product-api
| +-- feature/customer-reviews
| +-- feature/cart-widget
|
+-- hotfix/critical-bug
|
+-- (fix on production)
+-- merge to main AND develop
Branch Naming
feature/short-description feature/product-api
bugfix/short-description bugfix/cart-calculation
hotfix/short-description hotfix/security-patch
release/version-number release/1.0.0
deploy/deployment-name deploy/staging
Commit Message Conventions
<type>(<scope>): <subject>
<body>
<footer>
Types:
feat: New feature
fix: Bug fix
docs: Documentation
style: Formatting (no code change)
refactor: Code refactoring
test: Adding tests
chore: Build, CI, dependencies
Scope: module or component affected
feat(catalog): Add product comparison
fix(checkout): Fix tax calculation
docs(readme): Update installation guide
Subject: imperative mood, lowercase, no period
feat(catalog): add product comparison page
fix(checkout): resolve tax calculation error
Examples
# Feature commit
git commit -m "feat(catalog): add product comparison feature"
# Bug fix
git commit -m "fix(checkout): resolve tax calculation for EU customers"
# With body
git commit -m "feat(customer): add wishlist functionality
- Add wishlist model and resource model
- Create wishlist block for product page
- Add remove from wishlist action"
# Breaking change
git commit -m "feat(api): change product endpoint response format
BREAKING CHANGE: Product API now returns nested objects"
Magento Development Workflow
# 1. Start feature from develop
git checkout develop
git pull origin develop
git checkout -b feature/my-feature
# 2. Make changes and commit
git add .
git commit -m "feat(module): implement feature"
# 3. Keep feature updated
git fetch origin
git rebase origin/develop
# 4. Push feature
git push origin feature/my-feature
# 5. Create pull request (GitHub/GitLab)
# 6. After merge, clean up
git checkout develop
git pull origin develop
git branch -d feature/my-feature
Key Takeaway
Use git flow for Magento: main for production, develop for integration, feature branches for development. Write conventional commit messages with type, scope, and description.
Quiz
1. What is the difference between git merge and git rebase?
2. What does git stash do?
3. What is the correct commit message format for a bug fix?
4. In git flow, which branch is for production?
5. What does --no-ff do in git merge?
Flashcards
Question
What does git stash do?
Click to reveal answer
Answer
Temporarily saves uncommitted changes without committing. Allows switching branches. Use git stash pop to restore.
Question
What is the difference between merge and rebase?
Click to reveal answer
Answer
Merge: preserves history, creates merge commits. Rebase: rewrites history, creates linear commit log. Never rebase shared branches.
Question
What is git flow?
Click to reveal answer
Answer
A branching model: main (production), develop (integration), feature/*, release/*, hotfix/*. Organizes development workflow.
Question
What is the conventional commit format?
Click to reveal answer
Answer
type(scope): subject. Types: feat, fix, docs, style, refactor, test, chore. Example: fix(checkout): resolve tax calculation error.
Question
What does .gitignore do?
Click to reveal answer
Answer
Tells Git which files to ignore. Never commit vendor/, generated/, env.php, media files, etc.
Question
How do you resolve a merge conflict?
Click to reveal answer
Answer
1. Edit conflicted files, 2. Remove conflict markers, 3. git add <file>, 4. git commit.
Question
What is the difference between git pull and git fetch?
Click to reveal answer
Answer
git fetch downloads remote changes. git pull = git fetch + git merge. Fetch is safer as it doesn't auto-merge.
Question
What does git rebase -i do?
Click to reveal answer
Answer
Interactive rebase. Lets you edit, squash, reorder, or drop commits. Useful for cleaning up feature branch history before merging.
Revision Notes
Key Takeaways
- 1. Git tracks file changes with commits, branches, and merges
- 2. Use .gitignore to exclude generated files and secrets
- 3. Branches isolate features; merge preserves history, rebase creates linear history
- 4. Git flow: main (production), develop (integration), feature/*
- 5. Conventional commits: type(scope): subject format
- 6. Never rebase shared/public branches
- 7. Use git stash for temporary change storage
Interview Tips
- • Explain the difference between merge and rebase
- • Describe git flow branching model
- • Know conventional commit message format
- • Explain when to use git stash
- • Describe how to resolve merge conflicts
Cheat Sheet
Git Cheat Sheet
Basic Commands:
git init, git clone, git status
git add, git commit, git push, git pull
Branching:
git branch, git checkout -b, git switch -c
git merge, git rebase
Stashing:
git stash, git stash pop, git stash list
Log:
git log --oneline --graph --all
Conventional Commits:
type(scope): subject
types: feat, fix, docs, style, refactor, test, chore
Git Flow:
main (production) -> develop (integration) -> feature/*
.gitignore:
vendor/, generated/, var/, pub/media/*, env.php