Git & GitHub・One step further

Branches — experiment without breaking what works

Rebuilding something without breaking what already works — that's what a branch is for. Creating one, switching between them, merging, and fixing a conflict when one appears.

“I want to redo the design, but I want to keep what I have.” That’s what a branch is for. It splits your work into a separate line so you can try things without breaking the original.

A branch is a line of work

The line you’re on is usually called main. Branch off and you get this:

main       ●──●──●            ← stays in a working state
                └──●──●       ← where you try the new design

Nothing you do on the branch touches main. If it works out, merge it; if it doesn’t, throw the branch away.

Create one and move onto it

Add -c (create) to make a branch and switch to it at once.

git switch -c new-design

You now have a branch called new-design and you’re on it. Confirm with:

git branch
* new-design
  main

The * marks the branch you’re on. Edits you commit now go only onto new-design.

To go back, drop the -c:

git switch main

The moment you switch, the files in your editor change back too. It’s startling the first time, but it’s correct: switching branches tells Git to put the folder into that branch’s state.

Merging: git merge

When the branch work is ready, merge it into main. The key is to move to the receiving side first.

git switch main
git merge new-design

The changes from new-design are now part of main. A branch that has done its job can be deleted:

git branch -d new-design

When you hit a conflict

If the same line changed on both main and your branch, Git can’t choose for you. That’s a conflict.

Open the file and you’ll find markers:

<<<<<<< HEAD
<h1>Shima's page</h1>
=======
<h1>About Shima</h1>
>>>>>>> new-design
  • From <<<<<<< HEAD to ======= — the content on your current branch (main)
  • From ======= to >>>>>>> — the content on the branch being merged in

What to do is simple: edit it into the form you want and delete the three marker lines.

<h1>About Shima</h1>

Then record it as usual:

git add index.html
git commit -m "Settle the heading wording"

Merging on GitHub: pull requests

On a team, instead of merging locally you push the branch to GitHub and open a pull request — a request to merge.

git push -u origin new-design

After pushing, GitHub shows a “Compare & pull request” button. Opening the request lets others review your changes line by line. Once approved, a button on the page merges it into main.

Even working alone, pull requests are worth practicing: they give you a moment to review your own changes.

When to branch

  • For a large rewritemain stays intact if it goes wrong
  • When splitting work with others — you can touch the same files and merge afterwards
  • For a small solo fix — working directly on main is fine

“Branch when the change is big” is enough of a rule to start with.

Summary of this lesson

  1. A branch is a separate line of work. git switch -c name creates and moves; git switch main returns
  2. To merge, move to the receiving branch first, then git merge branch-name
  3. A conflict is a question. Edit it into the form you want, then add and commit

FAQ

What is a branch in Git?
A separate line of work. You can experiment freely on it while the original state stays untouched, then merge it back when it works out.
Should I use git switch or git checkout?
For changing branches, git switch. checkout does several unrelated jobs, which made it easy to misuse, so switch was added later for this one purpose.
What do I do when I get a conflict?
It happens when the same line changed on both sides. Open the file Git marked up, edit it into the form you want, then add and commit.