0
0
Gitdevops~5 mins

Working in multiple branches simultaneously in Git - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you need to work on different features or fixes at the same time. Git branches let you keep these changes separate so you can switch between tasks without mixing code.
When you want to fix a bug while working on a new feature without losing your progress.
When you need to test different ideas in separate branches before deciding which one to keep.
When collaborating with others, and each person works on their own branch to avoid conflicts.
When you want to prepare a release branch while continuing development on the main branch.
When you want to review or merge changes from one branch to another safely.
Commands
This command creates a new branch called 'feature-login' to work on the login feature separately.
Terminal
git branch feature-login
Expected OutputExpected
No output (command runs silently)
Switches your working directory to the 'feature-login' branch so you can start making changes there.
Terminal
git checkout feature-login
Expected OutputExpected
Switched to branch 'feature-login'
Creates another branch named 'bugfix-header' to fix a header issue without disturbing the login feature work.
Terminal
git branch bugfix-header
Expected OutputExpected
No output (command runs silently)
Switches to the 'bugfix-header' branch so you can work on the header fix independently.
Terminal
git checkout bugfix-header
Expected OutputExpected
Switched to branch 'bugfix-header'
Switches back to the main branch to see the stable code or prepare for merging your changes.
Terminal
git checkout main
Expected OutputExpected
Switched to branch 'main'
Merges the changes from 'bugfix-header' into the current branch (main) to include the header fix.
Terminal
git merge bugfix-header
Expected OutputExpected
Updating abc1234..def5678 Fast-forward header.html | 2 ++ 1 file changed, 2 insertions(+)
Key Concept

If you remember nothing else from this pattern, remember: branches let you work on different tasks separately and switch between them without mixing changes.

Common Mistakes
Trying to work on two branches at the same time without switching branches.
Git only lets you work on one branch at a time in your working directory, so changes can get mixed or lost.
Use 'git checkout branch-name' to switch to the branch you want to work on before making changes.
Creating a branch but forgetting to switch to it before starting work.
Your changes will go to the current branch, not the new one, causing confusion and mixing code.
After creating a branch with 'git branch', always run 'git checkout branch-name' to switch to it.
Merging branches without checking for conflicts or testing.
Conflicts can cause broken code or lost changes if not handled carefully.
Always review and test your code after merging, and resolve conflicts carefully if they appear.
Summary
Create separate branches for different tasks using 'git branch branch-name'.
Switch between branches with 'git checkout branch-name' to work on them independently.
Merge finished work back into main branches carefully using 'git merge branch-name'.