Branching Fundamentals¶
Overview¶
Create, switch, and list branches confidently, explain HEAD, and apply naming conventions used in Cloud/DevOps teams.
Branches isolate change. Protected main plus short-lived feature/* or fix/* branches is the default for GitHub Flow.
This is a core tutorial in Module 5 · Branching of the REBASH Academy Git for Cloud & DevOps Engineers series — written for Cloud, DevOps, Platform, and SRE engineers.
Prerequisites¶
Learning Objectives¶
By the end of this tutorial, you will be able to:
- Create/switch branches (
switch/checkout) - Explain HEAD
- Name branches consistently
- List and delete local branches safely
Architecture¶
This topic’s control points and relationships are shown below.
Theory¶
What¶
A branch is a movable pointer to a commit. Creating a branch does not copy files; it creates a new name for a commit ID. HEAD usually points at a branch name; when it points at a raw commit you are in detached HEAD. Day-to-day work uses git switch (and git switch -c) rather than older checkout habits.
Why¶
Branches isolate experiments, features, and hotfixes without disturbing main. Short-lived branches plus pull requests are the default DevOps collaboration model. Long-lived branches exist for release maintenance but raise merge cost — use them deliberately.
How it works¶
git switch -c feature/add-healthcheck creates a branch at the current commit and checks it out. New commits advance that branch pointer only. git branch -vv shows local branches and their upstream tracking. Remote-tracking branches such as origin/main are updated by fetch. Naming conventions (feature/…, fix/…, hotfix/…, chore/…, often with ticket IDs) make automation and CODEOWNERS routing easier.
Key concepts¶
| Idea | Detail |
|---|---|
| Branch = pointer | Cheap to create and delete |
| Upstream | Local branch linked to origin/… |
| Detached HEAD | HEAD points at a commit, not a branch |
| Default branch | Usually main; protect it on the host |
Delete merged local branches with git branch -d; prune remote-tracking names after fetch with prune enabled.
Common pitfalls¶
- Doing real work in detached HEAD and “losing” commits (recover via reflog)
- Reusing vague names like
fixorupdateacross tickets - Letting feature branches live for weeks against a moving
main - Force-deleting (
-D) shared branches without team agreement
Hands-on Lab¶
Create a workspace for this tutorial.
Focus: create topic branches, switch, merge, delete
Step 1 – Branch workflow¶
git init
git config user.name "REBASH Learner"
git config user.email "learner@rebash.local"
echo main > README.md
git add README.md && git commit -m "chore: main baseline"
git branch feature/login
git switch feature/login
echo "login" > login.txt
git add login.txt && git commit -m "feat: login stub"
git switch -
git branch -vv
git log --oneline --decorate --graph --all
Step 2 – Merge feature to main¶
Final step – Cleanup note¶
Validation¶
- Lab commands run under
~/rebash-git/module-05/ - You can explain each Theory section in your own words
- You used modern tooling where it applies to this topic
- You can describe one production failure mode for this topic
Code Walkthrough¶
Production practice for Branching Fundamentals always combines:
- Inspect before you change (status, plan, logs, dry-run)
- Prefer reversible, documented changes (Git, IaC, drop-ins, version pins)
- Capture evidence (command output, pipeline logs) for handovers
- Prefer current tools and APIs over legacy shortcuts
- Least privilege — escalate credentials only when required
Keep runbooks short enough to follow under pressure. Automate checks; keep humans for judgement.
Security Considerations¶
- Treat credentials and tokens for git as privileged — never commit them
- Prefer short-lived auth (OIDC, roles, SSO) over long-lived keys
- Validate blast radius before apply/deploy/delete operations
- Restrict who can approve production changes
- Collect audit logs; limit who can read sensitive traces
Common Mistakes¶
Doing real work in detached HEAD and “losing” commits (recover via reflog)
Validate assumptions against the Theory section and official docs before changing production.
Reusing vague names like fix or update across tickets
Lab shortcuts (open security groups, admin roles, skip approvals) must not ship unchanged.
Changing production without a rollback path
Always know how to revert (previous artefact, prior release, state rollback, DNS failback).
Best Practices¶
- Encode Branching Fundamentals changes as code and review them in pull requests
- Pin versions (images, modules, actions, provider plugins)
- Separate environments with clear promotion gates
- Alert on symptoms with runbooks attached
- Destroy lab resources; tag everything with owner and expiry where possible
Troubleshooting¶
| Symptom | Likely cause | Fix |
|---|---|---|
| Auth / permission denied | Wrong identity, policy, or scope | Check caller identity, roles, and least-privilege policies |
| Timeout / no route | Network, DNS, security group, or endpoint | Trace path, DNS, and allow-lists before retrying |
| Drift / unexpected plan | Manual change or wrong state/workspace | Reconcile desired vs actual; avoid click-ops on managed resources |
| Pipeline/job red | Flaky step, cache, or missing secret | Read failing step logs; bisect recent workflow/config changes |
| Cost spike | Idle load balancer, NAT, oversized compute | Inventory billable resources; stop/delete labs promptly |
Summary¶
Branching Fundamentals is essential for Cloud and DevOps engineers working with git. Practise the lab until the inspection and change path is muscle memory, then continue the track.
Interview Questions¶
- What is a branch in Git's data model?
- How do you list merged versus unmerged branches?
- Feature branch naming conventions you recommend?
- When is deleting a branch safe?
- How do long-lived branches create risk?
Sample answer — question 2
Use git branch --merged/--no-merged and compare SHAs with main before deleting.
Sample answer — question 4
Protect main/release branches and require PRs.