Git and GitHub · Part 2 · Branches and merging

Creating, switching, renaming and deleting branches

A place to practice

The previous lesson showed what a branch is. This one covers the commands used with them every day, in a fresh repository:

Terminal
cd ~/Desktop
mkdir branches-lab
cd branches-lab
git init
echo "<h1>My Site</h1>" > index.html
git add index.html
git commit -m "Add homepage"

Listing branches

Terminal
git branch
Output
* main

The * marks the current branch. (git status also names it on its first line.)

CommandShows
git branch -vEach branch with its latest commit
git branch -aLocal branches and branches on GitHub
git branch --mergedBranches already merged into this one, safe to delete
git branch --no-mergedBranches with work that isn't merged yet

Creating branches

1.Create, and stay where you are

Terminal
git branch navbar

navbar now exists, but the current branch is still main.

2.Create and switch in one step

This is the version used almost every time:

Terminal
git switch -c footer
Output
Switched to a new branch 'footer'

-c means create. Listing the branches now shows the * on footer, in alphabetical order:

Output of git branch
* footer
  main
  navbar

3.Start from another branch or commit

A new branch starts from the current commit unless told otherwise:

Terminal
git switch -c hotfix main        # starts from main, wherever you are
git switch -c old-version a1b2c3d   # starts from an old commit

Switching branches

Terminal
git switch main
git switch navbar
git switch -        # back to the previous branch

git switch - flips between the last two branches, like a TV remote's back button.

Switching with unsaved changes

Edits that aren't committed don't belong to any branch. They sit in the working directory, and switching does one of two things with them.

1.Git carries them along

Terminal
git switch navbar
echo "<nav>Home</nav>" > nav.html
git switch main
git status
Output
Untracked files:
        nav.html

nav.html came along to main, because it isn't on any branch until it's committed. Commit it where it belongs:

Terminal
git switch navbar
git add nav.html
git commit -m "Add navigation bar"

2.Git refuses to switch

If an unsaved edit is in a file that's different on the other branch, switching would overwrite it, so Git stops:

Terminal
git switch main
echo "<p>Edited on main</p>" >> index.html
git commit -am "Edit homepage on main"

git switch navbar
echo "<p>Unsaved edit</p>" >> index.html
git switch main
Output
error: Your local changes to the following files would be overwritten by checkout:
        index.html
Please commit your changes or stash them before you switch branches.
Aborting
ChoiceCommandWhen
Commit itgit commit -am "…"The work is ready
Stash itgit stashIt's half done and should wait in a drawer
Throw it awaygit restore index.htmlIt isn't needed

Here, throw it away and switch:

Terminal
git restore index.html
git switch main

Renaming branches

Terminal
git switch footer
git branch -m site-footer       # renames the current branch
git branch -m navbar main-nav   # renames another branch

-m stands for move. A common use is renaming an old master branch: git branch -m master main.

Deleting branches

A branch can't be deleted while it's the current one, so switch away first.

1.-d: the safe delete

Terminal
git switch main
git branch -d site-footer
Output
Deleted branch site-footer (was 09e9ce0).

It worked because every commit on site-footer is already in main. main-nav has a commit that isn't, so -d refuses:

Terminal
git branch -d main-nav
Output
error: the branch 'main-nav' is not fully merged
hint: If you are sure you want to delete it, run 'git branch -D main-nav'

2.-D: delete anyway

Terminal
git branch -D main-nav
Output
Deleted branch main-nav (was 70223fb).

Use it for experiments that really aren't needed.

3.Bringing a deleted branch back

Deleting a branch only removes the name; the commits are still there. Git even printed the hash, so a new branch can point at it again:

Terminal
git switch -c main-nav 70223fb

Without the hash, git reflog finds it.

Naming branches

Teams use a prefix that says what the branch is for:

PrefixForExample
feature/A new featurefeature/dark-mode
fix/A bug fixfix/login-redirect
hotfix/An urgent fix for productionhotfix/payment-crash
chore/Maintenance and dependencieschore/upgrade-next-16
docs/Documentationdocs/api-setup
refactor/Cleaner code, same behaviorrefactor/auth-service
  • Lowercase, with words joined by hyphens, and no spaces.
  • Short but clear: fix/navbar-mobile, not fix/the-navbar-was-broken-on-small-iphones.
  • Many teams add a ticket number: feature/WL-142-property-filters.

git checkout and git switch

Older tutorials use git checkout for branches. Both work, but switch is clearer and harder to misuse:

OldNew
git checkout maingit switch main
git checkout -b featuregit switch -c feature
git checkout -git switch -
git checkout a1b2c3dgit switch --detach a1b2c3d

Quick reference

To…Run
List branchesgit branch
List them with their latest commitgit branch -v
Create and switchgit switch -c name
Create from a specific branchgit switch -c name main
Switchgit switch name
Go back to the previous branchgit switch -
Rename the current branchgit branch -m new-name
Delete safelygit branch -d name
Delete anywaygit branch -D name
See merged branchesgit branch --merged
Bring back a deleted branchgit switch -c name hash