Branching & Merging

In version control (like Git), Branching allows you to diverge from the main line of development and continue to work without affecting that main line. Merging is the process of putting those changes back together.

Branching & Merging

1. What is Branching?

Think of a branch as a separate workspace. When you want to add a new feature or fix a bug, you create a branch. This ensures the main (or master) branch always contains stable, working code.

  • Isolation: Developers can work on different features simultaneously.
  • Safety: Experimental code won't break the production environment.

2. What is Merging?

Once your work on a branch is complete and tested, you "merge" it back into the main branch. Git looks at the history of both branches and combines the changes.

  • Fast-forward Merge: Occurs when the main branch hasn't changed since you branched off.
  • Three-way Merge: Occurs when both branches have new, different commits.

3. Merge Conflicts

A conflict happens when two branches have different changes to the same line of the same file. Git won't know which version to keep and will ask you to decide manually.

4. Essential Git Commands

Action Command
Create a new branchgit branch feature-name
Switch to a branchgit checkout feature-name
Merge into current branchgit merge feature-name
Delete a branchgit branch -d feature-name

Knowledge Check

1. Why do we use branching?
A) To delete code | B) To work on features without breaking the main code | C) To make Git slower

2. What is a "Merge Conflict"?
A) When Git crashes | B) When two branches change the same line of a file | C) When you delete a branch

3. Which command is used to combine branches?
A) git commit | B) git merge | C) git branch