Module 4 Lesson 2: Creating and switching branches
·DevOps

Module 4 Lesson 2: Creating and switching branches

Master the mechanics of branch management. Learn how to create new branches, navigate between them, and understand 'HEAD'—the pointer that tells Git where you are.

Module 2 Lesson 2: Creating and switching branches

Now that we know why we branch, let’s learn how. In this lesson, we master the commands to create, list, delete, and move between our parallel timelines.


1. Listing Branches

To see all the local branches in your repository:

git branch

The branch with an asterisk (*) next to it is the branch you are currently on.


2. Creating a Branch

To create a new branch without switching to it:

git branch feature/new-page

The Shortcut (Create and Switch)

In daily work, you almost always want to switch to the branch as soon as you create it.

# In modern Git (2.23+)
git switch -c feature/new-page

# In older Git
git checkout -b feature/new-page

(The -c or -b stands for "create")


3. Switching Branches

To move back and forth between existing branches:

# Switch to main
git switch main

# Switch to your feature
git switch feature/new-page

What happens when you switch?

When you switch branches, Git updates the files in your Working Directory. If you were on main and switch to feature/ui, any files that only exist in feature/ui will suddenly appear, and any changes in main will "disappear" from your disk.

Don't worry! They aren't gone; they are just safely tucked away in the Git database.


4. Deleting Branches

Once a feature is merged and done, you should delete the branch to keep your repo clean.

# Delete a branch that has been safely merged
git branch -d feature/old-feature

# Force delete a branch (even if it hasn't been merged yet)
git branch -D feature/unfinished-experiment

5. Understanding HEAD

In Lesson 3 of Module 1, we briefly saw the HEAD file. The HEAD is Git's way of knowing "Where am I right now?"

  • When you git switch feature/A, Git moves HEAD to point to feature/A.
  • If you make a commit, the commit happens on whatever HEAD is pointing to.
graph LR
    subgraph "Internal Git Pointer"
        HEAD["HEAD"] --> Main["main"]
    end
    
    subgraph "After Switch"
        HEAD2["HEAD"] --> Dev["feature/X"]
    end

Lesson Exercise

Goal: Practice the branch dance.

  1. Go to your git-practice folder.
  2. Create a new branch content/about-us.
  3. Switch to that branch.
  4. Create a new file about.txt and commit it.
  5. Switch back to main. Does about.txt still exist on your disk? (It shouldn't!).
  6. Switch back to content/about-us. Is the file back? (It should be!).

Observation: This is the magic of Git. It allows your computer to "look" like different versions of the project instantly.


Summary

In this lesson, we established:

  • git branch lists your current branches.
  • git switch -c <name> creates and moves you to a new branch.
  • git switch <name> moves you between existing branches.
  • git branch -d <name> deletes a branch.
  • HEAD is the master pointer that tells Git your current location.

Next Lesson: Now that we have work happening in parallel branches, how do we bring it all together? Welcome to Module 4 Lesson 3: Merging branches.

Subscribe to our newsletter

Get the latest posts delivered right to your inbox.

Subscribe on LinkedIn