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:
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
git branch* mainThe * marks the current branch. (git status also names it on its first line.)
| Command | Shows |
|---|---|
git branch -v | Each branch with its latest commit |
git branch -a | Local branches and branches on GitHub |
git branch --merged | Branches already merged into this one, safe to delete |
git branch --no-merged | Branches with work that isn't merged yet |
Creating branches
1.Create, and stay where you are
git branch navbarnavbar now exists, but the current branch is still main.
2.Create and switch in one step
This is the version used almost every time:
git switch -c footerSwitched to a new branch 'footer'-c means create. Listing the branches now shows the * on footer, in alphabetical order:
* footer
main
navbar3.Start from another branch or commit
A new branch starts from the current commit unless told otherwise:
git switch -c hotfix main # starts from main, wherever you are
git switch -c old-version a1b2c3d # starts from an old commitSwitching branches
git switch main
git switch navbar
git switch - # back to the previous branchgit 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
git switch navbar
echo "<nav>Home</nav>" > nav.html
git switch main
git statusUntracked files:
nav.htmlnav.html came along to main, because it isn't on any branch until it's committed. Commit it where it belongs:
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:
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 mainerror: 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| Choice | Command | When |
|---|---|---|
| Commit it | git commit -am "…" | The work is ready |
| Stash it | git stash | It's half done and should wait in a drawer |
| Throw it away | git restore index.html | It isn't needed |
Here, throw it away and switch:
git restore index.html
git switch mainRenaming branches
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
git switch main
git branch -d site-footerDeleted 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:
git branch -d main-naverror: 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
git branch -D main-navDeleted 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:
git switch -c main-nav 70223fbWithout the hash, git reflog finds it.
Naming branches
Teams use a prefix that says what the branch is for:
| Prefix | For | Example |
|---|---|---|
feature/ | A new feature | feature/dark-mode |
fix/ | A bug fix | fix/login-redirect |
hotfix/ | An urgent fix for production | hotfix/payment-crash |
chore/ | Maintenance and dependencies | chore/upgrade-next-16 |
docs/ | Documentation | docs/api-setup |
refactor/ | Cleaner code, same behavior | refactor/auth-service |
- Lowercase, with words joined by hyphens, and no spaces.
- Short but clear:
fix/navbar-mobile, notfix/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:
| Old | New |
|---|---|
git checkout main | git switch main |
git checkout -b feature | git switch -c feature |
git checkout - | git switch - |
git checkout a1b2c3d | git switch --detach a1b2c3d |
Quick reference
| To… | Run |
|---|---|
| List branches | git branch |
| List them with their latest commit | git branch -v |
| Create and switch | git switch -c name |
| Create from a specific branch | git switch -c name main |
| Switch | git switch name |
| Go back to the previous branch | git switch - |
| Rename the current branch | git branch -m new-name |
| Delete safely | git branch -d name |
| Delete anyway | git branch -D name |
| See merged branches | git branch --merged |
| Bring back a deleted branch | git switch -c name hash |