“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 designNothing 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-designYou now have a branch called new-design and you’re on it. Confirm with:
git branch* new-design
mainThe * marks the branch you’re on. Edits you commit now go only onto new-design.
To go back, drop the -c:
git switch mainThe 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-designThe changes from new-design are now part of main. A branch that has done its job can be deleted:
git branch -d new-designWhen 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
<<<<<<< HEADto=======— 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-designAfter 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 rewrite —
mainstays 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
mainis fine
“Branch when the change is big” is enough of a rule to start with.
Summary of this lesson
- A branch is a separate line of work.
git switch -c namecreates and moves;git switch mainreturns - To merge, move to the receiving branch first, then
git merge branch-name - A conflict is a question. Edit it into the form you want, then add and commit