0
0
Gitdevops~10 mins

Working in multiple branches simultaneously in Git - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Working in multiple branches simultaneously
Start on branch main
Create new branch feature
Switch to feature branch
Make changes on feature
Commit changes on feature
Switch back to main
Make changes on main
Commit changes on main
Branches have separate changes
Merge or switch as needed
This flow shows how you create and switch between branches to work on different tasks separately, committing changes independently.
Execution Sample
Git
git checkout -b feature
# Switch to new branch 'feature'
# Make changes and commit

git checkout main
# Switch back to 'main'
# Make changes and commit
This code creates a new branch, switches to it, commits changes, then switches back to main and commits other changes.
Process Table
StepCommandCurrent BranchActionResult
1git checkout -b featuremainCreate and switch to 'feature'Now on branch 'feature'
2edit filesfeatureMake changesWorking directory changed on 'feature'
3git commit -am 'feat: add feature'featureCommit changesCommit added on 'feature'
4git checkout mainfeatureSwitch to 'main'Now on branch 'main'
5edit filesmainMake changesWorking directory changed on 'main'
6git commit -am 'fix: update main'mainCommit changesCommit added on 'main'
7-mainBranches have separate historiesFeature and main have different commits
💡 Finished working on both branches with separate commits
Status Tracker
VariableStartAfter 1After 3After 4After 6Final
Current Branchmainfeaturefeaturemainmainmain
Feature Branch Commits001111
Main Branch Commits000011
Key Moments - 2 Insights
Why do changes on the 'feature' branch not appear when I switch back to 'main'?
Because each branch has its own separate commit history and working directory state, as shown in execution_table rows 3 and 4 where switching branches changes the current branch and working files.
Can I commit changes on one branch and then switch to another branch without losing those changes?
Yes, commits are saved per branch. The execution_table shows commits made on 'feature' (row 3) and then switching to 'main' (row 4) before making new commits (row 6).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, which branch is active after the command?
Afeature
Bmain
Cdetached HEAD
Dunknown
💡 Hint
Check the 'Current Branch' column at step 4 in execution_table
At which step does the first commit happen on the 'feature' branch?
AStep 2
BStep 3
CStep 5
DStep 6
💡 Hint
Look for 'Commit added on feature' in the 'Result' column of execution_table
If you made changes but did not commit on 'feature' and switched to 'main', what would happen?
AChanges stay on 'feature' and are not visible on 'main'
BChanges are lost
CChanges move automatically to 'main'
DGit prevents switching branches
💡 Hint
Git usually prevents switching branches if there are uncommitted changes that would be overwritten, not shown in execution_table but important to know
Concept Snapshot
Working with multiple branches means:
- Create a branch: git checkout -b branch_name
- Switch branches: git checkout branch_name
- Each branch has its own commits
- Changes on one branch do not affect others
- Commit changes before switching to avoid conflicts
Full Transcript
This visual execution shows how to work on multiple git branches at the same time. You start on the main branch, create a new branch called feature, and switch to it. Then you make changes and commit them on feature. Next, you switch back to main, make different changes, and commit there. Each branch keeps its own history and changes separate. This way, you can work on different tasks without mixing code. Remember to commit your changes before switching branches to avoid losing work.