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

<code>git branch feature/create-article</code>

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

<code>git checkout feature/create-article</code>

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.

<code>git switch feature/create-article</code>

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

<code>git checkout -b feature/create-article-2</code>

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

Modern alternative:

<code>git switch -c feature/create-article-2</code>

4. List all branches

<code>git branch</code>

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

<code>  feature/create-article
* feature/create-article-2
  main</code>

To list remote branches:

<code>git branch -a</code>

5. Rename a branch

<code>git branch -m feature/create-article-2 feature/create-article-1</code>

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

<code>git branch -m feature/create-article-1</code>

6. Safely delete a local branch

<code>git branch -d feature/create-article-1</code>

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

7. Force‑delete a branch (dangerous)

<code>git branch -D feature/create-article</code>

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

<code>git merge feature/create-article</code>

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:

<code>git checkout main
git merge feature/create-article</code>

2. Resolve merge conflicts

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

<code>&lt;&lt;&lt;&lt;&lt;&lt;&lt; HEAD
(current branch changes)
=======
(changes from the branch being merged)
&gt;&gt;&gt;&gt;&gt;&gt;&gt; main</code>

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:

<code>git add .</code>

Commit the merge:

<code>git commit -m "Resolve conflict files."</code>

To abort the merge and return to the previous state:

<code>git merge --abort</code>

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.

<code>git checkout main
git merge feature/create-article</code>

2. Three‑way merge (forked branches)

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

<code>git checkout main
git merge feature/create-article</code>

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

<code>git branch -r</code>

Shows only remote branches prefixed with origin/ .

2. Create a local branch that tracks a remote branch

<code>git checkout --track origin/dev</code>

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

3. Update remote branches

<code>git fetch</code>

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

4. Merge remote changes into the current branch

<code>git pull origin main</code>

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

<code>git push --set-upstream origin dev</code>

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

<code>git push origin --delete dev</code>

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

<code>git remote prune origin</code>

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.

<code>git checkout -b feature/create-article</code>

After development, merge the feature branch into main or dev :

<code>git checkout dev
git merge feature/create-article</code>

2. Hot‑fix branches

Hot‑fixes address urgent production bugs, usually based on main .

<code>git checkout -b hotfix/main-article dev</code>

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 .

<code>git checkout -b release/1.0.0 dev</code>

Test, then merge into main and tag the release:

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

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

<code>git checkout dev
git merge release/1.0.0</code>

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

workflowgitVersion Controlcommandsbranching
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

login 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.