Git Version Control: Branches, Merge Conflicts & Workflow Best Practices
This comprehensive Git tutorial covers everything from basic commands to advanced workflow strategies.
Git Fundamentals
Basic Git Commands
# Initialize a repository
git init
# Clone an existing repository
git clone <repository-url>
# Check status
git status
# Add files to staging area
git add <filename>
git add .
# Commit changes
git commit -m "Descriptive commit message"
# Push changes to remote
git push origin main
# Pull changes from remote
git pull origin main
Understanding Git States
- Working Directory: Your local files
- Staging Area: Files ready to commit
- Local Repository: Your committed changes
- Remote Repository: Shared repository (GitHub/GitLab)
Branching Strategy
Creating and Managing Branches
# Create new branch
git branch feature-new-feature
# Switch to branch
git checkout feature-new-feature
# Create and switch branch in one command
git checkout -b feature-new-feature
# List all branches
git branch -a
# Delete branch (local)
git branch -d feature-new-feature
# Delete branch (remote)
git push origin --delete feature-new-feature
Merging Branches
# Switch to target branch
git checkout main
# Merge feature branch
git merge feature-new-feature
# Merge with commit message
git merge feature-new-feature --no-ff -m "Merge feature: Add new functionality"
# Abort merge
git merge --abort
Handling Merge Conflicts
Understanding Merge Conflicts
Merge conflicts occur when Git cannot automatically resolve differences between branches.
Resolving Conflicts
# Start merge
git merge feature-branch
# Git shows conflicts
# <<<<<<< HEAD
# Your changes
# =======
# Their changes
# >>>>>>> feature-branch
# Edit files to resolve conflicts
# Remove conflict markers and keep desired content
# Mark as resolved
git add <resolved-file>
# Continue merge
git commit
Conflict Resolution Tools
# Use merge tool
git mergetool
# Check for conflicts
git diff --name-only --diff-filter=U
# Show conflict details
git diff
Professional Git Workflows
Git Flow Workflow
# Initialize Git Flow
git flow init
# Start feature
git flow feature start new-feature
# Finish feature
git flow feature finish new-feature
# Start release
git flow release start v1.0.0
# Finish release
git flow release finish v1.0.0
# Start hotfix
git flow hotfix start critical-bug-fix
# Finish hotfix
git flow hotfix finish critical-bug-fix
GitHub Flow (Simplified)
# 1. Create branch
git checkout -b feature/user-authentication
# 2. Make changes and commit
git add .
git commit -m "Add user authentication"
# 3. Push branch
git push origin feature/user-authentication
# 4. Create Pull Request on GitHub
# 5. Review and merge via GitHub interface
# 6. Update local main
git checkout main
git pull origin main
Advanced Git Commands
Rebase vs Merge
# Rebase feature branch onto main
git checkout feature-branch
git rebase main
# Interactive rebase
git rebase -i HEAD~3
# Rebase with conflicts
git rebase --continue
git rebase --abort
Cherry-pick
# Copy specific commit to current branch
git cherry-pick <commit-hash>
# Cherry-pick without committing
git cherry-pick --no-commit <commit-hash>
Stashing Changes
# Stash current changes
git stash
# Stash with message
git stash save "Work in progress on feature X"
# List stashes
git stash list
# Apply stash
git stash apply
# Apply and remove stash
git stash pop
# Drop specific stash
git stash drop stash@{1}
Best Practices
Commit Guidelines
- Atomic commits: One logical change per commit
- Descriptive messages: Use present tense, be specific
- Conventional commits: Use standard format (feat:, fix:, docs:, etc.)
# Good commit messages
git commit -m "feat: Add user authentication system"
git commit -m "fix: Resolve memory leak in data processing"
git commit -m "docs: Update API documentation"
Branch Naming Conventions
# Feature branches
feature/user-login
feature/payment-integration
# Bugfix branches
bugfix/memory-leak
bugfix/validation-error
# Hotfix branches
hotfix/critical-security-patch
hotfix/database-connection
# Release branches
release/v1.2.0
Repository Organization
# Use .gitignore
echo "node_modules/" > .gitignore
echo "*.log" >> .gitignore
echo ".env" >> .gitignore
# Keep repository clean
git clean -fd
# Use tags for releases
git tag -a v1.0.0 -m "Release version 1.0.0"
git push origin v1.0.0
Troubleshooting Common Issues
Undo Changes
# Undo last commit (keep changes)
git reset --soft HEAD~1
# Undo last commit (discard changes)
git reset --hard HEAD~1
# Undo specific commit
git revert <commit-hash>
# Reset to remote state
git reset --hard origin/main
Recover Lost Commits
# Find lost commits
git reflog
# Restore lost commit
git checkout <commit-hash>
git checkout -b recovery-branch
Large File Handling
# Check repository size
git count-objects -vH
# Find large files
git rev-list --objects --all | git cat-file --batch-check='%(objecttype) %(objectname) %(objectsize) %(rest)' | sed -n 's/^blob //p' | sort -n
# Use Git LFS for large files
git lfs track "*.zip"
git add .gitattributes
git add large-file.zip
git commit -m "Add large file with LFS"
Integration with Development Tools
Git Hooks
# Install pre-commit hook
cp .git/hooks/pre-commit.sample .git/hooks/pre-commit
# Example pre-commit hook
#!/bin/sh
# Run tests before commit
npm test
# Check code style
npm run lint
IDE Integration
- VS Code: Built-in Git integration
- IntelliJ IDEA: Advanced Git support
- Sublime Text: Git packages available
Security Considerations
- Never commit sensitive data: passwords, API keys, tokens
- Use environment variables: For configuration
- Signed commits: Verify author identity
- Access control: Manage repository permissions
# Configure Git user
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
# Sign commits with GPG
git config --global commit.gpgsign true
git config --global gpg.program gpg