Branching and Merging
45 minA branch in Git is a lightweight movable pointer to one of your commits. The default branch name in Git is `main` or `master`. Branches allow you to work on different features, experiments, or bug fixes in parallel without affecting the main codebase. Creating a branch is extremely fast in Git because it's just creating a pointerâno files are copied. This makes branching a core part of Git workflows.
`git branch <name>` creates a new branch. This command creates a new branch pointer pointing to the current commit but doesn't switch to it. You can list all branches with `git branch` and see which branch you're on (marked with *). Branches are local by defaultâthey exist only in your repository until you push them. Understanding branch creation helps you organize your work effectively.
`git checkout <name>` or `git switch <name>` switches to the specified branch. The newer `git switch` command is more intuitive and was added specifically for switching branches. When you switch branches, Git updates your working directory to match that branch's state. Uncommitted changes can prevent switchingâyou may need to commit or stash them first. Understanding branch switching is essential for working with multiple features.
`git merge <branch>` merges the specified branch's history into the current branch. Git attempts to automatically merge changes, creating a merge commit if both branches have new commits. If changes conflict (same lines modified differently), Git marks conflicts for manual resolution. Understanding merge strategies (fast-forward vs three-way merge) helps you manage branch integration effectively.
Understanding merge conflicts and how to resolve them is crucial for collaborative development. Conflicts occur when the same lines are changed differently in different branches. Git marks conflicts in files with conflict markers (`<<<<<<<`, `=======`, `>>>>>>>`). You must manually edit these sections to resolve conflicts, then stage and commit the resolved files. Learning to resolve conflicts efficiently is essential for team collaboration.
Branch naming conventions help organize work: use prefixes like `feature/`, `bugfix/`, `hotfix/` to indicate branch purpose. Feature branches are for new features, bugfix branches for bug fixes, and hotfix branches for urgent production fixes. Deleting merged branches keeps your repository clean. Understanding branching strategies (Git Flow, GitHub Flow) helps teams coordinate effectively.
Key Concepts
- Branches are lightweight pointers to commits.
- git branch creates branches, git switch changes branches.
- git merge combines branch histories.
- Merge conflicts occur when the same lines change differently.
- Branching enables parallel development without conflicts.
Learning Objectives
Master
- Creating and switching between branches
- Merging branches and resolving conflicts
- Understanding merge strategies
- Using branches for feature development
Develop
- Understanding branching workflows and strategies
- Collaborating effectively with branches
- Managing parallel development streams
Tips
- Create feature branches for each new feature or bug fix.
- Use descriptive branch names: feature/user-authentication, bugfix/login-error.
- Delete merged branches to keep repository clean: git branch -d branch-name.
- Use git merge --no-ff to preserve branch history in merge commits.
Common Pitfalls
- Working directly on main/master, making it unstable.
- Not resolving merge conflicts properly, leaving conflict markers in code.
- Merging without testing, introducing bugs to main branch.
- Creating too many long-lived branches, making integration difficult.
Summary
- Branches enable parallel development on different features.
- git switch changes branches, git merge combines them.
- Merge conflicts require manual resolution.
- Proper branching strategy is essential for team collaboration.
Exercise
Simulate creating a new feature branch, making a commit, and merging it back to main.
git branch new-feature
git switch new-feature
# ...make changes and commit...
git switch main
git merge new-feature
Exercise Tips
- Use 'git switch -c new-feature' to create and switch in one command.
- View all branches: git branch -a (shows remote branches too).
- Delete branch after merging: git branch -d new-feature.
- Use 'git merge --no-ff' to create a merge commit even for fast-forward merges.