World map

 

BM: Baca dalam Bahasa Melayu

World 3: Parallel Universes

Clean up branches

You will learn: which branches are safe to delete, and how.

The problem

git branch lists about, dark-mode, main with a star, and old-idea. Terminal output: $ git branch about dark-mode * main old-idea

Your list of branches keeps growing. Old notes everywhere! Which ones can you throw away safely?

Watch

The about label disappears. The commit Add about page stays, safe inside main.
6d3a9b1a8feb63270a76ea20da6ce16cb8a636058c

git branch -d about peels off the note. The commits stay safe, because main already has them.

Try it

git branch --merged lists notes whose work is already in your branch. Tap the ones you can delete.

Tap each merged note you can delete. Not the one you are on!

~/diary $ git branch --merged

0 of 2 found

Try it

Delete the merged notes about and dark-mode. Keep old-idea.

6d3a9b1a8feb63270a76ea20da6ce16cb8a636058c

Predict

old-idea was never merged. You run git branch -d old-idea. What happens?

Pick one answer.

Watch

git branch -D old-idea prints: Deleted branch old-idea. git branch now lists about, dark-mode and main. Terminal output: $ git branch -D old-idea Deleted branch old-idea (was 636058c). $ git branch about dark-mode * main

Sure you don’t need it? git branch -D (capital D) deletes it anyway. Careful: its commit loses its last note.

Check

What does git branch -d say?

Drag a card into a box. Or tap the card, then tap the box.

      You did it!

      You can now tidy your branches without losing work.

      Go deeper (optional)

      git branch --no-merged lists notes with work still waiting. Deleted a note by mistake? git reflog still knows the commit. Then git branch old-idea <hash> sticks the note back on.

      git branch --no-merged lists old-idea. Terminal output: $ git branch --no-merged old-idea