In the world of software development, Git has emerged as an indispensable tool that empowers teams to collaborate seamlessly and manage their projects efficiently. Among the array of features Git offers, branches stand out as a pivotal concept that can fundamentally reshape the way developers work on projects. In this comprehensive guide, we'll take a deep dive into the realm of Git branches, exploring their significance, mechanics, and practical applications that enable developers to harness their power effectively.

Understanding Git Branches

At its core, a Git branch is a virtual timeline of a project's development. Imagine your project as a tree: the main trunk represents the stable, production-ready codebase, while branches are the offshoots where developers can freely experiment, implement new features, fix bugs, or make improvements without disrupting the main codebase.

When you create a branch, you're essentially taking a snapshot of the current state of the project. This snapshot becomes an independent environment where you can make changes, commits, and test your ideas without affecting the stability of the main codebase. Each branch exists in its own isolated space, allowing developers to work concurrently on different features or tasks without stepping on each other's toes.

Think of branches as alternate realities within your project. You can work on a specific feature in one branch, while a colleague addresses a bug in another. This isolation prevents changes from intertwining prematurely, and once your feature or bug fix is ready, you can seamlessly merge your branch back into the main codebase, incorporating your changes without disrupting the overall project.

Understanding git branches

Branch Commands

Let's say you created a new project, add files to staging and created the initial commit. You continue working on the project and at a point you decide to implement a new feature on the project. Implementing this new feature involves a major change in the code and you are not sure if you will continue with that feature, you just want to test and see how it looks.

For that you create a new branch on your local repository called feature1

git branch feature1	#creates a new branch called feature1

However, you may notice that you are still on the main branch, despite you created a new one. To change to the new branch and start creating commits on that one, you need to checkout to the new branch by using this command:

git checkout feature1	#switches from the current branch to the feature1 branch

There is a more elegant way for creating a new branch and switching to it automatically, by using git checkout instead of git branch command:

git checkout -b feature1 	#creates a new branch called feature1 and switches into it

To delete a branch, if you created it by mistake or something, you can run the git branch command with the -D flag and the name of the branch.

git branch -D branchname 	#deletes the branch with the name branchname

This image shows the git commands used to perform various actions on branches while visualising the branches.

This image shows the git commands used to perform various actions on branches while visualising the branches.

Key Concepts and Terminology

Before delving deeper into the practical aspects of using Git branches, it's important to familiarize yourself with key concepts:

  • Master/Main Branch: This is often the default branch, representing the stable and production-ready version of the project.
  • Feature Branches: Created to develop specific features or functionalities independently. These branches are eventually merged into the main branch.
  • Release Branches: Used for preparing a version of the software for release, often including final bug fixes and optimizations.
  • Hotfix Branches: Created to address critical issues in the production code and are intended for quick fixes.
  • Merging: The process of integrating changes from one branch into another.
  • Conflicts: Occur when Git cannot automatically merge changes and requires manual intervention.

Pull Requests And Merges

Let's say you created a new branch called feature1 and implemented a new feature in your app. Everything works well and now you want to merge that changes into the main branch. There are two ways to do this:

  • Merge the feature1 branch into the main branch locally and push the final result (if you are the only one working at this project it's fine)
  • Commit all changes in the feature1 branch, push feature1 branch and create a pull request from feature1 branch into main branch (used in teams with multiple devs working on the same project).

Merge Locally and Push

For the first scenario, the steps are very simple, after commiting the changes, you checkout into the main branch and use git merge command to merge feature1 branch into the main branch:

  1. Make sure you commited the changes:
  2. git add .					#add all the files you changed/created
    git commit -m "feature1 implemented" 		#commit the changes you've made
    
  3. Checkout from feature1 branch into the main branch (from the branch with the new feature into the original branch):
  4. git checkout main 		#switch to the main branch or in the branch you want to bring the new features
    
  5. In the main branch (or in the branch you want to add the new implemeted feature) run git merge feature1:
  6. git merge feature1 		#merge the feature1 branch into the current branch
    

Merge by Creating a Pull Request

If you are working with a team you have to push your branch remotely and then create a pull request. The persons responsible with code review will review and aprove your branch and then your branch will be merged into the requested branch.

For GitHub:

  1. Make and Commit Changes: Make your code changes on the new branch. Once you're done with your changes, use git add to stage your changes and git commit to commit them with a descriptive message.
  2. git add .					#add all the files you changed/created
    git commit -m "feature1 implemented" 		#commit the changes you've made
    
  3. Push Changes: Push your branch with the changes to your GitHub repository using git push origin branch-name.
  4. git push origin feature1					#push the new branch to the remote repository
      
    # if you previously connected your local feature1 branch to the remote branch you can use only git push command:
    
    git push 		
    
  5. Create a Pull Request: Go to the original repository on GitHub, and you should see a prompt to create a pull request from your recently pushed branch. Click on it, and you'll be taken to the pull request creation page.
  6. Review and Comment: Add a title and description for your pull request, explaining the changes you've made. You can also tag relevant team members for review. They'll be able to review your code, leave comments, and suggest changes.
  7. Resolve Feedback: If there are comments or suggested changes, make the necessary adjustments in your local branch and push the changes. The pull request will automatically update with the new changes.
  8. Merge Pull Request: Once the code is reviewed and approved, you can merge the pull request. If using the "Merge" button on the GitHub pull request page, you can choose to squash, rebase, or merge the changes as is.

For Bitbucket:

  1. Make and Commit Changes: Make your code changes on the new branch. Stage the changes using git add and then commit them using git commit with a clear message.
  2. git add .					#add all the files you changed/created
    git commit -m "feature1 implemented" 		#commit the changes you've made
    
  3. Push Changes: Push your branch with the changes to your Bitbucket repository using git push origin branch-name.
  4. git push origin feature1					#push the new branch to the remote repository
      
    # if you previously connected your local feature1 branch to the remote branch you can use only git push command:
    
    git push 		
    
  5. Create a Pull Request: Navigate to the Bitbucket repository in your browser. Click on the "Create pull request" button for your branch. Fill in the details, including a title and description for the pull request.
  6. Review and Comment: Team members can review your changes and leave comments directly on the pull request. You can respond to comments and make further updates to your code if needed.
  7. Resolve Feedback: Similar to GitHub, update your code based on the feedback received. Commit the changes locally and push them to your branch.
  8. Merge Pull Request: Once the pull request is approved, you can merge it. Bitbucket offers options to merge with different strategies like merge commit, squash, or rebase, similar to GitHub.

Benefits of Utilizing Git Branches

The advantages of incorporating Git branches into your development workflow are manifold:

  • Parallel Development: Developers can work on multiple features or fixes simultaneously, leading to increased productivity and reduced wait times.
  • Isolation: Branches offer a controlled environment for experimenting and implementing changes without affecting the stability of the main codebase.
  • Collaboration: Teams can collaborate seamlessly by working on separate branches and merging changes when ready.
  • Code Quality: Isolating features or fixes in dedicated branches enables thorough testing and validation before integration.
  • Versioning: Branches facilitate the management of different versions of a project, ensuring older releases remain intact while new features are developed.

Useful git commands used for branching

This is a list of commands used when creating git branches:

git branch 					#shows the list of available branches

git branch branchname 				#creates a new branch with the name branchname

git checkout branchname 			#swithces into the branch with the name branchname

git checkout -b branchname 			#creates a new branch with the name branchname and swithces into it

git checkout branchname 			#swithces into the branch with the name branchname

git merge branchname 				#merges the branchname into the branch you are currently in

git push origin branchname 			#push the new branch to the remote repository

git push --set-upstream origin branchname	#connect your local branch to the remote one and push it there.


Git branches, while seemingly complex at first, become an invaluable asset once understood and embraced. They empower developers to navigate the intricate landscape of software development with confidence, enabling efficient collaboration, risk-free experimentation, and meticulous version control. By adopting Git branches and following best practices, you equip yourself with a dynamic toolkit that streamlines development, fosters collaboration, and paves the way for innovation. So go ahead, create, merge, and embark on your coding journey, knowing that Git branches are your steadfast companions on the road to success.