Git Tutorial menu

Git Branches

A branch is a separate line of work. You can try new ideas on a branch while the main code stays safe.

Why Branches?

Imagine your bakery website is live. You want to add an online order form, but it will take a week. If you work directly on main, the site is half-broken all week. Instead:

  1. Create a branch called order-form.
  2. Build the form there, with as many commits as you need.
  3. When it works, merge it into main.

Meanwhile, main stays clean, and you can still fix urgent bugs on it.

What a Branch Really Is

A branch is just a small label that points to a commit. When you make a new commit on a branch, the label moves forward to it. That is why creating branches in Git is instant and cheap.

Example

text
          main
           |
A --- B --- C
             \
              D --- E
                    |
                order-form

Here main points to commit C, and order-form has two extra commits, D and E.

Basic Branch Commands

CommandWhat it does
git branchList branches (* marks the current one)
git branch order-formCreate a branch (but stay where you are)
git switch order-formMove to a branch
git switch -c order-formCreate a branch and move to it
git branch -m new-nameRename the current branch
git branch -d order-formDelete a merged branch
git branch -D order-formForce delete, even if not merged

Older tutorials use git checkout for switching branches. It still works, but git switch is newer and clearer, so this tutorial uses it.

Creating and Using a Branch

Example

bash
git switch -c order-form
git branch
Output
  main
* order-form

Now make changes and commit them as usual:

Example

bash
echo "<form>Order here</form>" > order.html
git add order.html
git commit -m "Add order form page"
Output
[order-form 5c7e2d1] Add order form page
 1 file changed, 1 insertion(+)
 create mode 100644 order.html

The commit is on order-form only. Switch back to main:

Example

bash
git switch main
ls
Output
index.html  style.css

order.html is gone from the folder, because it does not exist on main. Switch back to order-form and it appears again. Git changes your files to match the branch.

Switching with Unsaved Changes

If you have uncommitted changes that would be overwritten, Git refuses to switch:

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.

Commit your work first, or use git stash (covered in a later lesson).

Seeing All Branches

Example

bash
git log --oneline --graph --all
Output
* 5c7e2d1 (order-form) Add order form page
* 8b41d07 (HEAD -> main) Add basic stylesheet
* 3f2a9c1 Add home page

Branch Naming Tips

  • Use lowercase and hyphens: order-form, fix-mobile-menu
  • Many teams use prefixes: feature/login, fix/footer-link
  • Keep one branch for one feature or fix

Exercise

Exercise

Which command creates a new branch called login and switches to it?

Learn Git with a teacher

Join the Full Stack Development course at Workownix, online or in Orangi Town, Karachi, and get a certificate.

See the course Ask on WhatsApp