The Hook (The "Byte-Sized" Intro)
Creating a branch takes less time than reading this sentence. One command, zero risk, infinite upside. Every time you start a new feature, fix a bug, or want to experiment — you should be branching. If you're coding directly on main, you're skydiving without a parachute.
📖 What is Creating Branches?
Creating a branch means making a new pointer at your current commit. You can then switch to it and start working independently from the main line — your changes are isolated until you merge.
Conceptual Clarity
git branch <name>creates a branch but does not switch to itgit switch -c <name>creates a branch and switches to it in one command (most common)- The new branch starts at your current commit (HEAD)
- You can create a branch from any commit, not just HEAD
- Branch names should follow a clear naming convention
Real-Life Analogy
Creating a branch is like opening a new tab in your browser. Your other tabs (branches) are exactly where you left them. This new tab starts from where you are now, and you can close it anytime without affecting anything else.
Visual Architecture
Why It Matters
- Safety: Never work directly on
main— create a branch for every piece of work. - Organization: Clear branch names tell your team what you're working on without asking.
- Code review: Feature branches are the foundation of pull request workflows.
- Rollback: If the feature fails, delete the branch.
mainis untouched.
Code
# ─── Create a branch (stays on current branch) ───
git branch feature/login
# ─── Create AND switch in one command (recommended) ───
git switch -c feature/signup
# Output: Switched to a new branch 'feature/signup'
# ─── Legacy syntax (still works) ───
git checkout -b feature/signup
# ─── Create a branch from a specific commit ───
git branch hotfix/urgent a1b2c3d
# ─── Create a branch from a tag ───
git branch release/v2.1 v2.0.0
# ─── Create a branch tracking a remote branch ───
git switch -c feature/login origin/feature/login
# ─── Verify the branch was created ───
git branch
# Output:
# feature/login
# feature/signup
# * mainBranch Naming Conventions
| Prefix | Purpose | Example |
|---|---|---|
feature/ | New functionality | feature/user-auth |
bugfix/ | Bug fixes | bugfix/login-redirect |
hotfix/ | Urgent production fixes | hotfix/security-patch |
release/ | Release preparation | release/v2.1.0 |
chore/ | Maintenance tasks | chore/update-deps |
experiment/ | Exploratory work | experiment/new-ui |
Rules:
- Use lowercase and hyphens (no spaces)
- Include a ticket/issue ID when applicable:
feature/JIRA-123-user-auth - Keep names short but descriptive
Key Takeaways
- Use
git switch -c <name>to create and switch in one step. - Always branch from the right starting point — usually
mainor the latest release. - Follow naming conventions:
feature/,bugfix/,hotfix/, etc. - Never work directly on
mainin a team project.
Interview Prep
-
Q: What is the difference between
git branchandgit switch -c? A:git branch <name>creates a branch but stays on the current branch.git switch -c <name>creates the branch AND switches to it in one step. The latter is preferred because you almost always want to switch immediately. -
Q: Can you create a branch from a specific commit? A: Yes. Use
git branch <name> <commit-hash>to create a branch starting from any commit, not just HEAD. This is useful for hotfixes from a tagged release. -
Q: What is a good branch naming convention? A: Use prefixes like
feature/,bugfix/,hotfix/followed by a descriptive name. Include ticket IDs when applicable (e.g.,feature/JIRA-123-user-auth). Use lowercase and hyphens, keep names concise.