1. Understanding Git Branching (Theory)


Imagine you're writing a book. You have the main storyline (the master branch), but you also want to explore different plot ideas. Instead of modifying the main story directly, you create separate drafts (branches) for each idea. If an idea works well, you merge it back into the main story. If it doesn't, you can simply discard that draft without affecting your original work.
Git branching follows the same principle, allowing developers to work on different features or bug fixes without disturbing the stable version of the code.
What is Git Branching?
Think of a Git repository as a tree 🌳:
- The trunk represents the
main
(or master
) branch, which contains the stable version of the project.
- Branches are like smaller branches sprouting from the trunk, allowing developers to work on new features without disturbing the stable version.
Key Concepts of Git Branching
- Each task or feature is developed in a separate branch.
- Once coding is complete, branches are merged back into the main branch.
- This approach supports parallel development, where multiple developers work on different features simultaneously.
- Any number of branches can be created and deleted.
- Changes remain isolated to a particular branch until merged.
- The default branch in Git is usually
master
or main
.
- Files in the working directory are visible across all branches until committed. Once committed, the files belong to that specific branch.
- When a new branch is created, it inherits the data from the branch it was created from (often
master
).
🛠️ Why Use Branching?