Skip to content
Laviyx Learn

Try

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.

Beginner7 min readUpdated
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.

Text
A <--- B   main (HEAD)

Create and switch#

See your branches. The * marks the current one:

Terminal
git branch
Output
* main

Create a branch and move to it with git switch -c:

Terminal
git switch -c add-cart
git branch
Output
Switched to a new branch 'add-cart'
* add-cart
  main

Both branches point to the same commit for now:

Text
A <--- B   main, add-cart (HEAD)

Commit on the branch#

Terminal
echo "Cart page" > cart.html
git add cart.html
git commit -q -m "Add cart page"
git log --oneline
Output
1134c27 Add cart page
1c49a62 Add footer
4e04c7b Add home page

Only add-cart moved:

Text
A <--- B   main
        \
         C   add-cart (HEAD)

Switch back to main, and Git changes the files in your folder to match:

Terminal
git switch main
ls
git log --oneline
Output
Switched to branch 'main'
index.html
1c49a62 Add footer
4e04c7b Add home page

cart.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:

Terminal
git switch add-cart
ls
Output
Switched to branch 'add-cart'
cart.html
index.html

git 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:

Terminal
git log --oneline --graph --all
Output
* 05d0fd0 Add logo
| * 1134c27 Add cart page
|/  
* 1c49a62 Add footer
* 4e04c7b Add home page

Useful branch commands#

CommandWhat it does
git branchList local branches
git branch -aAlso list remote branches
git branch -vShow each branch's latest commit
git switch nameMove to a branch
git switch -c nameCreate a branch and move to it
git branch -m new-nameRename the current branch
git branch -d nameDelete a merged branch
git branch -D nameDelete a branch even if it is not merged

Delete a branch#

Git protects unmerged work. Here add-cart has a commit that main does not have:

Terminal
git branch -d add-cart
Output
error: 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:

Text
feature/shopping-cart
fix/login-redirect
docs/update-readme
feature/LAV-142-payment-form

Avoid 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:

Terminal
echo "Changed" >> index.html
git switch add-cart
Output
error: 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.
Aborting

Commit 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
Terminal
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
Output
  add-post b26c1a8 Add first post
* main     bda50f2 Start blog

Key takeaway#

Next: Merging Branches.

Finished this lesson?