Git Topic
Understanding and Using Branches
What You'll Learn:
-
What branches are and why they're important
-
Common branching strategies
-
Branch operations and management
Detailed Explanation:
What are Branches?
Branches are independent lines of development that allow you to work on multiple features simultaneously without affecting the main codebase.
Basic Branch Operations
Viewing Branches:
git branch # Local branches
git branch -r # Remote branches
git branch -a # All branches
git branch -v # With last commit info
Creating and Switching Branches:
# Create new branch
git branch feature-branch
# Switch to branch
git checkout feature-branch
# Create and switch in one command
git checkout -b feature-branch
# Switch back to main
git checkout main
Common Branching Patterns:
-
Feature Branch Workflow:
# Start new feature
git checkout -b feature/user-login
# Work on feature...
git add .
git commit -m "Add user login functionality"
# Switch back to main for bug fix
git checkout main
git checkout -b hotfix/urgent-bug
# Fix bug and commit...
# Return to feature
git checkout feature/user-login
-
Release Preparation:
git checkout -b release/v1.2.0
# Final testing and preparation
Branch Management
Deleting Branches:
# Safe delete (only if merged)
git branch -d feature-branch
# Force delete (even if not merged)
git branch -D feature-branch
# Delete remote branch
git push origin --delete feature-branch
Renaming Branches:
# Rename current branch
git branch -m new-name
# Rename specific branch
git branch -m old-name new-name
Common Branching Strategies
Git Flow:
-
main- production code -
develop- development integration -
feature/*- new features -
release/*- release preparation -
hotfix/*- production fixes
GitHub Flow:
-
main- always deployable -
feature/*- all changes -
Simple and fast
Best Practices
-
Use descriptive branch names
-
Delete merged branches regularly
-
Keep branches up to date with main
-
One feature per branch
Remote Repositories and Collaboration
What You'll Learn:
-
Working with remote repositories (GitHub, GitLab, etc.)
-
Push, pull, and fetch operations
-
Team collaboration workflows
Detailed Explanation:
Remote Repository Basics
Adding Remote Repositories:
# View current remotes
git remote -v
# Add new remote
git remote add origin https://github.com/username/repo.git
# Add multiple remotes
git remote add upstream https://github.com/original/repo.git
Common Remote Operations
Pushing to Remote:
# First push (set upstream)
git push -u origin main
# Subsequent pushes
git push
# Push specific branch
git push origin feature-branch
# Push all branches
git push --all origin
# Push tags
git push --tags
Pulling from Remote:
# Pull changes (fetch + merge)
git pull origin main
# Pull specific branch
git pull origin develop
# Fetch without merging
git fetch origin
# Fetch and see changes before merging
git fetch origin
git log main..origin/main # See what would be merged
Collaboration Workflows
1. Contributing to Existing Project:
# Fork on GitHub, then:
git clone https://github.com/your-username/repo.git
cd repo
# Add original as upstream
git remote add upstream https://github.com/original/repo.git
# Create feature branch
git checkout -b my-feature
# Make changes and commit
git add .
git commit -m "Add new feature"
# Push to your fork
git push -u origin my-feature
# Create Pull Request on GitHub
2. Team Development:
# Get latest changes
git pull origin main
# Create feature branch
git checkout -b feature/team-feature
# Regular development
git add .
git commit -m "Progress update"
git push origin feature/team-feature
# Keep branch updated
git fetch origin
git merge origin/main
# or
git rebase origin/main
Handling Common Scenarios
When Your Push is Rejected:
# Someone else pushed changes
git pull origin main
# Resolve conflicts if any
git push origin main
Working with Multiple Remotes:
# Sync with upstream
git fetch upstream
git merge upstream/main
# Push to your fork
git push origin main
Best Practices for Collaboration
-
Always pull before pushing
-
Use descriptive branch names
-
Keep commits focused and logical
-
Communicate with your team
-
Review code before merging
Comments (0)
No comments yet. Be the first to comment!