Lesson 22 of 60 – Switching Branches
37%

Switching Branches in Git

The git switch command is used to move from one Git branch to another. It changes the current branch and updates the working directory to match the selected branch.

Note: Use git switch when you want to change branches. Use git switch -c when you want to create a new branch and switch to it in one step.

1. What is git switch?

git switch is a Git command used to change the currently active branch.

git switch branch-name

For example:

git switch feature-login

2. Why Switch Branches?

Developers often need to move between different branches while working on a project.

  • Move from main to a feature branch
  • Move from a feature branch to main
  • Work on different features
  • Review another branch
  • Prepare changes for merging
  • Fix bugs in a separate branch

3. Basic Syntax

The basic syntax is:

git switch branch-name

Replace branch-name with the branch you want to switch to.

4. Viewing Available Branches

Before switching, you can list local branches:

git branch

Example:

* main
  feature-login
  feature-dashboard

The asterisk indicates the current branch.

5. Switching from Main to a Feature Branch

Suppose the repository contains a branch named feature-login.

git switch feature-login

Git changes the current branch from main to feature-login.

6. Switching Back to Main

To return to the main branch:

git switch main

The working directory is updated to match the current state of the main branch.

7. Checking the Current Branch

After switching branches, use:

git branch --show-current

For example:

feature-login

This confirms which branch is currently active.

8. Using git status

You can also check the current branch using:

git status

Example:

On branch feature-login
nothing to commit, working tree clean

9. Switching to a Branch

If the branch already exists, use:

git switch feature-dashboard

Git moves HEAD to the selected branch.

10. Creating and Switching in One Command

The -c option creates a new branch and switches to it.

git switch -c feature-payment

This is equivalent to creating a branch first and then switching to it.

11. git switch vs git branch

Command Purpose
git branch feature-login Creates a branch
git switch feature-login Switches to an existing branch
git switch -c feature-login Creates and switches to a branch

12. Switching with Uncommitted Changes

If you have uncommitted changes, Git may allow the switch if those changes do not conflict with the target branch.

git status
git switch feature-login

Always check your working tree before switching branches.

13. When Git Prevents a Branch Switch

Git can prevent switching if your local uncommitted changes would be overwritten by changes from the target branch.

Git may display a message similar to:

error: Your local changes to the following files would be overwritten
by checkout

In this situation, review your changes before continuing.

14. Commit Changes Before Switching

One option is to commit your current work:

git add .
git commit -m "Save current work"

git switch main

Your changes are now safely stored in a commit.

15. Stashing Changes Before Switching

If your work is not ready for a commit, you can temporarily store it using Git stash.

git stash
git switch main

The stashed changes can later be restored.

16. Switching to a Remote-Tracking Branch

Git can also work with remote-tracking branches such as origin/main.

git switch --track origin/feature-login

This creates a local branch that tracks the specified remote-tracking branch when the branch does not already exist locally.

17. Tracking Branches

A local branch can track a remote branch. This makes commands such as pulling and pushing easier because Git knows the associated upstream branch.

git switch --track origin/feature-login

18. Switching to a Branch from a Specific Remote

You can explicitly create a local branch from a remote-tracking branch:

git switch -c feature-login --track origin/feature-login

The local branch is created and configured to track the remote branch.

19. Switching Between Multiple Feature Branches

git switch feature-login

# Work on login

git switch feature-dashboard

# Work on dashboard

git switch feature-payment

# Work on payment

This allows you to move between independent areas of development.

20. Switching Branches and Files

When switching branches, Git updates tracked files in the working tree when the selected branch contains different committed versions of those files.

git switch main

git switch feature-login

The working directory changes to represent the selected branch.

21. Switching to a Branch with No Local Changes

A simple and safe workflow is:

git status
git switch feature-login
git status

This lets you check the repository before and after the branch switch.

22. Older git checkout Command

Before git switch was introduced, developers commonly used git checkout to switch branches.

git checkout feature-login

Modern Git provides git switch as a clearer command specifically for branch switching.

23. Switching Branches with HEAD

HEAD represents the currently checked-out branch in the normal branch workflow.

HEAD → main

After:

git switch feature-login

HEAD → feature-login

24. Switching Branches Before Merging

Before merging a feature branch into main, you normally switch to the branch that should receive the changes.

git switch main
git merge feature-login

The merge operation will be covered in the next lessons.

25. Switching Branches for Testing

You can switch to another branch to inspect or test its code.

git switch feature-login

After testing, return to the previous branch:

git switch main

26. Common git switch Mistakes

  • Trying to switch to a branch that does not exist.
  • Forgetting to check the current branch.
  • Ignoring uncommitted changes.
  • Switching to the wrong branch before making a commit.
  • Confusing git switch with git switch -c.
  • Forgetting to check the repository after switching.

27. Useful git switch Commands

git switch main
git switch feature-login
git switch -c feature-login
git switch --track origin/feature-login
git switch -c feature-login --track origin/feature-login
git branch --show-current
git status

28. Complete Branch Switching Workflow

# View branches
git branch

# Check current branch
git branch --show-current

# Check working tree
git status

# Switch to feature branch
git switch feature-login

# Check status again
git status

# Return to main
git switch main

29. Practical Example

git switch main

git switch -c feature-homepage

# Make homepage changes

git add .
git commit -m "Add homepage"

git switch main

git switch feature-homepage

git status

In this example, the developer creates a homepage branch, commits the work, returns to main, and later switches back to the feature branch.

30. Summary of git switch

The git switch command is used to move between branches.

git switch branch-name

To create and switch to a new branch:

git switch -c branch-name

Always check your working tree before switching, especially when you have uncommitted changes.

📌 Key Points

  • git switch changes the current branch.
  • git switch branch-name switches to an existing branch.
  • git switch -c branch-name creates and switches to a new branch.
  • git branch can be used to view local branches.
  • git branch --show-current shows the current branch.
  • Check git status before switching branches.
  • Git may prevent a switch if local changes would be overwritten.
  • Commit or stash changes when appropriate before switching.
  • git switch --track can create a local tracking branch from a remote-tracking branch.
  • git checkout was traditionally used for branch switching.
  • Switching branches changes the working tree to match the selected branch.
  • Branches can be switched whenever you need to work on another development line.

🧠 Quick Quiz

Question: Which command is used to switch to an existing Git branch?