Git and GitHub course menu
Git Branches Explained: git branch and git switch
Learn what a Git branch is, create and switch branches with git branch and git switch, list and rename them, delete finished branches, and name branches well.
On this page
A branch is a separate line of work. You can build a feature on a branch while main stays stable, and combine them when the feature is ready. Companies usually do all their work this way.
A branch is just a pointer#
A branch is not a copy of your files. It is a small label pointing to one commit, so creating a branch is instant, even in a huge project. When you commit, the current branch's label moves forward.
A <--- B main (HEAD)Create and switch#
See your branches. The * marks the current one:
git branch* mainCreate a branch and move to it with git switch -c:
git switch -c add-cart
git branchSwitched to a new branch 'add-cart'
* add-cart
mainBoth branches point to the same commit for now:
A <--- B main, add-cart (HEAD)Commit on the branch#
echo "Cart page" > cart.html
git add cart.html
git commit -q -m "Add cart page"
git log --oneline1134c27 Add cart page
1c49a62 Add footer
4e04c7b Add home pageOnly add-cart moved:
A <--- B main
\
C add-cart (HEAD)Switch back to main, and Git changes the files in your folder to match:
git switch main
ls
git log --onelineSwitched to branch 'main'
index.html
1c49a62 Add footer
4e04c7b Add home pagecart.html has gone from the folder because it does not exist on main. It is safe on the other branch. Switch back and it returns:
git switch add-cart
lsSwitched to branch 'add-cart'
cart.html
index.htmlgit switch - returns to the previous branch, like the back button.
Branches that move apart#
Commit on main too, and the history splits. See it with the graph view:
git log --oneline --graph --all* 05d0fd0 Add logo
| * 1134c27 Add cart page
|/
* 1c49a62 Add footer
* 4e04c7b Add home pageUseful branch commands#
Delete a branch#
Git protects unmerged work. Here add-cart has a commit that main does not have:
git branch -d add-carterror: the branch 'add-cart' is not fully merged
hint: If you are sure you want to delete it, run 'git branch -D add-cart'
hint: Disable this message with "git config set advice.forceDeleteBranch false"Merge it first (next lesson), or use -D if you really want to throw the work away.
Naming branches#
Teams often use a type and a short description, sometimes with a ticket number:
feature/shopping-cart
fix/login-redirect
docs/update-readme
feature/LAV-142-payment-formAvoid spaces. Use short lowercase names that say what the work is.
Uncommitted changes when switching#
If your edits do not conflict with the other branch, Git carries them along. If they would be overwritten, Git refuses:
echo "Changed" >> index.html
git switch add-carterror: Your local changes to the following files would be overwritten by checkout:
index.html
Please commit your changes or stash them before you switch branches.
AbortingCommit them, or save them temporarily with git stash.
Common mistakes#
Practice#
Practice exercise
Create a branch add-post, add post.txt with one line, commit it, go back to main, and print all branches with git branch -v.
Show solution
git switch -q -c add-post
echo "First post" > post.txt
git add post.txt
git commit -q -m "Add first post"
git switch -q main
git branch -v add-post b26c1a8 Add first post
* main bda50f2 Start blogKey takeaway#
Next: Merging Branches.
Finished this lesson?