Basics of Git Branching
Introduction
GitHub is a powerful platform for hosting code, collaborating on projects, and automating workflows. While most of us are familiar with the basics—creating repositories, committing code, and submitting pull requests—there’s a wealth of advanced features that can drastically improve our development workflow. In this blog, we’ll explore advanced GitHub practices such as branch management, customizing pull requests and merging PRs, GitHub Actions, and collaboration best practices that will help you take full advantage of this platform.
For basics of GitHub visit my previous blog on GitHub Basics.
Branching is a core concept in Git, allowing you to create separate lines of development, work on features or fixes independently, and then merge those changes back into the main codebase. This is crucial for parallel development without interfering with the main production branch.
Basic Git Branching Concepts:
Main Branch (main or master): The stable production-ready code.
Feature Branches: Branches created for new features or bug fixes.
Develop Branch: Used in more advanced workflows, it holds all completed features and serves as a staging area before release.
Release and Hotfix Branches: For managing releases and emergency fixes.
Basic Git Branching Commands
- Creating a branch in Git, we can use the following command:
git branch <branch-name>
For example, to create a branch for new feature:
git branch feature
- Switching between branches:
The above code will create the branch but will not switch to it.
To switch to the new branch created use the following command:
git checkout feature #branch name-feature
Creating and Switching to a branch:
We can combinedly create and switch to a branch through one command using “-b“ flag:
git checkout -b feature
Merging Branches
Once we’re done working on your feature branch, we need to merge it back into the main branch. First, we need to make sure we’re on the branch we want to merge into (here main), and then run the following command:
git checkout main #swich to the main branch
git merge feature #merge the changes in feature in main branch
Deleting Branches
After merging a branch, we might want to delete it.
To delete a branch, use the following code:
To delete the local feature branch:
git branch -d feature
If the branch hasn’t been merged yet, and we still want to delete it:
git branch -D feature
Deleting a Remote Branch:
To delete a branch on the remote repository (e.g., GitHub), use:
git push origin --delete feature
Viewing Branches
Listing Branches
To view a list of all branches in your local repository:
git branch
To see both local and remote branches:
git branch -a
Example: Workflow with Git Commands
Clone the repo(if required):
git clone https://github.com/your-username/your-repository.git
Create and switch to a new branch:
git checkout -b feature
Make changes
(edit files as required)
Stage changes:
git add .
Commit changes:
git commit -m "initial commit"
Push the branch:
git push origin feature
Create a Pull Request (through GitHub).
Merge the PR (through GitHub or locally).
Delete the feature branch (locally and remotely).
#local delete: git branch -d feature #Remote delete: git push origin --delete feature
Pull latest changes on main branch:
git pull origin main
Conclusion
Branching is an essential part of Git and allows for parallel development without breaking the main codebase. By understanding how to create, switch, merge, and delete branches, we can significantly improve our workflow. With these tools, we’ll be able to work more effectively in teams and ensure a clean, maintainable codebase.
All the best!!
Subscribe to my newsletter
Read articles from Sheetal Bajaj directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Sheetal Bajaj
Sheetal Bajaj
Passionate IT undergraduate with a focus on "web development," "data science," "AI and DSA". I love sharing insights and building solutions that empower the tech community. Constantly learning and experimenting with DSA in C++,web dev and google cloud. Interests:Open Source, Full-Stack Development, Machine Learning. Currently Exploring: DSA in C++,web dev and google cloud.