Back to Git & Dev Tools
2026-04-246 min read

Create a branch and change something using the branching workflow (Git & Dev Tools)

Learn Create a branch and change something using the branching workflow (Git & Dev Tools) step by step with clear examples and exercises.

Title: Mastering Git Branching Workflow: Create, Change, and Merge Branches Like a Pro

Why This Matters

In software development, collaboration and experimentation are crucial. Git branching workflow allows developers to work on multiple features or bug fixes without affecting the main codebase. Understanding how to create, change, and merge branches can help you manage complex projects more efficiently, avoid conflicts, and ensure a smooth deployment process. This skill is essential for passing interviews, troubleshooting real-world issues, and maintaining a clean development environment.

The Importance of Branching in Collaborative Development

  • Allows multiple developers to work on different features or bug fixes simultaneously without affecting each other's progress.
  • Provides a safe space for experimentation and testing new ideas before merging them into the main codebase.
  • Helps maintain a stable main branch that is always ready for deployment.

Prerequisites

Before diving into branching, make sure you have a basic understanding of Git commands:

  1. Initializing a Git repository: git init
  2. Adding files to the staging area: git add
  3. Committing changes: git commit -m "commit message"
  4. Viewing commit history: git log
  5. Fetching and merging changes from a remote repository: git merge origin/
  6. Understanding basic Git workflows (e.g., feature branch workflow, GitFlow): These workflows provide guidelines for creating, managing, and merging branches in a team environment.

Core Concept

Git branching allows you to create separate lines of development, isolating changes until they are ready to be merged into the main codebase (usually called master or main). Here's a step-by-step guide on creating, changing, and merging branches:

  1. Creating a new branch: Use the command git branch to create a new branch. To switch to this branch, use git checkout .
$ git checkout -b feature/new-feature
  1. Making changes: Now you can make changes in your new branch without affecting the main codebase.
  1. Staging and committing changes: Use git add, git commit, and git push to stage, commit, and push your changes to the remote repository.
$ git add <file>
$ git commit -m "Add new feature"
$ git push origin feature/new-feature
  1. Switching back to the main branch: To switch back to the main branch, use git checkout master.
  1. Merging changes into the main branch: Once you're satisfied with your changes in the new branch, you can merge them into the main branch using git merge .
$ git checkout master
$ git merge feature/new-feature
  1. Deleting unnecessary branches: After merging, you can delete the old branch with git branch -d .

Merge Strategies and Conflict Resolution

  • Git offers various merge strategies to handle conflicts during the merging process. Commonly used strategies include recursive, octopus, and ours/theirs.
  • If conflicts occur, Git will pause and ask you to resolve them manually. Use a text editor or your preferred conflict resolution tool to address the differences between the branches.

Worked Example

Let's say we have a simple project in a Git repository, and we want to add a new feature: a command-line calculator.

  1. Creating a new branch: First, create a new branch for the calculator feature.
$ git checkout -b feature/calculator
  1. Adding files: Create a new file calculator.c and write some code for our simple calculator.
  1. Staging, committing, and pushing changes: Stage the new file, commit the changes, and push them to the remote repository.
$ git add calculator.c
$ git commit -m "Add calculator feature"
$ git push origin feature/calculator
  1. Switching back to the main branch: Now switch back to the main branch.
$ git checkout master
  1. Merging changes into the main branch: Merge the new calculator feature into the main branch.
$ git merge feature/calculator
  1. Testing the new feature: Run your program to test the calculator functionality.

Common Mistakes

  1. Forgetting to stage changes before committing: Always stage changes using git add before committing them with git commit.
  2. Merging conflicts: If you try to merge branches with unresolved conflicts, Git will pause and ask you to resolve the conflicts manually. Make sure your branches are clean and ready for merging.
  3. Deleting the main branch: Never delete the main branch directly. Instead, merge it into another branch or rebase it onto another branch before deletion.
  4. Not using descriptive branch names: Use meaningful branch names that clearly indicate the purpose of each branch. This makes it easier to understand the history and context of your changes.
  5. Not cleaning up unnecessary branches: Regularly delete old, unused branches to keep your repository organized and prevent clutter.
  6. ### Merge Strategies
  • Understanding different merge strategies can help you handle conflicts more effectively and maintain a cleaner commit history.
  1. ### Rebasing vs Merging
  • git rebase offers an alternative method for integrating changes from one branch into another, creating a cleaner commit history by "re-parenting" commits.
  1. ### Cherry-Picking Commits
  • Cherry-picking allows you to selectively apply individual commits from one branch to another, without merging the entire branch.

Practice Questions

  1. Create a new branch called feature/login and make some changes to implement a login system for your project. Merge the changes into the main branch when you're done.
  2. Suppose you have two branches, feature/new-feature and bugfix/broken-function, both containing uncommitted changes. How would you merge these branches without losing any changes?
  3. You are working on a new feature in the feature/new-feature branch. Suddenly, your team decides to switch to a different feature. How can you save your current work and start working on the new feature without losing any progress?
  4. You have made several commits in the main branch that need to be included in your current branch (feature/calculator). How would you incorporate these changes into your local branch?
  5. Discuss different Git merge strategies, their purposes, and when they should be used.
  6. Compare and contrast git rebase and git merge. What are some scenarios where each method might be more appropriate?
  7. Explain how to cherry-pick a commit in Git and why it can be useful in certain situations.

FAQ

  1. Why should I use Git branches?
  • To isolate changes and avoid conflicts when working on multiple features or bug fixes simultaneously.
  • To allow team members to work independently without affecting each other's progress.
  • To provide a safe space for experimentation and testing new ideas before merging them into the main codebase.
  1. What is the difference between git merge and git rebase?
  • git merge combines two lines of development by creating a new commit that merges both branches, while preserving the original commit history.
  • git rebase moves or "re-parents" commits from one branch onto another, resulting in a cleaner and more linear commit history.
  1. How can I see all branches in my repository?
  • Use the command git branch. This will display a list of all local branches.
  1. What is the purpose of the main branch (master or main) in Git?
  • The main branch represents the production-ready codebase that should always be stable and ready for deployment. All other branches are created from this branch, and changes are merged back into it when they're ready.
  1. Can I delete a branch if it has unmerged changes?
  • No, you cannot delete a branch with uncommitted or unmerged changes. First, resolve any conflicts or stash your changes before deleting the branch.
  1. ### Collaborative Development
  • Discuss best practices for collaborating on Git repositories, including branch naming conventions, pull requests, and code reviews.
  1. ### Remote Repositories and Fetching/Pulling Changes
  • Explain how to work with remote repositories, including fetching changes from a remote repository, creating a local branch based on a remote branch, and pulling changes into your local repository.
Create a branch and change something using the branching workflow (Git & Dev Tools) | Git & Dev Tools | XQA Learn