Unays Learn ยท Tools

Git branches and merging, step by step

A branch lets you try an idea without risking the working version of your project. When the idea works, you merge it back. This is how both solo developers and large teams work every day.

Why use branches?

A branch is a separate line of commits. The main line, usually called main, holds the version that works. You create a branch for each new feature or fix, commit freely on it, and only bring it into main when it is ready. If the idea fails, you simply delete the branch.

This tutorial assumes you know git add and git commit from Git basics.

Creating and switching branches

git branch                 # list branches; * marks the current one
git switch -c dark-mode    # create a branch called dark-mode and switch to it
# ...edit files...
git add style.css
git commit -m "Add dark mode colours"
git switch main            # go back to main: your dark mode changes disappear from the folder
git switch dark-mode       # ...and come back when you return

Switching branches changes the files in your folder to match that branch. Commit (or stash) your work before switching, so nothing gets mixed up. Older guides use git checkout -b, which does the same as git switch -c.

Merging a branch

When the feature is finished, switch to the branch you want to bring it into, then merge:

git switch main
git merge dark-mode
git log --oneline --graph --all   # see how the branches joined

If main has not changed since you branched, Git just moves main forward ("fast-forward"). If both have new commits, Git combines them and creates a merge commit.

Merge conflicts

A conflict happens when both branches changed the same lines. Git stops and marks the file:

<<<<<<< HEAD
<h1>Welcome to our class site</h1>
=======
<h1>Welcome to Class 8A</h1>
>>>>>>> new-title
  • The part between <<<<<<< HEAD and ======= is your current branch's version.
  • The part after ======= is the incoming branch's version.

Edit the file so it contains the text you actually want, delete all three marker lines, then finish the merge:

git add index.html
git commit                 # completes the merge
# or, to give up and go back to before the merge:
git merge --abort

Conflicts are normal, not a sign that something broke. Small branches that you merge often have fewer and smaller conflicts.

Cleaning up

git branch -d dark-mode    # delete a branch that has been merged
git branch -D experiment   # force-delete an unmerged branch (its commits are lost)

On GitHub and similar sites, teams usually push a branch and open a pull request, so others can review the changes before they are merged into main.

Try it yourself

In a practice folder: commit a file on main, create a branch and change the first line, switch back to main and change the same line differently, then merge and resolve the conflict. Using the command line basics makes this quicker.

This tutorial was written with AI assistance and reviewed by Unays. The runnable code examples were tested before publishing. Spotted a mistake? Tell us at [email protected].