Lesson 21 of 60 – Creating Branches
35%

Creating Branches in Git

A Git branch allows you to create a separate line of development from an existing commit. Creating branches is useful when you want to develop a new feature, fix a bug, or experiment with changes without directly modifying the main development branch.

Note: Creating a branch and switching to that branch are two separate operations when using git branch. The command git switch -c can create and switch to a new branch in one step.

1. What Does Creating a Branch Mean?

Creating a branch means creating a new branch reference that starts from the current commit.

A → B → C
         ↑
        main

If you create a new branch at commit C, both branches initially point to C.

2. Basic Branch Creation Command

The basic command for creating a branch is:

git branch branch-name

For example:

git branch feature-login

3. Creating a Feature Branch

Suppose you want to develop a login feature. You can create:

git branch feature-login

This creates the branch but keeps you on your current branch.

4. Creating a Bug-Fix Branch

A separate branch can be created for fixing a bug:

git branch fix-login-error

The branch name describes the purpose of the work.

5. Checking the New Branch

After creating a branch, use:

git branch

Example:

* main
  feature-login

The asterisk shows that you are still on main.

6. Creating and Switching to a Branch

Modern Git provides a convenient command to create and switch to a branch at the same time:

git switch -c feature-login

The -c option means create a new branch.

7. Difference Between branch and switch -c

Command Action
git branch feature-login Creates the branch only
git switch -c feature-login Creates and switches to the branch

8. Branch Starts from Current Commit

When you create a branch, it starts from the commit currently checked out.

A → B → C
         ↑
        main

git branch feature

A → B → C
         ↑
       main
         ↑
      feature

9. Creating a Branch from a Specific Commit

You can create a branch starting from a specific commit:

git branch feature-login 8f32abc

Here, 8f32abc represents the commit from which the branch should start.

10. Creating a Branch from Another Branch

A branch can also be created from another branch:

git branch feature-login develop

The new branch starts from the commit currently referenced by develop.

11. Creating and Switching with switch

A common workflow is:

git switch -c feature-login

After this command, Git creates the branch and changes the current branch to feature-login.

12. Verifying the Current Branch

After switching to the new branch, check the current branch:

git branch --show-current

Example output:

feature-login

13. Checking Branch Status

You can also use:

git status

Example:

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

14. Creating a Branch for a New Feature

git switch main

git switch -c feature-dashboard

You can now work on the dashboard feature without making the feature commits directly on the main branch.

15. Creating a Branch for a Bug Fix

git switch main

git switch -c fix-navbar

You can now make and commit the navbar fix on the new branch.

16. Making Changes on the New Branch

After switching to the new branch, work normally:

git add .
git commit -m "Add dashboard feature"

The new commit advances the branch you are currently on.

17. Branches Can Develop Independently

          D → E
         /
A → B → C
         \
          F → G

After branches are created, they can contain different commits and develop independently.

18. Creating Multiple Branches

You can create multiple branches in the same repository.

git branch feature-login
git branch feature-dashboard
git branch fix-navbar

List them using:

git branch

19. Naming Branches Clearly

Good branch names should communicate the purpose of the work.

feature-login
feature-payment
fix-navbar
fix-login-error
update-homepage

Clear names make repositories easier to understand.

20. Creating a Branch with a Specific Commit

Suppose your history is:

A → B → C → D

You can create a branch from commit B:

git branch old-version B

The new branch will point to commit B.

21. Creating a Branch from HEAD

You can explicitly create a branch from the current HEAD:

git branch feature-login HEAD

In normal usage, the commit argument can be omitted because a new branch is created from the current commit by default.

22. Creating a Branch with a Remote Branch as Starting Point

A local branch can be created from a remote-tracking branch:

git branch feature-login origin/main

This creates a local branch starting at the commit referenced by origin/main.

23. Branch Creation Does Not Copy Files

A branch is a reference to a commit. Creating a branch does not create a separate physical copy of the entire project directory.

git branch feature-login

Git stores repository objects efficiently and branches simply point to commits.

24. Creating a Branch After a Commit

git add .
git commit -m "Add homepage"

git switch -c feature-contact

The new branch starts from the commit containing the homepage changes.

25. Creating a Branch Before Making Changes

A common development workflow is to create a feature branch before starting the feature:

git switch main
git pull
git switch -c feature-contact

This keeps the new work separate from the main branch.

26. Common Branch Creation Mistakes

  • Creating a branch from the wrong starting commit.
  • Forgetting that git branch does not switch branches.
  • Using unclear branch names.
  • Starting feature work on the wrong branch.
  • Forgetting to check the current branch.
  • Creating unnecessary branches without a clear purpose.

27. Useful Branch Creation Commands

git branch
git branch feature-login
git switch feature-login
git switch -c feature-login
git branch feature-login 8f32abc
git branch feature-login develop
git branch --show-current
git status

28. Complete Feature Branch Workflow

# Check current branch
git branch

# Switch to main
git switch main

# Create and switch to feature branch
git switch -c feature-login

# Check current branch
git branch --show-current

# Make changes
git add .

# Commit changes
git commit -m "Add login feature"

29. Practical Example

git init

git add .
git commit -m "Initial project"

git branch feature-about

git branch

git switch feature-about

git add .
git commit -m "Add about page"

git status

Here, feature-about is created from the initial project commit and then used for developing the About page.

30. Summary of Creating Branches

Creating branches is one of the most important Git workflows. A branch gives you a separate development path from a starting commit.

git branch feature-name

git switch -c feature-name

Use git branch when you only want to create a branch. Use git switch -c when you want to create and switch to the branch immediately.

📌 Key Points

  • git branch branch-name creates a new branch.
  • Creating a branch does not automatically switch to it.
  • git switch -c branch-name creates and switches to a branch.
  • A new branch normally starts from the current commit.
  • You can create a branch from a specific commit.
  • You can create a branch from another branch.
  • git branch --show-current displays the current branch.
  • Feature branches are useful for new development work.
  • Bug-fix branches are useful for isolated fixes.
  • Clear branch names make projects easier to manage.
  • Branches can develop independently after they diverge.
  • After development, branches can be merged using Git merge.

🧠 Quick Quiz

Question: Which command creates a new branch and switches to it?