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.
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.
\
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
Lists local branches and marks the currently checked-out branch with an asterisk.
Create a New Branch
Creates a branch named feature/login
without switching to it.
Switch to a Branch
Switches your working directory to the selected branch.
Create and Switch in One Command
Creates the new branch and switches to it immediately.
6. A Typical Branching Workflow
- Start from the appropriate base branch.
- Create a feature branch.
- Make changes and commit your work.
- Test your changes and review the code.
- Push the branch to the remote repository if needed.
- Merge the completed work into the target branch according to your team's workflow.
$ 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.