Back to Git & Dev Tools
2026-03-219 min read

branch (Git & Dev Tools)

Learn branch (Git & Dev Tools) step by step with clear examples and exercises.

Title: Mastering Git Branching: A full guide for Developers

Why This Matters

Git branching is a fundamental skill for every developer, enabling teams to manage multiple features, bug fixes, and experiments simultaneously without affecting the main codebase. Understanding git branching can help you work more efficiently, collaborate better with your team, and avoid costly mistakes. This guide will delve deeper into the core concepts of git branching, common mistakes, practice questions, and frequently asked questions to help you master this essential tool.

Prerequisites

Before diving into Git branching, ensure you have a basic understanding of the following:

  • Git basics (installation, configuration, and initializing a repository)
  • Basic Git commands (add, commit, push, pull)
  • Understanding the Git workflow (branching, merging, and pulling/pushing)
  • Familiarity with common Git concepts such as staging area, commits, and tags

Core Concept

Creating a Branch

To create a new branch in Git, use the git branch command followed by the name of your new branch. For example:

git branch feature-x

This creates a new branch named feature-x but does not switch to it yet. To start working on this branch, use the git checkout command:

git checkout feature-x

Switching Between Branches

To switch between branches in Git, use the git checkout command followed by the name of the desired branch. For example:

git checkout master

This switches you back to the master branch.

Merging Branches

When you're ready to merge your changes into the main codebase, switch back to the master branch and use the git merge command followed by the name of the branch you want to merge:

git checkout master
git merge feature-x

If there are any conflicts during the merge, Git will prompt you to resolve them manually. To learn more about resolving merge conflicts, refer to the Git Merge Conflicts tutorial by Atlassian.

Deleting a Branch

Once you've merged your changes into the main codebase and verified everything works as expected, you can delete the branch using the git branch -d command:

git branch -d feature-x

However, if the branch has not been merged yet, you cannot delete it with this command. Instead, you'll need to use git branch -D (with a capital D), which forces deletion of the local branch even if it has unmerged changes:

git branch -D feature-x

Be cautious when using git branch -D, as this will permanently delete your work if it hasn't been merged.

Merge Strategies

Git offers various merge strategies to handle merges more efficiently. For example, the recursive strategy can help manage conflicts in complex projects with many contributors. To set a custom merge strategy for a specific repository, add the following line to your Git configuration file (usually located at ~/.gitconfig):

[merge "recursive"]
ourstrategy = recursive

Rebasing

Rebasing is another powerful Git feature that allows you to integrate changes from one branch onto another, creating a cleaner and more linear commit history. To learn more about rebasing, refer to the Git Rebasing tutorial by Atlassian.

Worked Example

Let's walk through an example of creating a new branch, making changes, merging it back into the main codebase, and deleting the branch using both git merge and git rebase.

  1. Initialize a new Git repository:
git init
  1. Create a new file called example.txt with some content:
echo "Hello World" > example.txt
  1. Commit the initial changes:
git add .
git commit -m "Initial commit"
  1. Create a new branch called feature-x and switch to it:
git branch feature-x
git checkout feature-x
  1. Edit the example.txt file to add some additional content:
echo "This is a feature branch" >> example.txt
  1. Commit the changes on the feature-x branch:
git add .
git commit -m "Added feature-specific content"
  1. Merge the feature-x branch into the main codebase using git merge:
git checkout master
git merge feature-x
  1. Verify that the changes have been merged successfully:
cat example.txt
  1. Delete the feature-x branch:
git branch -d feature-x
  1. Now, let's perform the same actions using git rebase instead of git merge. First, switch to the master branch:
git checkout master
  1. Rebase the feature-x branch onto the master branch:
git rebase feature-x
  1. Resolve any conflicts that may arise during the rebase process. Once complete, the changes from feature-x will be integrated into the main codebase.
  1. Delete the feature-x branch:
git branch -d feature-x

Common Mistakes

  1. Forgetting to create a new branch before making changes: This can lead to unwanted changes in the main codebase, making it difficult to manage multiple features or bug fixes simultaneously.
  2. Merging conflicts without resolving them: If there are conflicts during a merge, they must be resolved manually to avoid breaking the codebase.
  3. Deleting a branch that still has unmerged changes: Before deleting a branch, ensure all its changes have been merged into the main codebase to prevent data loss.
  4. Not using descriptive branch names: Using clear and concise branch names helps others understand what changes are being made in each branch.
  5. Merging too early or too late: Merging too early can result in unnecessary merges, while merging too late can lead to conflicts and delays.
  6. Ignoring merge conflicts: Failing to resolve merge conflicts can result in broken code and increased frustration for team members.
  7. Not using feature toggles or environment variables: When working on a new feature, it's important to isolate the changes to avoid affecting other parts of the application before they are ready to be merged into the main codebase.
  8. Not testing thoroughly after merging: It's crucial to test your changes thoroughly after merging them into the main codebase to ensure everything works as expected and catch any potential issues early.
  9. Not using git hooks for automated testing and linting: Git hooks can help automate tests and linting, ensuring that code quality remains high and reducing the risk of introducing bugs.
  10. Not using a version control system: Using a version control system like Git is essential for managing your codebase effectively, collaborating with others, and maintaining a clean commit history.

Practice Questions

  1. Create a new Git repository and create two branches: feature-a and feature-b. Make changes on each branch, merge them into the main codebase, and delete both branches using git merge and git rebase.
  2. You have a feature branch called feature-x with some unmerged changes. How can you ensure these changes are not lost when deleting the branch?
  3. Your teammate has created a new branch called bugfix-123. How can you view their changes without merging the branch into your current codebase?
  4. You have a feature branch with some unresolved merge conflicts. What steps should you take to resolve these conflicts and complete the merge?
  5. Your team is working on multiple features simultaneously, each on its own branch. How can you ensure that everyone's changes are merged in the correct order without causing unnecessary conflicts?
  6. You have a bugfix branch called bugfix-123 that needs to be merged into the main codebase but contains several unrelated changes. What steps should you take to isolate and merge only the necessary changes?
  7. Your team is using GitHub for version control, and you've noticed that your repository has a large number of small commits. How can you improve the commit history by squashing multiple commits into one?
  8. You're working on a new feature that requires changes to several files in your application. To avoid affecting other parts of the application before they are ready to be merged, what steps should you take to isolate these changes using feature toggles or environment variables?
  9. Your team is collaborating on a project with multiple branches and frequent merges. What strategies can you implement to maintain a clean and organized Git history?
  10. You've been asked to create a new branch for a hotfix that needs to be applied immediately. How can you ensure this hotfix is merged into the main codebase as quickly as possible without causing conflicts or breaking other features?

FAQ

  1. Why use branches in Git?

Branches allow developers to work on separate features or bug fixes without affecting the main codebase, making it easier to manage multiple tasks simultaneously and collaborate more effectively.

  1. How do I create a new branch in Git?

To create a new branch in Git, use the git branch command followed by the name of your new branch: git branch . To start working on this branch, switch to it using git checkout .

  1. How do I merge branches in Git?

To merge branches in Git, switch to the branch you want to merge into (usually the main codebase) and use the git merge command followed by the name of the branch you want to merge: git merge .

  1. What happens if there are conflicts during a merge in Git?

If there are conflicts during a merge in Git, you'll need to resolve them manually before completing the merge. Git will prompt you to edit the conflicting files and make the necessary changes.

  1. How do I delete a branch in Git?

To delete a branch in Git, use the git branch -d command followed by the name of the branch you want to delete: git branch -d . However, if the branch has not been merged yet, you cannot delete it with this command. Instead, you'll need to use git branch -D (with a capital D), which forces deletion of the local branch even if it has unmerged changes: git branch -D . Be cautious when using git branch -D, as this will permanently delete your work if it hasn't been merged.

  1. What is rebasing in Git?

Rebasing is a Git feature that allows you to integrate changes from one branch onto another, creating a cleaner and more linear commit history.

  1. How do I rebase a branch in Git?

To rebase a branch in Git, switch to the branch you want to rebase (usually the feature branch) and use the git rebase command followed by the name of the base branch: git rebase .

  1. What is a merge strategy in Git?

A merge strategy in Git is a set of rules that Git follows when merging two branches. By default, Git uses the "resolve" merge strategy, but you can customize this behavior using merge strategies like "recursive" or "ours."

  1. How do I set a custom merge strategy in Git?

To set a custom merge strategy in Git, add the following line to your Git configuration file (usually located at ~/.gitconfig):

[merge "recursive"]
ourstrategy = recursive
  1. What is a feature toggle or environment variable?

A feature toggle is a mechanism that allows you to enable or disable specific features in your application without affecting other parts of the codebase. Environment variables can be used to configure different settings for different environments, such as development, staging, and production.

branch (Git & Dev Tools) | Git & Dev Tools | XQA Learn