Branching & Merging in Git #
What is a Branch? #
A branch in Git is an independent line of development. It allows you to work on new features, bug fixes, or experiments without affecting the main project (usually the main or master branch).
Every Git repository starts with a default branch (often named main). You can create multiple branches to isolate your work, and later merge them back when ready.
git branch — List or Create Branches #
Used to view, create, or delete branches in your repository.
Examples:
- List all branches:
git branch - Create a new branch:
git branch feature-login - Delete a branch:
git branch -d feature-login
The current active branch is highlighted with an asterisk (*) in the list.
git checkout — Switch to a Branch #
Moves you from one branch to another.
Example:
git checkout feature-login
Now you’re working on the feature-login branch.
Alternatively, you can create and switch in one command:
git checkout -b feature-login
Note: In newer versions of Git,
git switchis recommended instead ofgit checkoutfor switching branches.
Merging in Git #
What is Merging? #
Merging combines changes from one branch into another. Usually, when a feature is complete, it’s merged back into the main branch.
git merge — Combine Branches #
To merge a branch into your current branch:
git merge feature-login
This command integrates the changes from feature-login into the branch you’re currently on (e.g., main).
Understanding Merge Conflicts #
A merge conflict occurs when two branches change the same part of a file differently. Git can’t automatically decide which change to keep, so it asks you to resolve it manually.
When a conflict occurs:
- Open the file in your editor — you’ll see markers like:
<<<<<<< HEAD Current branch changes ======= Merged branch changes >>>>>>> feature-login - Edit the file to keep or combine the correct code.
- Stage the file and commit the resolved version:
git add filename.txt git commit -m "Resolved merge conflict"
git diff — Compare Changes #
Shows the difference between two commits, branches, or the working directory and staging area.
Examples:
- Compare working directory with staging area:
git diff - Compare two branches:
git diff main feature-login
This helps review changes before merging.
Branching Strategies #
1. Git Flow #
- Designed for large projects.
- Uses multiple branches like
main,develop,feature,release, andhotfix. - Ideal for organized release management.
2. GitHub Flow #
- Simpler and used for continuous deployment.
- Create a feature branch → work on it → open a pull request → review and merge into
main.
3. Trunk-Based Development #
- Developers work in short-lived branches or directly on the
mainbranch. - Encourages frequent integration and automated testing.
Summary #
| Command | Description |
|---|---|
git branch | List or create branches |
git checkout <branch> | Switch branches |
git checkout -b <branch> | Create and switch branch |
git merge <branch> | Merge a branch into current branch |
git diff | Compare changes |
git branch -d <branch> | Delete a branch |