CHAPTER 2 · 1 lesson · 13 exercises

Branches and Merging

Create branches to work in parallel, then merge them back. Resolve merge conflicts when they happen.

0 / 13 solved

You want to make a big change to working code, but keep the ability to go back if it fails.

You could copy the entire folder, but then you lose track of what actually changed, and joining them back is painful manual work.

Git provides a first-class feature for this: branches.

  • git branch … list existing branches, or create a new branch
  • git switch … switch to another branch (-c creates and switches at once)
  • git merge … merge another branch into your current branch

The key challenge of this chapter is a merge conflict. When two branches modify the same line in different ways, Git stops automatic merging.

Remember: a conflict is not a system failure. Git is simply saying, "Both sides changed this line, so please decide which code to keep." Only a developer can read the logic and decide.

Changes to separate regions often merge automatically. Nearby edits or deleting a file that the other branch edited can also cause conflicts. Git tracks changes line by line, not file by file. "Touching the same file always causes conflicts" is the most common beginner myth.

Watch the commit graph as you work. Once you see a branch diverge and merge back together, git merge becomes intuitive and visual.

Lessons in this chapter

Branching and Merging

  1. 02Branching and mergingCreate and switch branches to develop in isolation, merge changes back into main, and resolve merge conflicts.Go to the exercises

Next up is “Sharing with Remote Repositories”. Push local commits to remote repositories (origin) and pull changes to synchronize.