Branches and merges

Create and switch branches

Start a feature branch and move your working tree to it.

8 minute lesson

~~~

Before creating a branch, inspect your current state:

git status
git branch --show-current

Create a branch at the current commit and switch to it:

git switch -c add-search

Git creates the add-search reference, then points HEAD to it. The working tree still represents the same commit, so the files might not visibly change yet.

Verify the result:

git branch --show-current
git log -1 --oneline --decorate

Now create a commit. Only add-search moves to the new commit.

Later, return to the main branch:

git switch main

Switching updates the working tree and index to match the destination commit. Git refuses when that update would overwrite conflicting local changes.

Do not solve that refusal by discarding work. Commit the change if it is coherent, or store it deliberately before switching.

Exercise: create a branch, commit a new file, switch back to main, and confirm the file disappears from the working tree but remains in the branch history.

Lesson completed

Take this course offline

Get every free book, course edition, and software download.

Get the download library →