CH 22 / 75Part 4: Branches and Merging

Creating, Switching and Deleting Branches

What you will learn on this page

  • Switch and create branches with git switch and git switch -c
  • Understand that switching swaps the contents of the working tree
  • Understand how uncommitted changes are handled when you switch
  • Rename branches with git branch -m, and delete them knowing the difference between -d and -D
On this page11 items

In #21, we learned that a branch is a sticky note attached to a commit. On this page, you’ll practice the full set of operations: creating a branch, switching with git switch, renaming it and deleting it. You’ll also see how switching swaps the contents of the working tree.

Create and switch#

Let’s write a cookie recipe on a branch separate from main. First create the branch, then move to it.

text3 lines
cd ~/git-practice
git branch cookie
git switch cookie
text1 lines
Switched to branch 'cookie'

git switch <branch> is the command that reattaches HEAD to that branch. In the terms of Part 3, #18, the contents of .git/HEAD are rewritten to ref: refs/heads/cookie.

text1 lines
git branch
text2 lines
* cookie
  main

The * has moved to cookie. If you commit now, only cookie moves forward, as we saw in #21.

text3 lines
echo "Butter 100g" > recipes/cookie.txt
git add recipes/cookie.txt
git commit -m "Add cookie recipe"
text3 lines
[cookie 4b541f5] Add cookie recipe
 1 file changed, 1 insertion(+)
 create mode 100644 recipes/cookie.txt

The [cookie 4b541f5] on the first line means “recorded 4b541f5 on the cookie branch.”

Create and switch in one step#

Instead of typing git branch and git switch one after the other, adding -c (create) does it in a single command.

text1 lines
git switch -c <branch-name>

In real work, this is what you’ll use almost every time.

Switching swaps the contents of the working tree#

Go back to main and compare what’s in the recipes folder.

text3 lines
ls recipes
git switch main
ls recipes
text4 lines
cake.txt
cookie.txt
Switched to branch 'main'
cake.txt

When you go back to main, cookie.txt disappears from the working tree. It wasn’t deleted; the commit main points to doesn’t contain cookie.txt, so the working tree was swapped to match that state. Go back with git switch cookie and it reappears.

main and cookie point to different commits. With git switch main, the working tree is swapped to cake.txt, shopping.txt and todo.txt; with git switch cookie, it is swapped to a state that also includes cookie.txt
git switch swaps the contents of the working tree to the state of that branch's commit
text1 lines
git log --oneline --decorate --graph --all -n 3
text3 lines
* 4b541f5 (cookie) Add cookie recipe
* 4e16e23 (HEAD -> main) Add cleaning to the to-do list
* 370768c (tag: v1.0) Add cake recipe

cookie is one commit ahead of main. main doesn’t know about the cookies yet. How to bring cookie’s changes into main is covered next, in #23.

What happens to uncommitted changes?#

What gets swapped when you switch is “the committed contents.” Changes you haven’t committed yet don’t belong to any branch.

text2 lines
echo "Buy apples" >> shopping.txt
git switch cookie
text2 lines
Switched to branch 'cookie'
M	shopping.txt
text1 lines
git status -s
text1 lines
 M shopping.txt

The M shopping.txt line tells you “you moved while carrying a modified file.” There’s only one working tree and one staging area (Part 2, #08), so uncommitted changes come along with you when you switch.

However, if carrying them along would overwrite the contents of the branch you’re switching to, Git stops the switch.

text4 lines
git switch main
git restore shopping.txt
echo "Draft" > recipes/cookie.txt
git switch cookie
text4 lines
error: The following untracked working tree files would be overwritten by checkout:
	recipes/cookie.txt
Please move or remove them before you switch branches.
Aborting

The draft recipes/cookie.txt you created on main collides with the file of the same name recorded on the cookie branch, so the switch was aborted. This is a safety mechanism to protect your changes.

Delete the draft you created for practice.

text1 lines
rm recipes/cookie.txt

Go back to the previous branch#

git switch - takes you back to the branch you were on just before. It’s handy when going back and forth between two branches.

text2 lines
git switch -c tmp-test
git switch -
text2 lines
Switched to a new branch 'tmp-test'
Switched to branch 'main'

Renaming and deleting#

Renaming: git branch -m#

text2 lines
git branch -m cookie recipe-cookie
git branch -v
text2 lines
* main          4e16e23 Add cleaning to the to-do list
  recipe-cookie 4b541f5 Add cookie recipe

-m (move) renames a branch. It just rewrites the name on the sticky note, so it has no effect at all on commits. To rename the branch you’re on, write just one name: git branch -m <new-name>.

Deleting: -d and -D#

text1 lines
git branch -d tmp-test
text1 lines
Deleted branch tmp-test (was 4e16e23).

-d (delete) deletes a branch. tmp-test pointed to the same commit as main, so nothing is lost by deleting it. So what happens if you try to delete recipe-cookie, which hasn’t been brought into main yet?

text1 lines
git branch -d recipe-cookie
text3 lines
error: the branch 'recipe-cookie' is not fully merged
hint: If you are sure you want to delete it, run 'git branch -D recipe-cookie'
hint: Disable this message with "git config set advice.forceDeleteBranch false"

Git refuses because it’s “not fully merged.” Deleting this branch would leave no name through which to reach the cookie commit. If you still want to delete it, use the uppercase -D.

OptionBehavior
-dDeletes only branches that have been merged. Refuses if unmerged
-DDeletes even if not merged

We’ll bring recipe-cookie into main next, in #23, so leave it instead of deleting it.

FAQ#

Q. I’ve heard you can also switch branches with git checkout.
A. You can. git checkout <branch> does the same thing as git switch, and git checkout -b <name> is the same as git switch -c <name>. However, checkout also doubles as the operation for restoring files (today’s git restore), which is confusing, so Git 2.23 added switch and restore, which split those roles. This guide uses switch. If you see checkout in older articles, mentally substitute it.

Q. What characters can I use in branch names?
A. Lowercase letters, numbers and hyphens are the safe choice. Spaces and characters such as ~ ^ : ? aren’t allowed. Names separated by /, like feature/login, are allowed and commonly used (covered in #28).

Summary#

  • Switch with git switch <name>, and create and switch with git switch -c <name>. Go back to the previous branch with git switch -
  • Switching swaps the contents of the working tree to the state of that branch’s commit
  • Uncommitted changes come along when you switch. If they would overwrite something, Git stops you
  • Rename with git branch -m
  • -d deletes only merged branches; -D deletes forcibly

Next page#

23. How Merging Works (Fast-forward and 3-way) ── Bring the cookie recipe you wrote on recipe-cookie into main, and learn to tell the two kinds of merge apart.