Chuck's Academy

Git Branching and Merging

Basic Concepts of Branches in Git

What Are Branches in Git?

Branches in Git are like parallel paths of development that allow developers to work on different features, bug fixes, or experiments in isolation. Each branch has its own set of commits, which allows you to keep the code separate until it's ready to be merged.

The 'main' or 'master' Branch

By convention, the main branch of a repository is called main or master. This branch typically contains the production code or stable versions of the project. When you initialize a new repository, Git automatically creates an initial branch called main (or master, depending on the configuration).

Creating Branches

To create a new branch, use the git branch command:

bash

This command creates a new branch from the current branch you are on.

Example:

bash

This command creates a new branch named feature-xyz.

Listing Branches

You can list all the branches in your repository using:

bash

This command will show all the branches, highlighting the branch you are currently on with an asterisk (*).

Switching Branches

To switch from one branch to another, use the git checkout command:

bash

Example:

bash

This command switches to the feature-xyz branch.

Working with Branches

By creating and switching branches, you can develop different features in isolation without affecting other branches. For example, you can have a stable main branch and create a new feature-xyz branch to work on a new feature. While working on feature-xyz, any changes you make and commit will be isolated from main until you decide to merge the branches.

Merging Branches

When a branch is ready to be integrated with another, you can merge it using the git merge command (details on merging in upcoming chapters).

Deleting Branches

Once you've merged a branch and no longer need it, you can delete it to keep your repository clean using:

bash

Example:

bash

This command will delete the feature-xyz branch.

Summary

  • Branches: Allow you to work on different parts of the project in isolation.
  • Creating: Use git branch <branch-name>.
  • Switching: Use git checkout <branch-name>.
  • Listing: Use git branch to see all branches.
  • Deleting: Use git branch -d <branch-name> to delete a branch.

Branch management is fundamental to effective collaboration and organized development, providing an environment where developers can experiment and work in parallel. In the following chapters, we will see how to create, delete, and navigate between branches in greater detail.


Ask me anything