Back to Git & Dev Tools
2025-12-156 min read

"Understanding history: What is a branch?" (Git & Dev Tools)

Learn "Understanding history: What is a branch?" (Git & Dev Tools) step by step with clear examples and exercises.

Title: Mastering Git History and Branches: A full guide for Developers

Why This Matters

As a developer, understanding Git history and branches is essential for managing your codebase effectively. By utilizing Git's branching system, you can collaborate with other developers, track changes, and maintain a clean and organized development workflow. In this lesson, we will delve into the concepts of Git branches, their importance, and how they help you streamline your development process.

Prerequisites

To fully grasp the concepts covered in this lesson, you should have a basic understanding of:

  1. Git fundamentals: git init, git add, git commit
  2. Git commands for managing branches: git branch, git checkout, git merge, and git pull
  3. Understanding the Git workflow and its stages (setup, working, staging area, and committed)
  4. Familiarity with remote repositories (such as GitHub or Bitbucket) and how to clone, push, and pull from them

Core Concept

What is a Branch?

In Git, a branch represents an independent line of development. Each branch has its own history, allowing developers to work on different features or bug fixes without affecting the main codebase. This isolation enables parallel development and reduces the risk of conflicts when merging changes back into the main branch.

Creating and Switching Branches

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

$ git branch new-feature

This command creates a new branch named "new-feature" but does not switch to it. To start working on this branch, you need to check it out using the git checkout command:

$ git checkout new-feature

Now you can make changes, add files, and commit them as usual. When you're ready to merge your changes back into the main branch, use the git merge command while on the main branch:

$ git checkout master
$ git pull origin master # (optional) fetch latest changes from remote repository
$ git merge new-feature

Merging Branches

Merging branches in Git combines the changes from one branch into another. When merging, Git automatically handles conflicts if they occur during the merge process. To resolve any conflicts manually, you can use the git mergetool command:

$ git mergetool

Deleting Branches

Once a branch is merged into another branch or no longer needed, it can be safely deleted using the git branch -d command:

$ git branch -d new-feature

Managing Remote Branches

To collaborate with other developers, you often need to create a local branch based on a remote branch. Use the git checkout -b command followed by the name of the remote branch:

$ git checkout -b my-branch origin/my-branch

To push your changes to a remote repository, use the git push command:

$ git push origin my-branch

Protected Branches and Merge Requests (Optional)

In some workflows, branches may be protected from direct deletion or merging. In such cases, you may need to create a merge request on platforms like GitHub or GitLab to collaborate with other developers.

Worked Example

In this example, we will create two branches for developing a simple "Hello World" application. We'll make changes to the main branch and then create a new feature branch to add additional functionality.

  1. Initialize a new Git repository:
$ git init
  1. Create a basic "Hello World" application:
$ touch hello_world.c
$ cat > hello_world.c << EOF
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
EOF
  1. Commit the initial changes:
$ git add .
$ git commit -m "Initial commit"
  1. Create a new feature branch to add a greeting for a specific person:
$ git checkout -b greet-john origin/master # (assuming you have cloned a remote repository)
  1. Edit the hello_world.c file to include a personalized greeting:
$ cat > hello_world.c << EOF
#include <stdio.h>
int main() {
printf("Hello, John!\n");
printf("Hello, World!\n");
return 0;
}
EOF
  1. Commit the changes in the greet-john branch:
$ git add .
$ git commit -m "Add personalized greeting for John"
  1. Push the changes to the remote repository (assuming you have set up your origin):
$ git push origin greet-john
  1. Create a pull request on the remote platform (e.g., GitHub) and merge it into the main branch.
  1. Delete the greet-john branch:
$ git branch -d greet-john

Common Mistakes

  1. Forgetting to create a new branch before making changes: This can lead to unwanted changes in the main branch and potential conflicts when merging.
  2. Merging branches without properly resolving conflicts: Failing to address merge conflicts can result in incorrect code or broken functionality.
  3. Deleting a branch that has not been merged into another branch: If a branch contains valuable work, deleting it prematurely may cause data loss.
  4. Using git merge on the wrong branches: Merging the wrong branches can lead to unwanted changes and conflicts.
  5. Not using descriptive branch names: Clear and concise branch names help with understanding the purpose of each branch and facilitate collaboration among team members.
  6. Failing to pull updates from the remote repository before merging local branches: This can result in merge conflicts due to outdated code.
  7. Ignoring protected branches or merge request workflows when collaborating with other developers.

Practice Questions

  1. How do you create a new branch in Git?
  2. What command is used to switch between branches in Git?
  3. Describe the process of merging two branches in Git, including resolving conflicts if necessary.
  4. What happens if you delete a branch that has not been merged into another branch?
  5. Why is it important to use descriptive branch names in Git?
  6. How do you collaborate with other developers using remote repositories and merge requests?
  7. What is the purpose of the master branch in Git, and why should it be kept clean and stable?
  8. What is a protected branch, and how can you work around its restrictions when collaborating with others?

FAQ

Q: Can I have multiple active branches in my Git repository at the same time?

A: Yes, you can work on multiple branches simultaneously as long as you switch between them using git checkout.

Q: What is the difference between a local branch and a remote branch in Git?

A: A local branch exists only on your machine, while a remote branch lives on a remote repository (such as GitHub or Bitbucket). To collaborate with other developers, you often need to create a local branch based on a remote branch.

Q: How do I revert changes made in the current branch back to the last commit?

A: You can use the git reset command followed by the hash of the commit you want to revert to. For example, git reset --hard . Be careful when using this command as it will discard all changes made after the specified commit.

Q: What is a merge conflict in Git, and how can I resolve it?

A: A merge conflict occurs when two branches have conflicting changes to the same file or line of code. To resolve a merge conflict, you need to manually edit the affected files using a text editor or Git's built-in mergetool.

Q: What is the purpose of the master branch in Git?

A: The master branch is the default main branch in Git. It represents the production codebase and should be kept clean and stable. Other branches are typically used for development, testing, or feature work that is not yet ready to be merged into the master branch.

Q: How do I create a local branch based on a remote branch?

A: Use the git checkout -b command followed by the name of the remote branch: git checkout -b my-branch origin/my-branch.

Q: What is a protected branch, and how can I work around its restrictions when collaborating with others?

A: A protected branch is a branch that has been set to read-only or requires approval before it can be merged into another branch. To work around these restrictions, you may need to create a merge request on platforms like GitHub or GitLab and follow the platform's workflow for collaboration.

Q: How do I push my local changes to a remote repository after creating a new branch?

A: Use the git push command followed by the name of your local branch: git push origin my-branch.

"Understanding history: What is a branch?" (Git & Dev Tools) | Git & Dev Tools | XQA Learn