0%

Branches · practice

Return to Main

The opening-hours is safe on add-opening-hours. Now you want to look at the project as main records it, without deleting or moving the work you just made.

Check what has been saved

The previous lesson left one opening-hours commit on add-opening-hours, with no unfinished edits. Run git status before switching. If it reports a change, inspect it before continuing; this exercise expects the saved file and single commit from the previous lesson. Discard an extra edit with git restore only if you have decided you do not want it.

In your own projects, uncommitted edits do not belong to a yet. Git may carry them along when you switch if they do not conflict with the destination. If switching would overwrite them, Git refuses and names the affected files. Do not force the switch: commit wanted work on the branch where it belongs before trying again. The clean starting point in this exercise makes the file change below unambiguous.

Switch to an existing branch

Earlier, git switch -c add-opening-hours created a branch and switched to it. The branch main already exists, so this time you leave out -c and name the branch to switch to:

git switch main

Git moves HEAD to main and updates the working tree to match the commit main points to. The baseline commit has no hours.txt, so that file disappears from the working tree. It is not lost: the file remains in the commit reached by add-opening-hours.

The command ls lists the names in the current folder. Use it to inspect the working tree, then ask Git which branch is current:

ls
git branch
git status

ls shows README.md but not hours.txt. Plain git branch puts * beside main, and git status says the working tree is clean.

Switching branches did not undo a commit, copy a folder, or move main forward. It changed which existing branch HEAD follows and made the one working tree match that branch’s commit.

Add opening hours             <- add-opening-hours
        |
Clarify the guide title       <- main (HEAD), clearer-title
        |
Start the neighborhood guide

If you run git switch add-opening-hours, hours.txt comes back, because Git makes the working tree match that branch again. Try the round trip if you like. Just finish on main: run git switch main once more before you submit.

Why did hours.txt disappear after git switch main?

A file appearing and disappearing as you switch is alarming the first time and completely ordinary afterwards. Switching is not a save and it is not an undo. Git is rebuilding your working tree to match a different point in the history, and it can do that instantly because each of those points is a whole snapshot it already holds.

One piece of tidying is left. The still carries a branch name that has finished its job.