Branches: create one and switch to it
25 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)Showing steps for Windows. They are marked “On Windows” below.
A branch lets you build a new feature on the side while main keeps the working version of your project. The examples use price-list, a small project with prices.txt and a README, committed twice on main as in Your first repository and commit (opens in a new tab). Windows learners type the commands in PowerShell, Mac learners in Terminal. Your commit IDs will be different.
What a branch is
A branch is a name that points to one commit. When you commit on a branch, the name moves forward to the new commit, so each branch is its own line of work inside the same repository.
Priya wants to add stapler to the price list, but the price list on main must keep working while she tries it. Copying the whole folder is slow and messy. A branch does the same job with one small label.
Example 1 · See your branches
Runs in: Your terminal
PS C:\Users\Priya\price-list> git branchPS C:\Users\Priya\price-list> git log --onelineOutput* main d504b31 (HEAD -> main) Add README 9d23448 Add price list
What this code does
git branchlists the branches. There is onlymain, and*marks the branch you are on.- In the log,
(HEAD -> main)sits ond504b31: the labelmainpoints at the latest commit.
The main branch and HEAD
The main branch is the branch Git creates with your first commit, usually named main. By team habit it always holds the working, tested version of the project, and new work joins it only when it is finished.
Older projects call it master; it works the same way. Every commit you made in Git Basics went on main.
HEAD is Git's name for where you are now. In History and differences (opens in a new tab) it marked your current commit. It usually does that through a branch: HEAD points to main, and main points to a commit. (HEAD -> main) in the log says exactly that.
When you commit, only the branch that HEAD points to moves forward. Every other branch stays where it was.
Create a feature branch
A feature branch is a short-lived branch for one feature or fix, such as add-stapler. You create it from main, commit your work on it, and join it back into main when the work is finished and tested.
Name a branch after its work, in lower case with hyphens: add-stapler, fix-pen-price. Spaces are not allowed. git switch -c <name> creates the branch and moves you onto it in one step.
Example 2 · Create add-stapler and move to it
Runs in: Your terminal
PS C:\Users\Priya\price-list> git switch -c add-staplerPS C:\Users\Priya\price-list> git branchOutputSwitched to a new branch 'add-stapler' * add-stapler main
What this code does
-cmeans create. Git put the new labeladd-stapleron the current commitd504b31and moved HEAD to it.git branchnow lists two branches, and the*moved toadd-stapler. No file in the folder changed yet.
Example 3 · Commit on the feature branch
Runs in: Your terminal
PS C:\Users\Priya\price-list> cat prices.txtPS C:\Users\Priya\price-list> git add prices.txtPS C:\Users\Priya\price-list> git commit -m "Add stapler to the price list"PS C:\Users\Priya\price-list> git log --oneline --all --graphOutputNotebook: Rs 15 Pen: Rs 10 Stapler: Rs 40 [add-stapler 8ab0b9a] Add stapler to the price list 1 file changed, 1 insertion(+) * 8ab0b9a (HEAD -> add-stapler) Add stapler to the price list * d504b31 (main) Add README * 9d23448 Add price list
What this code does
- Priya first added
Stapler: Rs 40toprices.txtin VS Code and saved it (Ctrl + S). The file now has three items. - The commit line starts with
[add-stapler 8ab0b9a]: the commit went onadd-stapler. --all --graphshows every branch:add-staplermoved to the new commit, whilemainstill points tod504b31.
git branch add-glue-stick (without switch) only creates a label: you stay on the branch you were on. git branch -v lists every branch with the commit it points to.
Switch between branches
Switching branches moves HEAD to another branch and changes the files in your folder to match that branch's latest commit. git switch <branch> does it; the older git checkout <branch> does the same job.
A friend asks Priya for today's price list while the stapler work is unfinished. She switches back to main, and the stapler line disappears from prices.txt. It comes back when she switches to add-stapler again.
On Windows
In PowerShell, print the file on each branch. Keep prices.txt closed in your editor, or let VS Code reload it, so you see the switched version.
PS C:\Users\Priya\price-list> git switch mainPS C:\Users\Priya\price-list> cat prices.txtOutputSwitched to branch 'main' Notebook: Rs 15 Pen: Rs 10
- On
main,prices.txtis the version fromd504b31, so there is no stapler line. git switch add-staplerbrings the stapler line back: it is safe in the commit8ab0b9a.git checkout mainandgit checkout -b add-staplerare the older spellings ofgit switch mainandgit switch -c add-stapler.
git switch is newer and only switches branches, so it is harder to misuse than git checkout, which also does other jobs.Switching with uncommitted changes
Uncommitted changes belong to no branch. If the branch you switch to has a different version of a file you changed, Git refuses, so your work is never overwritten.
Commit the change first, or throw it away with git restore, then switch.
On Windows
On add-stapler, Priya changed the stapler price to Rs 45 in VS Code, saved, and tried to go back to main without committing.
PS C:\Users\Priya\price-list> git switch mainerror: Your local changes to the following files would be overwritten by checkout: prices.txt Please commit your changes or stash them before you switch branches. Aborting
- Nothing changed: Git stopped before touching any file, and Priya is still on
add-stapler. - To keep the change:
git add prices.txt,git commit -m "Raise stapler price to Rs 45", thengit switch mainworks. - "Stash" in the message is another Git tool for parking changes; a commit works just as well.
Next step
Check your understanding
10 quick questions