Skip to main content

Branches

About 3 min

Branches

In Git, a branch is simply a lightweight, movable pointer to one of your commits. When you create a branch, Git just creates a new pointer that references the same commit you're currently on.

  • The main Branch: By convention, your primary stable line of development is called main. This pointer indicates the latest, most reliable snapshot of your project.
  • The HEAD Pointer: This special pointer shows you which branch you are currently on. When you switch branches, Git moves the HEAD pointer.

Why Branching is Essential in Team Projects

Branching isn't just a convenience; it's a core requirement for collaborative development.

  1. Isolation and Stability: If one person introduces a half-finished feature or a bug that breaks the build, it instantly halts progress for everyone else. By using a feature branch, a developer can experiment and commit incomplete code in isolation without affecting the stable main branch or any other developer's work.
  2. Parallel Development: Branches enable multiple people to work on different tasks simultaneously (e.g., Feature A, Bug Fix B, and Experiment C) without stepping on each other's toes. Only when a task is fully complete and approved is it integrated via a merge.
  3. Code Review: Branches provide the mechanism for modern code review. A feature branch is pushed to a remote server (like GitHub) where it can be opened as a Pull Request (PR) or Merge Request (MR). The team can review the changes before the code is integrated.
  4. Instant Revert: If a merged feature later causes a major problem, Git can easily revert the single merge commit, wiping out that feature's changes without undoing hours of subsequent work done by other developers.

Branching Naming Conventions

Using consistent names is vital for clarity, especially in large teams. A good name tells you what the branch is for and who created it.

The general format is often: <type>/<issue-id>-<short-description>

Branch TypeNaming Convention ExamplePurpose
Featurefeature/user-auth-api or feat/TICKET-123-login-formFor developing new functionality. Often prefixed with feature/ or feat/.
Bugfixbugfix/issue-45-fix-homepage-crash or fix/TICKET-88-css-bugFor fixing problems in the existing code. Often prefixed with bugfix/ or fix/.
Hotfixhotfix/v1.2.1-critical-patchFor urgent fixes that need to be applied directly to the stable release (as seen in Gitflow).
Chorechore/update-deps or refactor/cleanup-dashboardFor non-code changes like updating dependencies, config files, or internal refactoring.

Branching Workflow: Isolate and Merge

CommandActionExplanation
git branch <name>Create BranchCreates a new pointer (branch) at the current commit. It does not switch you to the new branch.
git switch <name> or git checkout <name>Switch BranchMoves the HEAD pointer to the specified branch. Your Working Directory files are instantly updated to match the snapshot the new branch points to.
git switch -c <name> or git checkout -b <name>Create & SwitchThe common shortcut: creates a new branch and immediately switches the HEAD pointer to it, ready for isolated development.
git log --oneline --decorateView BranchesA useful command for visualizing where all your branch pointers currently are in the commit history.

Integrating Changes: Merging

When your isolated feature is complete, stable, and tested, you must bring its changes back into your stable branch (e.g., main). This process is called merging.

The Merge Process

  1. Switch back to the branch you want to integrate the changes into (usually main): git switch main
  2. Merge the feature branch into the current branch: git merge <feature-branch-name>
CommandActionExplanation
git merge <branch>IntegrateCombines the commit history of the named branch into your currently checked-out branch.
git branch -d <name>Delete BranchRemoves the local branch pointer after it has been successfully merged. Since the commits are now part of the main history, the branch is no longer needed.
git push origin --delete <name>Delete RemoteRemoves the branch from the remote server (e.g., GitHub).

Merge Scenarios

  • Fast-Forward Merge: If the main branch hasn't changed since you created your feature branch, Git simply moves the main pointer forward to the last commit of your feature branch.
  • Three-Way Merge: If both main and your feature branch have new commits, Git creates a new commit (a merge commit) that combines the two lines of work.
  • Merge Conflict: If Git cannot figure out how to combine changes (e.g., both branches edited the same line of the same file), it pauses the merge, and requires you to manually resolve the conflict.

Remote Branch Synchronization

Just as you use git push for commits, you must use it for branches to share your isolated work or make new features accessible to collaborators.

CommandActionExplanation
git push -u origin <name>Share BranchPushes your newly created local branch to the remote server, establishing a link for future pushes.
git fetchUpdate Remote InfoDownloads all the newest remote references and branch pointers from the server, but does not modify your local files or branches.
git pullFetch & MergeDownloads the latest changes from the remote tracking branch and attempts to automatically merge them into your current local branch.