Creating and switching branches
Create, switch, rename and delete branches with git switch and git branch, and see what Git does with uncommitted changes when you switch.
Creating and switching branches
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- git switch -c <name> creates a branch and moves onto it in one step, and git switch <name> moves to an existing one.
- git switch - jumps back to the branch you were on before, which is handy for flipping between two lines of work.
- Uncommitted changes come with you when you switch, unless they would overwrite a file that differs on the other branch, in which case Git refuses.
- git branch -d deletes a branch only if its commits are merged elsewhere, and -D deletes it regardless.
- git switch --detach <commit> puts you on a commit with no branch, so commits made there are easy to lose.
Why branch at all
tinynotes needs a search command. You don’t know how long it will take, and in the meantime you might need to fix something small on main. If both kinds of work pile up in one working directory, a half-finished search feature gets in the way of the quick fix.
A branch gives the feature its own line of commits. main stays in a state you trust, and you can set the feature down and come back. This lesson is the commands. The previous one explained why they’re cheap.
git switch
Two commands exist, and they are easy to mix up. git branch manages branches (list, create, rename, delete). git switch moves you onto one. Git 2.23 added switch so that moving between branches no longer shares a command with checkout’s other jobs.
$ git switch -c search
Switched to a new branch 'search'
-c means create, and the branch starts from your current commit. I added a search command to notes.py and committed:
$ git commit -m "Add a search command"
[search 748069e] Add a search command
1 file changed, 5 insertions(+)
$ git log --oneline --decorate -3
748069e (HEAD -> search) Add a search command
ecbaa7f (main) Move design notes into docs
f416046 Add design notes
Go back to main, and see both branches:
$ git switch main
Switched to branch 'main'
$ git branch -v
* main ecbaa7f Move design notes into docs
search 748069e Add a search command
* marks the branch you’re on. And git switch - returns to the previous branch, each time swapping:
$ git switch -
Switched to branch 'search'
$ git switch -
Switched to branch 'main'
Check where you are at any time with git branch --show-current, which prints just the name, or with git status, whose first line says it.
What happens to uncommitted work
Switching rewrites the working directory to match the target branch. So what about edits you haven’t committed? Git’s rule: it carries them across if it can, and refuses if it can’t.
I added a line to README.md, which is identical on both branches, and switched. It worked, and Git printed M README.md as a reminder that the edit came along:
$ git switch search
Switched to branch 'search'
M README.md
Then I edited notes.py on main, which differs on search:
$ git switch search
error: Your local changes to the following files would be overwritten by checkout:
notes.py
Please commit your changes or stash them before you switch branches.
Aborting
Git protects your edits. Your options are to commit them, discard them, or stash them. The stashing lesson, later in this group, covers the third. The edits you carry across belong to the working directory, not to a branch. Commit them and they land on whichever branch you’re on at that moment, which is a classic way to commit to the wrong branch. Look at git branch --show-current before you commit.
Starting from somewhere else
A new branch starts at the current commit, unless you name another:
$ git switch -c hotfix HEAD~2
Switched to a new branch 'hotfix'
$ git log --oneline -1
5c9e9c4 Document the clear command
Handy for branching from an old release. Any commit ID or name works.
Deleting and renaming
$ git branch -d hotfix
Deleted branch hotfix (was 5c9e9c4).
-d is the safe delete. It works only when the branch’s commits are reachable from the branch you’re on, in other words merged. For an unmerged one:
$ git branch -d idea
error: the branch 'idea' is not fully merged
hint: If you are sure you want to delete it, run 'git branch -D idea'
-D forces it, and prints the old ID in case you want to recreate it. Rename with git branch -m tmp scratch. A name with a space is rejected: “fatal: ‘bad name’ is not a valid branch name”. Creating a branch whose name exists fails with “fatal: a branch named ‘search’ already exists”, and switching to one that doesn’t exist fails with “fatal: invalid reference: nope”.
Detached HEAD
You can switch to a commit instead of a branch:
$ git switch --detach HEAD~2
HEAD is now at 5c9e9c4 Document the clear command
$ git status
HEAD detached at 5c9e9c4
HEAD now points straight at a commit, not at a branch, so a new commit moves nothing that has a name. That’s fine for looking around, and a trap for new work, because nothing will lead to it once you switch away. To keep what you did, create a branch there with git switch -c <name>. Switching away prints “Previous HEAD position was …” with the commit ID, which is your handle for finding it again.
The old spelling
git checkout -b old-style does the same as git switch -c old-style, and git checkout main as git switch main. I ran both. You’ll meet checkout in older tutorials and scripts. switch only moves between branches, which is why it’s easier to teach.
What the video covers
The Modern SDE’s lecture 21 is about six minutes. From its title and description (I had no transcript) it covers git switch versus git checkout, moving HEAD to a branch, and what a divergent history looks like. The channel is small and new, and I judged the language to be English from its metadata only. GitKraken’s older short video on creating, switching and deleting branches is linked as a second view. I didn’t confirm which commands it uses.
Where this fits
search has a commit that main doesn’t. Time to bring them together: merging.
Check yourself
What is the difference between git branch feature and git switch -c feature?
git branch creates the branch and leaves you where you are. git switch -c creates it and moves you onto it.
You have uncommitted edits to notes.py and switch to a branch where notes.py differs. What happens?
Git refuses and lists the files that would be overwritten. Commit or stash the edits first.
Why does git branch -d refuse to delete a branch you have not merged?
Deleting it would leave its commits reachable from nothing, so you could lose work. -D overrides the check when you really mean it.
How do you start a branch from an older commit instead of the current one?
Give a start point: git switch -c hotfix HEAD~2 creates the branch at the commit two before the current one.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.