Master Git & GitHub From Scratch

Clear, practical, and structured lessons designed to take you from Git fundamentals to real-world GitHub collaboration.

```cshtml

Git Branching Concepts

Git branching allows developers to work on different features, bug fixes, and experiments independently without directly affecting the main development branch.

In this lesson, you will learn what branches are, why developers use them, and how branching supports a collaborative development workflow.

1. What Is a Git Branch?

A Git branch is a lightweight, movable pointer to a commit. It identifies a line of development in your repository.

Branches allow you to develop a new feature or fix a bug separately from the main line of development. When your work is ready, you can integrate it into another branch using a merge or another integration workflow.

Simple example: Imagine your application is working correctly. You want to add a login feature. Instead of changing the main branch directly, you create a separate branch, implement the login feature, test it, and then merge the completed changes.

2. Why Are Branches Important?

  • Independent development: Work on new features without directly changing the main branch.
  • Safer bug fixes: Isolate a fix and test it before integrating it.
  • Team collaboration: Different developers can work on separate branches.
  • Experimentation: Try new ideas without immediately affecting the primary development line.
  • Organized workflow: Keep feature development, releases, and fixes separated when the project requires it.

3. Common Types of Branches

Branch Type Purpose Example Name
Main Branch The primary development or release branch, depending on the team's workflow. main
Feature Branch Used to develop a new feature independently. feature/login
Bug-Fix Branch Used to correct a specific problem. fix/login-error
Release Branch Used to prepare and stabilize a release when the team's workflow requires it. release/1.0

4. Understanding Branches Visually

Consider a project with a main branch and a separate feature branch. Both branches can point to different commits as development progresses.

main: A --- B --- C
  \
feature: D --- E

In this simplified diagram, commits D and E represent work developed on the feature branch after it diverged from the main branch at commit B.

Once the feature is complete, its changes can be integrated into the main branch.

5. Basic Git Branch Commands

Git provides simple commands for creating, listing, and switching branches.

View Existing Branches
$ git branch

Lists local branches and marks the currently checked-out branch with an asterisk.

Create a New Branch
$ git branch feature/login

Creates a branch named feature/login without switching to it.

Switch to a Branch
$ git switch feature/login

Switches your working directory to the selected branch.

Create and Switch in One Command
$ git switch -c feature/login

Creates the new branch and switches to it immediately.

6. A Typical Branching Workflow

  1. Start from the appropriate base branch.
  2. Create a feature branch.
  3. Make changes and commit your work.
  4. Test your changes and review the code.
  5. Push the branch to the remote repository if needed.
  6. Merge the completed work into the target branch according to your team's workflow.
$ git switch main
$ git pull
$ git switch -c feature/login
$ git add .
$ git commit -m "Add login feature"
$ git push -u origin feature/login

These commands assume that the repository has a remote named origin, the main branch is named main, and the changes are ready to commit. Review git status before staging files.

Key Takeaway

Git branches provide separate lines of development within a repository. They help developers isolate features and bug fixes, collaborate safely, and integrate changes when the work is ready.

```