← Git from Scratch Lesson 9 of 17 · 10 XP

Branches: branch and switch

Theory and problem statement

Machine translation, not yet reviewed by a native speaker.

A branch is a movable pointer to a commit. New commits move the current branch forward, while the other branches stay where they are.

git branch                 # list of branches, * marks the current one
git branch feature         # create a branch (at the current commit)
git switch feature         # switch to it (the old way: git checkout feature)
git switch -c login        # create it and switch to it right away (git checkout -b login)

HEAD is the pointer "where I am now". Usually it points to a branch: HEAD → main.

Why branches: every feature or fix is done in a separate branch. If the idea doesn't work out, the branch is simply deleted, and main stays working. If it works out, it's merged into main. That's exactly how your team works with alex/themes, alex/terminal and the other branches in the IGD project.

When you switch branches, git changes the files in the working directory to the version from that branch. So commit your changes before switching; otherwise git will refuse to switch so that nothing gets lost.

Task. Create a branch feature and switch to it. Add a file login.py (any content) and commit it with the message add login. The main branch must still have one commit.

Sign up for free to solve this task right in your browser: code editor, running, test checks and hints.