Files
first-contributions/docs/additional-material/git_workflow_scenarios/why-using-branches.md
T
2025-03-26 07:37:33 +05:30

3.8 KiB
Raw Blame History

Why Use Branches When Contributing?

Git branches are an essential tool for collaboration in software development. They allow multiple developers to work on different features or bug fixes simultaneously without interfering with the main project code. By using branches, you can experiment freely, test new ideas, and merge only the best changes into the main project.

What Are Branches?

A branch in Git is essentially a separate line of development. It allows you to create an isolated version of the project where you can make changes without affecting the main codebase. When you're ready, you can merge your branch back into the main project.

How Branches Work

Every branch is just a pointer to a specific commit in the project history. When you create a new branch, Git duplicates the state of the current branch, allowing you to work independently. New commits are added to this branch's history without affecting the main branch.

  • To switch between branches, use git checkout.
  • To combine changes from one branch into another, use git merge.

Why Use Branches?

Branches make collaboration structured and efficient. Without them, all changes would be made directly to the main project, leading to confusion, errors, and conflicting code.

Example: The Car Paint Job Analogy

Imagine a car manufacturing team deciding on the default color for a new car model. Initially, the car is set to be olive green. However, a few team members want to see how it looks in red.

  • Instead of repainting the original car, they create a prototype with red paint.
  • If the red color is approved, it replaces the original color (i.e., the branch is merged into the main project).
  • If the red color is rejected, the prototype is discarded (i.e., the branch is deleted).

Similarly, in Git, branches allow developers to test new features without directly modifying the main codebase.

Feature Branching

Instead of having one branch per developer, it's better to create one branch per feature. This keeps things organized and prevents unnecessary conflicts.

Example: Alice & Bob's Feature Development

  • Alice is working on Feature A and makes several commits.
  • She then switches to Feature C and makes more commits.
  • Meanwhile, Bob finishes Feature B and wants to start working on Feature A.
  • Bob pulls in Alices branch, but now his branch contains Feature A, Feature B, and some incomplete parts of Feature C.
  • When he tries to merge his branch, he faces conflicts because Feature C is unfinished.

To avoid this:

  • Alice should have separate branches for Feature A and Feature C.
  • Bob should have separate branches for Feature B and Feature A.

This way, they can work without interfering with each other's progress.

Creating and Managing Branches

Create a New Branch

git branch my-new-branch

This creates a new branch named my-new-branch without switching to it.

Switch to a Branch

git checkout my-new-branch

This moves you to my-new-branch, allowing you to work on it.

Create and Switch to a Branch (Shortcut)

git checkout -b my-new-branch

This creates and switches to the new branch in a single step.

Delete a Branch (After Merging)

git branch -d my-new-branch

This removes my-new-branch if it has already been merged.

Force Delete a Branch (Without Merging)

git branch -D my-new-branch

Use this with caution! It deletes the branch even if it has unmerged changes.

Additional Resources