0
0
Gitdevops~15 mins

Creating branches with git branch - Mechanics & Internals

Choose your learning style9 modes available
Overview - Creating branches with git branch
What is it?
Creating branches with git branch means making a new path in your project where you can work on changes separately from the main code. A branch is like a copy of your project at a certain point, allowing you to try new ideas without affecting the original. Using the git branch command, you can create, list, and manage these branches easily. This helps keep your work organized and safe.
Why it matters
Without branches, all changes would happen in one place, making it risky to try new things or fix bugs without breaking the project. Branches let multiple people work on different features at the same time without interfering with each other. This makes teamwork smoother and safer, and helps keep the project stable and organized.
Where it fits
Before learning to create branches, you should understand basic git commands like git init, git add, and git commit. After mastering branches, you will learn how to switch between branches with git checkout or git switch, merge changes, and resolve conflicts. Branching is a key step toward mastering collaborative workflows in git.
Mental Model
Core Idea
A git branch is a separate line of work in your project that lets you develop features or fixes independently without disturbing the main code.
Think of it like...
Creating a branch in git is like making a photocopy of a document to write notes on, so the original stays clean while you experiment on the copy.
Main branch (master/main)
  │
  ├─ New branch (feature)
  │    └─ Work happens here without changing main
  └─ Other branches

Each branch points to a commit history that can grow independently.
Build-Up - 7 Steps
1
FoundationWhat is a Git Branch?
🤔
Concept: Introduce the idea of branches as separate lines of development in git.
A branch in git is a pointer to a snapshot of your project. It lets you work on different versions of your project at the same time. The default branch is usually called 'main' or 'master'. Creating a new branch means making a new pointer that can move independently as you add commits.
Result
You understand that branches let you isolate work and keep changes separate.
Understanding branches as pointers to commits helps you see how git tracks different lines of work.
2
FoundationUsing git branch to List Branches
🤔
Concept: Learn how to see existing branches in your repository.
Run the command: git branch This shows all branches in your project. The current branch is marked with an asterisk (*). Example: $ git branch * main feature1 bugfix This means you are on 'main' branch, and 'feature1' and 'bugfix' exist.
Result
You can list all branches and identify which one you are working on.
Knowing how to list branches helps you keep track of your work and avoid confusion.
3
IntermediateCreating a New Branch with git branch
🤔Before reading on: do you think 'git branch new-feature' switches you to the new branch immediately? Commit to your answer.
Concept: Learn how to create a new branch without switching to it.
To create a new branch, use: $ git branch new-feature This makes a new branch called 'new-feature' that points to the current commit. However, you stay on your current branch until you switch. Check branches again: $ git branch * main new-feature The new branch exists but you are still on 'main'.
Result
A new branch is created but you remain on the original branch.
Knowing that git branch only creates but does not switch prevents confusion about where your changes go.
4
IntermediateCreating and Switching Branches Together
🤔Before reading on: which command creates and switches to a new branch in one step? git branch -b or git checkout -b? Commit your guess.
Concept: Learn how to create a branch and switch to it immediately.
The git branch command alone does not switch branches. To create and switch in one step, use: $ git checkout -b new-feature or with newer git versions: $ git switch -c new-feature This creates 'new-feature' and moves you to it, so your next commits go there.
Result
You are now working on the new branch right after creating it.
Understanding the difference between creating and switching helps avoid mistakes in where you commit changes.
5
IntermediateDeleting Branches with git branch -d
🤔
Concept: Learn how to remove branches you no longer need.
To delete a branch, use: $ git branch -d branch-name This deletes the branch if it has been merged. To force delete (dangerous), use: $ git branch -D branch-name Deleting branches cleans up your project and avoids clutter.
Result
Branches you delete no longer appear in the branch list.
Knowing how to safely delete branches keeps your project organized and prevents confusion.
6
AdvancedBranch Creation Points and Detached HEAD
🤔Before reading on: if you create a branch while in detached HEAD state, where does it point? Commit your answer.
Concept: Understand where branches point when created and what detached HEAD means.
Normally, branches point to the current commit on a branch. But if you are in detached HEAD state (not on any branch), creating a branch points to the commit you currently have checked out. Detached HEAD means you are not on a branch, so commits you make may be lost unless you create a branch. Example: $ git checkout $ git branch new-branch Now 'new-branch' points to that commit, saving your place.
Result
You can save work from detached HEAD by creating a branch.
Understanding detached HEAD and branch creation points prevents losing work and clarifies git's commit tracking.
7
ExpertInternal Structure of Branches in Git
🤔Before reading on: do you think branches store the entire project or just a pointer? Commit your answer.
Concept: Learn how branches are stored internally as simple pointers to commits.
In git, a branch is just a file inside the .git/refs/heads directory containing the SHA-1 hash of the commit it points to. When you create a branch, git creates this file with the current commit hash. As you commit on that branch, git updates this pointer to the new commit. This lightweight design makes branches fast and cheap to create and switch.
Result
You understand branches are pointers, not copies of the project.
Knowing branches are pointers explains why git operations are fast and how git tracks history efficiently.
Under the Hood
Git branches are stored as simple text files containing commit hashes inside the .git directory. When you create a branch, git writes the current commit hash to a new file named after the branch. The HEAD file points to the current branch's file. When you commit, git updates the branch file to the new commit hash. This pointer system allows git to track multiple lines of development efficiently without duplicating data.
Why designed this way?
Git was designed for speed and efficiency. Using lightweight pointers instead of full copies of the project saves disk space and makes switching branches instant. This design contrasts with older version control systems that copied files or directories, which was slower and used more space. The pointer model also makes branching and merging powerful and flexible.
┌─────────────┐       ┌─────────────┐       ┌─────────────┐
│ Commit A   │◄──────│ Branch main │◄──────│ HEAD        │
└─────────────┘       └─────────────┘       └─────────────┘
       ▲
       │
┌─────────────┐
│ Commit B   │
└─────────────┘
       ▲
       │
┌─────────────┐       ┌─────────────┐
│ Commit C   │◄──────│ Branch feature│
└─────────────┘       └─────────────┘
Myth Busters - 4 Common Misconceptions
Quick: Does 'git branch new-feature' switch you to the new branch immediately? Commit yes or no.
Common Belief:Running 'git branch new-feature' automatically switches you to the new branch.
Tap to reveal reality
Reality:'git branch new-feature' only creates the branch but does not switch to it. You remain on your current branch until you explicitly switch.
Why it matters:Assuming you switched can cause you to commit changes to the wrong branch, leading to confusion and extra work to fix.
Quick: Are branches full copies of your project files? Commit yes or no.
Common Belief:Branches are full copies of the project files, so creating one duplicates all data.
Tap to reveal reality
Reality:Branches are just pointers to commits, not copies of files. Git stores data efficiently and shares unchanged files between branches.
Why it matters:Thinking branches duplicate data can make you avoid branching, missing out on git's powerful workflow benefits.
Quick: If you delete a branch with unmerged changes, are those changes lost? Commit yes or no.
Common Belief:You can safely delete any branch without losing work.
Tap to reveal reality
Reality:Deleting a branch with unmerged commits can cause you to lose those commits unless they are referenced elsewhere.
Why it matters:Deleting branches carelessly can cause permanent loss of work, which is hard to recover.
Quick: Does creating a branch in detached HEAD state point to the current branch? Commit yes or no.
Common Belief:Creating a branch in detached HEAD state points to the current branch automatically.
Tap to reveal reality
Reality:In detached HEAD state, creating a branch points to the current commit, not any branch, saving your place in history.
Why it matters:Misunderstanding this can cause confusion about where your branch points and risk losing commits.
Expert Zone
1
Branches are lightweight pointers, but their names and locations in .git/refs/heads affect how git commands behave and how remotes track them.
2
Creating many branches is cheap, but managing them well with naming conventions and cleanup is crucial in large projects.
3
Detached HEAD state is a powerful tool for exploring history, but forgetting to create a branch there can cause lost commits.
When NOT to use
Avoid creating branches for very small or trivial changes that can be done quickly on the main branch. For temporary experiments, consider using git stash or worktrees instead. Also, avoid creating branches without a clear purpose or cleanup plan, as this clutters the repository.
Production Patterns
In professional workflows, branches are used for features, bug fixes, releases, and hotfixes. Teams often use naming conventions like 'feature/xyz' or 'bugfix/abc'. Branches are created from stable points and merged back after review. Automation tools trigger tests on branches to ensure quality before merging.
Connections
Version Control Systems
git branching builds on the general idea of branching in version control but uses a unique pointer model.
Understanding git branches helps grasp how modern version control differs from older systems that copied files, improving efficiency.
Project Management
Branches correspond to tasks or features in project management, allowing parallel work streams.
Knowing how branches map to tasks helps coordinate team work and track progress clearly.
Biology - Evolutionary Trees
Branches in git resemble evolutionary branches where species diverge from common ancestors.
Seeing git branches like evolutionary trees helps understand how code changes diverge and merge over time.
Common Pitfalls
#1Creating a branch but assuming you are on it immediately.
Wrong approach:git branch new-feature # Then start committing changes
Correct approach:git branch new-feature git checkout new-feature # Or simply git checkout -b new-feature
Root cause:Misunderstanding that 'git branch' only creates but does not switch branches.
#2Deleting a branch without merging or saving its commits.
Wrong approach:git branch -d feature-xyz # Deletes even if unmerged
Correct approach:git branch -d feature-xyz # Only deletes if merged # Use git branch -D feature-xyz to force delete with caution
Root cause:Not checking branch merge status before deletion.
#3Creating branches with unclear or inconsistent names.
Wrong approach:git branch fix git branch new git branch test
Correct approach:git branch feature/login git branch bugfix/login-error git branch hotfix/security-patch
Root cause:Ignoring naming conventions that help organize and identify branches.
Key Takeaways
Git branches are simple pointers to commits that let you work on different versions of your project safely and independently.
The git branch command creates branches but does not switch to them; use git checkout -b or git switch -c to create and switch in one step.
Branches are lightweight and fast because they only store commit references, not full copies of files.
Managing branches with clear names and deleting unused ones keeps your project organized and reduces confusion.
Understanding detached HEAD state and branch pointers prevents losing work and clarifies git's powerful branching model.