Introduction
Imagine you are writing a story. You hit a point where the plot could go in two different directions: the hero fights the dragon, or the hero befriends the dragon. You want to write both versions to see which is better, but you don't want to mess up your main manuscript.
In Git, branching allows you to do exactly this. It creates a parallel line of development where you can experiment safely. Branches are one of Git's most important ideas because they make isolated work cheap and easy.
In Git, a branch is not a full copy of the project. It is mainly a movable pointer to a line of commits. That is why creating branches is fast and lightweight.
- Develop Features: Build a new login system in a separate space.
- Experiment: Try a new design idea without breaking the current website.
- Fix Bugs: Patch a critical error in isolation.
In this lab, you will create a parallel timeline (branch), make changes in it, and then switch back to the main timeline to prove that your experiments didn't affect the original project.
Create a Feature Branch
By default, every Git repository starts with one main branch, usually called master or main. Think of this as the "Prime Timeline"—the official, stable version of your project.
To work on a new feature (like a login page) without risking the stability of the main project, you create a new branch.
Check your current location:
git branch
Output:
* master
The * indicates you are currently on the master timeline.
That marker shows your current branch context. In Git, many commands act relative to the branch HEAD currently points to, so knowing where you are matters.
Create a new branch named feature_login:
git branch feature_login
You have now created a new timeline! However, you are not in it yet.
This shows an important Git distinction: creating a branch and switching to a branch are separate actions. A branch can exist without being your current working context.
Verify the branch was created:
git branch
Output:
feature_login
* master
The feature_login branch exists, but the * shows you are still standing in master.
Switch to the New Branch
Creating a timeline isn't enough; you need to travel to it. The command to switch branches is git checkout.
Switch to your new branch:
git checkout feature_login
Output:
Switched to branch 'feature_login'
Verify you have moved:
git branch
Output:
* feature_login
master
The asterisk (*) has moved! You are now working inside the feature_login timeline. Any changes you make from now on will only happen here.
More precisely, new commits made now will advance feature_login and not master. Both branches still share the earlier history up to the point where they diverge.
Work in the Feature Branch
Now that you are in your safe experimental space, let's write some code. We will create a script for our "login feature".
Create a file named login.sh:
echo "echo 'Logging in...'" > login.sh
Verify it exists in your folder:
ls
You should see both README.md (inherited from master) and your new login.sh.
At this point, login.sh is just a file on your disk. To officially save it to this branch's history, we need to commit it.
This reinforces the branch concept: files appear in your working tree, but branch history changes only when you create commits on that branch.
Commit Changes to the Branch
Let's save your work. Remember the standard Git workflow: Initialize -> Stage -> Commit.
Check the status:
git status
login.sh is Untracked.
Stage the file:
git add login.sh
Commit the file to the feature_login history:
git commit -m "Add login feature script"
Output:
[feature_login 8a2b3c] Add login feature script
You have now permanently recorded this change, but only in the feature_login timeline.
That isolation is why branches are so useful. They let you develop, test, or experiment without moving the main branch forward until you are ready.
Verify Branch Isolation
This is the magic moment. We claim that feature_login is a separate universe from master. Let's prove it by going back to master.
Switch back to the master branch:
git checkout master
Output:
Switched to branch 'master'
Look at your files:
ls
Where is login.sh? It's gone!
This is exactly what we wanted. The file login.sh does not exist in the master timeline. Your experiment is safely isolated.
To confirm it's not lost forever, switch back to your feature branch:
git checkout feature_login
ls
And it's back! Git swaps the files in your directory instantly as you hop between timelines.
Summary
In this lab, you learned the power of Branching:
- Safety: You can build new features in a separate branch (
feature_login) without risking the main code (master). - Context Switching: You used
git checkoutto jump between these parallel timelines. - Isolation: You proved that changes committed in one branch do not appear in another.
This is how professional teams work. Developers create branches for every new feature or bug fix, keeping the main codebase clean and stable.



