Lesson 20 of 60 – Git Branches
33%

Git Branches

A Git branch is an independent line of development inside a Git repository. Branches allow developers to work on new features, bug fixes, experiments, and other changes without directly changing the main development line.

Note: Branches are lightweight in Git and make it easy to work on different versions of a project independently.

1. What is a Git Branch?

A branch is a movable pointer to a commit in a Git repository. It provides an independent development path for your work.

main
  |
  A → B → C

The branch name points to the latest commit in that development line.

2. Why Use Branches?

Branches allow developers to work on different tasks without mixing changes unnecessarily.

  • Develop new features
  • Fix bugs
  • Experiment with ideas
  • Prepare releases
  • Work with other developers
  • Keep different development tasks separated

3. Main Branch

The main development branch is commonly named main. Some older repositories may use master.

main
 |
 A → B → C

The exact branch name is determined by the repository configuration and workflow.

4. Branches Are Pointers

A branch is not a complete copy of the entire repository. Git stores a branch as a reference pointing to a commit.

main → C

When a new commit is created on that branch, the branch pointer moves forward.

5. Viewing Branches

Use the following command to list local branches:

git branch

The current branch is normally marked with an asterisk (*).

6. Example of git branch

$ git branch

* main
  feature-login
  bug-fix

In this example, main is the currently checked-out branch.

7. Creating a Branch

Use git branch followed by a branch name to create a new branch.

git branch feature-login

This creates the branch but does not automatically switch to it.

8. Creating and Switching to a Branch

You can create a branch and switch to it using:

git switch -c feature-login

The -c option tells Git to create the branch before switching to it.

9. Checking the Current Branch

Use:

git branch --show-current

This displays the name of the current branch.

feature-login

10. Branch and Commit History

A → B → C
         ↑
        main

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

A → B → C
         ↑
       main
         ↑
      feature

11. Making a Commit on a Branch

After switching to a branch, make changes and commit them normally.

git switch feature-login

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

The new commit is added to the current branch.

12. Branches Can Diverge

        C → D
       /
A → B
       \
        E → F

Different branches can develop different commits after they diverge from a common commit.

13. Switching Between Branches

Use git switch to move to another branch.

git switch main

git switch feature-login

Git updates the working tree to match the selected branch.

14. Older checkout Method

The older command for switching branches is:

git checkout feature-login

Modern Git provides git switch specifically for changing branches, while git checkout also has other historical uses.

15. Listing All Branches

To list local and remote-tracking branches:

git branch -a

This can display branches such as:

* main
  feature-login
  remotes/origin/main
  remotes/origin/feature-login

16. Viewing Branches with Last Commit

You can display branch names together with their latest commit using:

git branch -v

This is useful when you want to quickly inspect the current state of local branches.

17. Branch Names

Choose branch names that clearly describe the work.

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

Clear names make collaborative projects easier to understand.

18. Feature Branch

A feature branch is commonly used for developing a new feature.

git switch -c feature-login

You can develop and commit the feature without making those commits directly on the main branch.

19. Bug Fix Branch

A separate branch can be used to fix a problem in an application.

git switch -c fix-login-error

After completing the fix, the branch can later be merged into the appropriate development branch.

20. Deleting a Branch

A local branch that is no longer needed can be deleted using:

git branch -d feature-login

Git normally prevents deletion with -d when it detects that the branch contains commits that have not been merged.

21. Force Deleting a Branch

The -D option forces deletion of a local branch.

git branch -D feature-login
Warning: Use force deletion carefully because it can remove a branch reference even when Git considers its commits unmerged.

22. Renaming a Branch

To rename the current branch:

git branch -m new-name

For example:

git branch -m feature-auth

23. Branches and HEAD

HEAD identifies the commit or branch that Git is currently using.

HEAD → main → C

When you switch to another branch:

HEAD → feature-login → D

HEAD now refers to the selected branch.

24. Branches and Working Files

When you switch branches, Git updates tracked files in the working tree according to the selected branch.

git switch main

git switch feature-login

This allows each branch to have its own committed version of the project.

25. Branch Status

Use git status to see the current branch and working tree state.

git status

Example:

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

26. Common Branch Mistakes

  • Creating changes on the wrong branch.
  • Forgetting to check the current branch.
  • Using unclear branch names.
  • Deleting a branch before checking its commits.
  • Switching branches with uncommitted changes without understanding the result.
  • Accidentally committing feature work directly to the main branch.

27. Useful Branch Commands

git branch
git branch --show-current
git branch -a
git branch -v
git branch feature-login
git switch feature-login
git switch -c feature-login
git branch -d feature-login
git branch -D feature-login
git branch -m new-name

28. Complete Branch Workflow

# Check current branch
git branch

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

# Make changes
# Stage changes
git add .

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

# Check branches
git branch

29. Practical Branch Example

git init

git add .
git commit -m "Initial commit"

git switch -c feature-homepage

# Make homepage changes

git add .
git commit -m "Create homepage feature"

git switch main

git branch

Here, the homepage work is developed separately from the main branch.

30. Summary of Git Branches

Git branches provide separate development paths inside a repository. They allow developers to work on features, fixes, and experiments without directly mixing every change into the main branch.

git branch
git switch -c feature-name
git switch main

After creating and working on a branch, it can later be merged into another branch using Git's merge functionality.

📌 Key Points

  • A Git branch is a movable pointer to a commit.
  • Branches provide separate development paths.
  • git branch lists local branches.
  • git branch feature-name creates a branch.
  • git switch feature-name switches branches.
  • git switch -c feature-name creates and switches to a branch.
  • git branch --show-current shows the current branch.
  • git branch -a lists local and remote-tracking branches.
  • git branch -d deletes a local branch safely when Git allows it.
  • git branch -D force deletes a local branch.
  • git branch -m renames a branch.
  • Feature and bug-fix branches help separate development work.
  • Branches can diverge and later be merged.

🧠 Quick Quiz

Question: Which command creates a new branch without switching to it?