Git Branching Overview
Posted in Devops, Git on October 5, 2024 by achaggar007 ‐ 6 min read

In this blog post, we will explore various strategies and best practices for managing branches in Git. Effective branch management is crucial for maintaining a clean and efficient workflow in software development projects.
Why Branch Management Matters?
Branch management helps in organizing work, collaborating with team members, and maintaining code quality. Proper branch management can prevent conflicts and make the development process smoother.
The key branch types include the main (or master) branch, which holds stable, production-ready code; feature branches for developing new features; and develop branches (used in Git Flow) for integrating completed work. Release branches are used to prepare for new releases, while hotfix branches address urgent issues in production. Branches enable parallel development, experimentation, and easy merging of changes back into the main codebase.
Real World Analogy
“Git branching is like working on different dishes in a kitchen. The main branch is the final meal being served. Each chef (developer) takes a separate branch to work on individual components (like appetizers, entrees, or desserts). They prepare their dish independently without affecting others. Once each dish is ready, they are brought together (merged) into the main course for a complete meal, ensuring everything fits perfectly together without disrupting the ongoing preparation.”
Branching Strategies
Feature Branching
Feature branching involves creating a new branch for each feature or task. This allows developers to work on features independently without affecting the main codebase.
Release Branching
Release branching is used to prepare a codebase for a new release. A release branch is created from the main branch, and only bug fixes and release-specific changes are made to this branch.
Hotfix Branching
Hotfix branches are used to quickly address critical issues in the production environment. These branches are created from the main branch and merged back into both the main and development branches after the fix is applied.
Best Practices
Consistent Naming Conventions
Use clear and consistent naming conventions for branches to make it easier to understand the purpose of each branch.
Regular Merging
Regularly merge changes from the main branch into feature branches to keep them up-to-date and reduce the risk of conflicts.
Code Reviews
Implement code reviews to ensure that all changes meet the project’s quality standards before merging them into the main branch.
Tools for Branch Management
Git Flow
Git Flow is a branching model that defines a strict branching strategy designed around the project release. It provides a robust framework for managing branches.
GitHub Flow
GitHub Flow is a simpler branching model that focuses on continuous delivery. It is ideal for projects that require frequent deployments.
GitLab Flow
GitLab Flow combines feature-driven development and release management. It offers flexibility and integrates well with CI/CD pipelines.
Git Commands for Branch Management
Creating a New Branch
To create a new branch, use the following command:
git checkout -b <branch-name>
Switching Between Branches
To switch to an existing branch, use:
git checkout <branch-name>
Merging Branches
To merge a branch into the current branch, use:
git merge <branch-name>
Deleting a Branch(es)
On local env
#To delete a branch that is no longer needed, use:
git branch -d <branch-name>
#To delete the unmerged branch, use:
git branch -D <branch-name>
#Bulk delete merged branches on local at once:
git branch --merged | egrep -v "(^\*|master|dev)" | xargs git branch -d
#Bulk delete un-merged branches on local at once:
git branch --no-merged | egrep -v "(^\*|master|dev)" | xargs git branch -D
Note: Please review and use the unmerged bulk delete carefully as it will remove all the unmerged branches and their changes.
On Remote
# Dry run to list remote branches would be pruned
git remote prune <remote> --dry-run
# Delete the listed remote branches would be pruned
git remote prune <remote>
# Delete a specific branch on the remote
git push <remote> --delete <branch>
# Bulk delete merged branches on the remote
git branch -r --merged | egrep -v "(^\*|master|dev)" | sed 's/origin\///' | xargs -n 1 git push origin --delete
# Bulk delete unmerged branches on the remote
git branch -r --no-merged | egrep -v "(^\*|master|dev)" | sed 's/origin\///' | xargs -n 1 git push origin --delete
Caution: Use with extereme care, it will remove branches remotely for unmerged changes
Caution: Use with extereme care, it will remove branches remotely for unmerged changes
Viewing Branches
To list all branches in the repository, use:
git branch
Pushing a Branch to Remote
To push a local branch to the remote repository, use:
git push origin <branch-name>
Pulling Changes from Remote
To pull changes from the remote branch to your local branch, use:
git pull origin <branch-name>
Fetching Updates from Remote
To fetch updates from the remote repository without merging them, use:
git fetch
Restoring Files
To restore a specific file to its state in the last commit, use:
git restore <file-name>
Discarding Local Changes
To discard all local changes in the working directory, use:
git checkout -- .
Cleaning Untracked Files and Directories
To remove all untracked files and directories from the working directory, use:
git clean -fd
Resetting Commits
The git reset
command is used to undo changes by resetting the current branch to a specific state. There are three main variations of git reset
:
Soft Reset
A soft reset moves the HEAD to a specified commit but leaves the working directory and index unchanged. This is useful if you want to uncommit changes but keep them staged for a new commit.
git reset --soft <commit>
Mixed Reset
A mixed reset (the default option) moves the HEAD to a specified commit and resets the index, but leaves the working directory unchanged. This unstages changes but keeps them in the working directory.
git reset --mixed <commit>
Hard Reset
A hard reset moves the HEAD to a specified commit and resets both the index and working directory to match that commit. This will discard all changes in the working directory and index.
git reset --hard <commit>
Conclusion
Git branching is a powerful feature that allows developers to create isolated environments for feature development, bug fixes, and experimentation without affecting the main codebase. By utilizing branches, teams can work concurrently on different tasks, streamline collaboration, and maintain a cleaner project history. Effective branching strategies, such as Git Flow or trunk-based development, can enhance productivity and ensure a smooth integration process when merging changes back into the main branch. Overall, mastering Git branching is essential for efficient version control and collaborative software development.