Understanding Branches

Understanding Branches

Git Branches

Introduction

Branches allow you to work on multiple lines of development simultaneously. This lesson covers creating, managing, and using branches effectively.

What Are Branches?

A branch is an independent line of development. Think of it as a parallel universe where you can make changes without affecting the main codebase.

main:     A---B---C---D---E
             \
feature:   \--F---G---H

Viewing Branches

List Local Branches

git branch

List Remote Branches

git branch -r

List All Branches

git branch -a

Show Current Branch

git branch --show-current

Or use:

git symbolic-ref --short HEAD

Creating Branches

Create and Switch (Short Method)

git checkout -b feature-login

This creates and switches in one command.

Create Without Switching

git branch feature-login

Create from Specific Commit

git branch feature-login abc1234

Create from a Tag

git branch release-1.0 v1.0.0

Switching Branches

Switch to Existing Branch

git checkout feature-login

Or the newer syntax:

git switch feature-login

Switch to Previous Branch

git checkout -

Or:

git switch -

Deleting Branches

Delete Merged Branch

git branch -d feature-login

Force Delete (Even if Not Merged)

git branch -D feature-login

Delete Remote Branch

git push origin --delete feature-login

Or old syntax:

git push origin :feature-login

Renaming Branches

Rename Current Branch

git branch -m new-name

Rename Specific Branch

git branch -m old-name new-name

Tracking Branches

Set Upstream During Push

git push -u origin feature-branch

This sets the upstream (tracking) relationship.

View Tracking Relationship

git branch -vv

Shows local branches and their remote tracking branches.

Pull Changes for Branch

git pull

This pulls changes from the upstream branch.

Practical Branching Workflow

Example: Creating a Feature

# Start from main
git checkout main
git pull origin main

Create feature branch

git checkout -b feature-new-login

Make changes

... edit files ...

git add . git commit -m "Add login form"

Push to remote

git push -u origin feature-new-login

Example: Switching Between Tasks

# Working on feature
git checkout feature-login

Something urgent comes up

git stash git checkout main git checkout -b hotfix-urgent

Fix the urgent issue

git add . git commit -m "Fix urgent bug" git push -u origin hotfix-urgent

Return to feature

git checkout feature-login git stash pop

Branch Naming Conventions

Common Patterns

  • feature/description - New features
  • bugfix/description - Bug fixes
  • hotfix/description - Urgent production fixes
  • release/version - Release preparation
  • experiment/description - Experimental work

Examples

feature/user-authentication
feature/add-shopping-cart
bugfix/fix-login-error
hotfix/critical-security-patch
release/v2.0.0
experiment/new-algorithm

Summary

Essential branch commands:

  • git branch - List branches
  • git checkout -b - Create and switch
  • git checkout or git switch - Switch branches
  • git branch -d - Delete branch
  • git push -u - Push and set upstream
Branches enable parallel development and are essential for team collaboration.

Next Lesson

Learn how to merge branches together.

Quiz - Quiz - Understanding Branches

1. What command creates and switches to a new branch?

2. What is the default branch name in new Git repositories?

3. How do you list all local branches?

4. What command deletes a branch locally (after merge)?

5. What does tracking a branch mean?

SSH Keys and Authentication