0
0
Gitdevops~15 mins

Working in multiple branches simultaneously in Git - Deep Dive

Choose your learning style9 modes available
Overview - Working in multiple branches simultaneously
What is it?
Working in multiple branches simultaneously means managing different versions of your project at the same time. Each branch is like a separate workspace where you can make changes without affecting others. This lets you develop features, fix bugs, or experiment safely. You can switch between branches to work on different tasks without losing progress.
Why it matters
Without the ability to work in multiple branches, all changes would happen in one place, making it easy to break the project or lose work. Branches let teams work together smoothly and keep the project organized. This avoids confusion and helps deliver better software faster.
Where it fits
Before this, you should understand basic git commands like clone, add, commit, and push. After learning this, you can explore advanced topics like merging, rebasing, and resolving conflicts. This is a key step in mastering collaborative software development.
Mental Model
Core Idea
Branches are parallel workspaces that let you develop different ideas independently and switch between them without losing progress.
Think of it like...
Imagine a notebook with multiple tabs. Each tab holds notes on a different topic. You can flip to any tab to work on that topic without mixing notes from others.
┌─────────────┐
│   main      │
│ (stable)    │
└─────┬───────┘
      │
 ┌────┴─────┐   ┌─────────────┐
 │ feature1 │   │  feature2   │
 │ (branch) │   │  (branch)   │
 └──────────┘   └─────────────┘

You can switch between 'feature1' and 'feature2' branches independently.
Build-Up - 7 Steps
1
FoundationUnderstanding Git Branch Basics
🤔
Concept: Learn what a branch is and how to create one.
A branch in git is a pointer to a snapshot of your changes. To create a branch, use: git branch feature1 This creates a new branch called 'feature1' but does not switch to it. To see all branches, use: git branch
Result
A new branch named 'feature1' exists alongside 'main'.
Knowing that branches are just pointers helps you understand they are lightweight and easy to create.
2
FoundationSwitching Between Branches
🤔
Concept: Learn how to move your workspace to a different branch.
To start working on a branch, you switch to it using: git checkout feature1 This changes your files to match the branch's snapshot. You can now edit files safely without affecting other branches.
Result
Your working directory updates to the 'feature1' branch state.
Switching branches changes your workspace, letting you work on different tasks without mixing changes.
3
IntermediateWorking on Multiple Branches Locally
🤔Before reading on: Do you think you can have changes in two branches open at the same time in one folder? Commit to yes or no.
Concept: Understand that you can only work on one branch at a time in a single folder, but you can use multiple folders or tools to work on many branches simultaneously.
Git only lets you have one active branch per folder. To work on multiple branches at once, you can clone the repository into separate folders: git clone repo.git repo-feature1 cd repo-feature1 git checkout feature1 And similarly for another branch: git clone repo.git repo-feature2 cd repo-feature2 git checkout feature2 This way, you have two folders, each on a different branch, letting you work simultaneously.
Result
Two separate folders each track a different branch, allowing parallel work.
Knowing that branches are tied to folders helps you manage multiple tasks without confusion or lost changes.
4
IntermediateUsing Git Worktrees for Multiple Branches
🤔Before reading on: Do you think git worktrees create full copies of the repo or share data efficiently? Commit to your answer.
Concept: Git worktrees let you have multiple working directories linked to the same repository without full copies.
Instead of cloning multiple times, use: git worktree add ../feature1 feature1 This creates a new folder '../feature1' checked out to the 'feature1' branch. You can work there independently while your main folder stays on another branch. Worktrees share the same git data, saving space and time.
Result
Multiple folders linked to one repo, each on different branches, ready for simultaneous work.
Understanding worktrees helps you efficiently manage multiple branches without wasting disk space.
5
IntermediateStashing Changes When Switching Branches
🤔Before reading on: What happens if you switch branches with uncommitted changes? Commit to your prediction.
Concept: Learn how to save unfinished work temporarily to switch branches safely.
If you have changes but want to switch branches, git may block you. Use: git stash This saves your changes safely. Then switch branches: git checkout feature2 Later, return and restore changes: git stash pop
Result
You can switch branches without losing or mixing unfinished work.
Knowing how to stash prevents lost work and keeps branches clean.
6
AdvancedHandling Conflicts Across Multiple Branches
🤔Before reading on: Do you think conflicts happen only when merging or also when switching branches? Commit to yes or no.
Concept: Understand when and why conflicts appear and how to resolve them when working on multiple branches.
Conflicts occur when changes in one branch clash with another. When merging branches, git tries to combine changes but stops if conflicts appear. You must edit files to fix conflicts, then commit. Conflicts do not happen just by switching branches unless uncommitted changes overlap.
Result
You learn to identify and fix conflicts, keeping branches consistent.
Knowing conflict causes helps you plan work and avoid surprises during merges.
7
ExpertAdvanced Branch Management with Worktrees and Scripts
🤔Before reading on: Can git worktrees be used to automate testing multiple branches simultaneously? Commit to yes or no.
Concept: Explore how experts use worktrees combined with automation to test and build multiple branches in parallel.
Experts create scripts that add worktrees for each feature branch, run tests or builds in parallel folders, then clean up. This speeds up integration and quality checks without manual switching. For example: for branch in $(git branch -r | grep feature); do git worktree add ../${branch##*/} $branch (cd ../${branch##*/} && ./run-tests.sh &) done wait This runs tests on all feature branches simultaneously.
Result
Faster feedback on multiple branches, improving team productivity.
Knowing how to combine git features with automation unlocks powerful workflows for large projects.
Under the Hood
Git branches are pointers to commits in the repository's commit graph. Switching branches updates the working directory to match the commit the branch points to. Worktrees create additional working directories linked to the same git data, sharing objects and refs without duplicating data. Stashing saves uncommitted changes in a stack-like structure inside git's internal storage.
Why designed this way?
Git was designed for speed and efficiency with large projects. Branches as pointers are lightweight, allowing fast creation and switching. Worktrees were added later to avoid costly full clones when working on multiple branches. Stashing helps manage uncommitted work without forcing commits, supporting flexible workflows.
┌───────────────┐
│  .git folder  │
│  (objects,    │
│   refs, stash)│
└──────┬────────┘
       │
┌──────┴────────┐       ┌───────────────┐
│  main branch  │──────▶│ commit snapshot│
└───────────────┘       └───────────────┘
       │
┌──────┴────────┐       ┌───────────────┐
│ feature1 branch│─────▶│ commit snapshot│
└───────────────┘       └───────────────┘

Multiple worktrees point to different branches but share the same .git data.
Myth Busters - 4 Common Misconceptions
Quick: Can you work on two branches at the same time in one folder? Commit to yes or no.
Common Belief:You can have two branches active in the same folder simultaneously.
Tap to reveal reality
Reality:Git only allows one active branch per folder. To work on multiple branches at once, you need separate folders or worktrees.
Why it matters:Believing otherwise leads to lost changes or confusion when switching branches.
Quick: Does switching branches automatically save your uncommitted changes? Commit to yes or no.
Common Belief:Git saves your uncommitted changes automatically when switching branches.
Tap to reveal reality
Reality:Git prevents switching if uncommitted changes would be overwritten. You must commit or stash changes first.
Why it matters:Assuming automatic saving causes lost work or errors during branch switching.
Quick: Do git worktrees duplicate the entire repository data? Commit to yes or no.
Common Belief:Git worktrees create full copies of the repository, using lots of disk space.
Tap to reveal reality
Reality:Worktrees share the same git data and only create separate working directories, saving space.
Why it matters:Misunderstanding this leads to inefficient workflows and wasted disk space.
Quick: Are conflicts caused by switching branches? Commit to yes or no.
Common Belief:Conflicts happen whenever you switch branches with changes.
Tap to reveal reality
Reality:Conflicts occur during merges when changes clash, not just by switching branches unless uncommitted changes overlap.
Why it matters:Misunderstanding conflicts causes unnecessary fear or mistakes in branch management.
Expert Zone
1
Worktrees share git objects but have separate index and HEAD files, allowing independent staging and commits.
2
Stashes are stored in a stack and can be applied selectively, allowing complex workflows with multiple saved changes.
3
Switching branches with untracked files can cause errors; understanding git's safety checks prevents accidental data loss.
When NOT to use
Working in multiple branches simultaneously is not ideal when changes must be tightly integrated immediately. In such cases, feature toggles or trunk-based development may be better. Also, avoid multiple clones if disk space is limited; use worktrees instead.
Production Patterns
Teams use worktrees for parallel feature development and automated CI pipelines to test multiple branches. Developers keep separate folders for hotfixes and features to avoid mixing work. Stashing is common to quickly switch contexts without committing unfinished work.
Connections
Version Control Systems
Builds-on
Understanding branches deepens knowledge of how version control manages parallel development and history.
Task Switching in Cognitive Psychology
Analogy in workflow management
Knowing how humans switch tasks helps appreciate why git enforces one active branch per folder to avoid confusion.
File System Links and Mount Points
Similar pattern
Git worktrees resemble file system links by sharing data but providing separate access points, improving efficiency.
Common Pitfalls
#1Trying to work on two branches in one folder without switching.
Wrong approach:git checkout feature1 # start editing files # without switching, edit files for feature2
Correct approach:git checkout feature1 # work on feature1 git checkout feature2 # work on feature2 separately
Root cause:Misunderstanding that git workspace can only track one branch at a time.
#2Switching branches with uncommitted changes that conflict.
Wrong approach:git checkout feature2 # error: Your local changes to the following files would be overwritten by checkout
Correct approach:git stash git checkout feature2 git stash pop
Root cause:Not saving or stashing changes before switching branches.
#3Cloning the entire repo multiple times to work on branches.
Wrong approach:git clone repo.git repo-feature1 git clone repo.git repo-feature2
Correct approach:git worktree add ../feature1 feature1 git worktree add ../feature2 feature2
Root cause:Not knowing about git worktrees for efficient multiple branch work.
Key Takeaways
Git branches let you work on different versions of your project independently.
You can only have one active branch per folder, but multiple folders or worktrees let you work on many branches simultaneously.
Git worktrees share repository data efficiently, avoiding full clones and saving disk space.
Stashing lets you save unfinished work temporarily to switch branches safely.
Understanding conflicts and branch switching rules prevents lost work and confusion.