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.
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-articleThis 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-articleSwitches 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-article3. Create and switch to a new branch in one step
git checkout -b feature/create-article-2Creates the branch and immediately checks it out, saving time.
Modern alternative:
git switch -c feature/create-article-24. List all branches
git branchShows all local branches; the current branch is marked with *.
feature/create-article
* feature/create-article-2
mainTo list remote branches:
git branch -a5. Rename a branch
git branch -m feature/create-article-2 feature/create-article-1Renames 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-16. Safely delete a local branch
git branch -d feature/create-article-1Deletes 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-articleDeletes 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.
1. Merge a branch into the current branch
git merge feature/create-articleIntegrates 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-article2. 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)
>>>>>>> mainSteps 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 --abortGit 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-article2. Three‑way merge (forked branches)
When branches have diverged, Git creates a new merge commit.
git checkout main
git merge feature/create-articleRemote 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 -rShows only remote branches prefixed with origin/.
2. Create a local branch that tracks a remote branch
git checkout --track origin/devCreates a local dev branch that automatically tracks origin/dev.
3. Update remote branches
git fetchDownloads updates from the remote without applying them to your working directory.
4. Merge remote changes into the current branch
git pull origin mainEquivalent 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 devThe --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 devRemoves origin/dev from the remote repository. To clean up local references to deleted remote branches:
git remote prune originBranch 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-article2. 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.0By using these branching strategies, you can manage feature development, urgent fixes, and stable releases efficiently.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Code Mala Tang
Read source code together, write articles together, and enjoy spicy hot pot together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
