Fundamentals 15 min read

Master Git Branching: Essential Commands and Workflows Explained

This comprehensive guide explains what Git branches are, why they are essential for isolation, collaboration, safe experimentation and version control, and walks you through creating, managing, merging, and syncing local and remote branches with practical command examples and workflow recommendations.

Code Mala Tang
Code Mala Tang
Code Mala Tang
Master Git Branching: Essential Commands and Workflows Explained
Git branching illustration
Git branching illustration

What is a Git branch?

Git branches are independent lines of development within a repository, acting like separate workspaces where you can make changes without affecting the main (or default) branch. They enable developers to develop new features, fix bugs, or experiment safely.

Why use branches?

Branches provide several benefits:

Isolation : Separate tasks such as features, bug‑fixes, or experiments.

Collaboration : Multiple developers can work on different branches without interfering with each other.

Safe experimentation : Test changes without impacting production code.

Version control : Easily roll back or switch between different project versions.

The default branch in Git is usually called main . New branches can be created for specific purposes, such as:

Feature branch (e.g., feature/new-ui) for developing new functionality.

Hotfix branch (e.g., hotfix/login-fix) for fixing production issues.

Release branch (e.g., release/v1.2.0) for preparing a stable version for deployment.

Creating and managing local branches

1. Create a new branch

git branch feature/create-article

This creates a new branch named feature/create-article without switching to it, useful when you want to prepare several branches before working on them.

2. Switch to a branch

git checkout feature/create-article

Switches your working directory to the specified branch so you can start coding.

Modern alternative : From Git 2.23 onward you can use git switch instead of checkout for a cleaner experience.

git switch feature/create-article

3. Create and switch to a new branch in one step

git checkout -b feature/create-article-2

Creates the branch and immediately checks it out, saving time.

Modern alternative:

git switch -c feature/create-article-2

4. List all branches

git branch

Shows all local branches; the current branch is marked with *.

feature/create-article
* feature/create-article-2
  main

To list remote branches:

git branch -a

5. Rename a branch

git branch -m feature/create-article-2 feature/create-article-1

Renames feature/create-article-2 to feature/create-article-1. If you are already on the branch, you can simply run:

git branch -m feature/create-article-1

6. Safely delete a local branch

git branch -d feature/create-article-1

Deletes the branch only if it has been merged; otherwise Git prevents deletion to avoid data loss.

7. Force‑delete a branch (dangerous)

git branch -D feature/create-article

Deletes the branch even if it contains unmerged changes—use with caution.

Merging branches

Merging integrates changes from one branch into another, typically bringing a feature or hotfix into main.

Git merge illustration
Git merge illustration

1. Merge a branch into the current branch

git merge feature/create-article

Integrates the changes from feature/create-article into the branch you are currently on. If there are no conflicts, Git completes the merge automatically.

Ensure you are on the correct branch before merging:

git checkout main
git merge feature/create-article

2. Resolve merge conflicts

If conflicts arise, Git pauses the merge and marks conflicted files with conflict markers:

<<<<<<< HEAD
(current branch changes)
=======
(changes from the branch being merged)
>>>>>>> main

Steps to resolve:

Open the conflicted file in an editor (e.g., VS Code).

Manually edit to keep the correct code.

Mark the file as resolved: git add . Commit the merge: git commit -m "Resolve conflict files." To abort the merge and return to the previous state:

git merge --abort

Git merge types

Git supports different merge strategies depending on branch history.

1. Fast‑forward merge (no fork)

When the target branch has no new commits, Git simply moves the branch pointer forward.

git checkout main
git merge feature/create-article

2. Three‑way merge (forked branches)

When branches have diverged, Git creates a new merge commit.

git checkout main
git merge feature/create-article

Remote branches and tracking

Remote branches are versions stored on a remote repository (GitHub, GitLab, Bitbucket) that enable collaboration and backup.

1. List remote branches

git branch -r

Shows only remote branches prefixed with origin/.

2. Create a local branch that tracks a remote branch

git checkout --track origin/dev

Creates a local dev branch that automatically tracks origin/dev.

3. Update remote branches

git fetch

Downloads updates from the remote without applying them to your working directory.

4. Merge remote changes into the current branch

git pull origin main

Equivalent to git fetch followed by git merge origin/main. If the branch already tracks the remote, a simple git pull suffices.

5. Push a new local branch to the remote

git push --set-upstream origin dev

The --set-upstream flag establishes tracking so future git pull and git push commands can omit the remote branch name.

6. Delete a remote branch

git push origin --delete dev

Removes origin/dev from the remote repository. To clean up local references to deleted remote branches:

git remote prune origin

Branch workflow (feature, hotfix, release)

This guide outlines typical workflows for using Git branches in feature development, hot‑fixes, and releases.

1. Feature branches

Feature branches let you develop new functionality in isolation. git checkout -b feature/create-article After development, merge the feature branch into main or dev:

git checkout dev
git merge feature/create-article

2. Hot‑fix branches

Hot‑fixes address urgent production bugs, usually based on main. git checkout -b hotfix/main-article dev After fixing, merge the hot‑fix back into main (and optionally dev).

3. Release branches

Release branches prepare a new version for production, based on dev. git checkout -b release/1.0.0 dev Test, then merge into main and tag the release:

git checkout main
git merge release/1.0.0
git tag -a v1.0.0 -m "Release 1.0.0"

Finally, merge the release back into dev to keep it up‑to‑date:

git checkout dev
git merge release/1.0.0

By using these branching strategies, you can manage feature development, urgent fixes, and stable releases efficiently.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

workflowGitVersion Controlcommands
Code Mala Tang
Written by

Code Mala Tang

Read source code together, write articles together, and enjoy spicy hot pot together.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.