Branch Workflow Simulation
What you'll build
A repository where you create a feature branch, add a change on it, switch back to main, merge the branch, and delete it. The full branch lifecycle on a toy project.
Goals
- Create and switch branches with git checkout -b
- Make commits on a branch without affecting main
- Merge a branch back into main with git merge
- Delete a merged branch to keep the repo clean
Prerequisites
- git init, add, commit (Create Repository Workflow)
- Understanding that branches are independent lines of work
Steps
- 1
Set up main with a base commit
Create a repository and make one commit on the default main branch. This is the stable base that your feature branch will build on.
Codemkdir branch-sim cd branch-sim git init echo "# Branch Sim" > README.md git add . git commit -m "Initial commit"Expected result
A repository on main with one commit.
- 2
Create and switch to a feature branch
Run git checkout -b add-greeting to create a new branch and switch to it in one step. Commits made here will not affect main until you merge.
Codegit checkout -b add-greetingExpected result
Git prints "Switched to a new branch 'add-greeting'".
Hint
git branch shows all branches; the current one is marked with *.
- 3
Make a commit on the feature branch
Add a new file and commit it on the feature branch. Because you are on add-greeting, main is untouched — this is the whole point of branching.
Codeecho "print('hi from feature branch')" > greet.py git add greet.py git commit -m "Add greeting script on feature branch"Expected result
A commit exists on add-greeting; main still has only the initial commit.
- 4
Switch back to main and merge
Run git checkout main to return to the main branch, then git merge add-greeting to bring the feature commit in. After merging, main has the same files as the branch.
Codegit checkout main git merge add-greetingExpected result
main now contains greet.py; git log shows both commits.
- 5
Delete the merged branch
Run git branch -d add-greeting to delete the branch. The -d flag only deletes fully merged branches, so it is safe — no work is lost.
Codegit branch -d add-greeting git branch git log --onelineExpected result
Only main remains; the log shows both commits on main.
Try it yourself
# TODO: set up repo, create branch, commit, merge back, delete branch
mkdir branch-sim
cd branch-sim
git initmkdir branch-sim
cd branch-sim
git init
echo "# Branch Sim" > README.md
git add .
git commit -m "Initial commit"
git checkout -b add-greeting
echo "print('hi from feature branch')" > greet.py
git add greet.py
git commit -m "Add greeting script on feature branch"
git checkout main
git merge add-greeting
git branch -d add-greeting
git branch
git log --onelineWhat you learned
You simulated the full feature-branch lifecycle — create, commit, merge, delete — which is the workflow every team uses to develop features without destabilizing the main branch.
Related Resources
Related Lessons
- Git Branches
Branch creation, switching, merging, and deletion covered here.
- Git Basics
The underlying commit and log commands used throughout.
Related Practice
- Branch Workflow Predict
Predict which branch a commit lands on after a sequence of commands.
- Git Commands MCQ
Quiz on checkout, merge, and branch -d behavior.
Related Tools
- Code Diff
Compare branch changes before and after merging.
- .gitignore Generator
Keep generated files out of your branch commits.
Related Cheatsheets
- Git Cheatsheet
Reference for branch and merge command syntax.
Related Reference
- Git Commands Reference
Reference for checkout, merge, and branch -d syntax.
Related Errors
- Merge Conflict
Resolving conflicting changes.
- Detached Head
Working in a detached HEAD state.
- Branch Does Not Exist
Handling missing branch names.
Related Glossary
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Related Errors
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
- Merge Conflict
Resolving conflicting changes.
- Detached Head
Working in a detached HEAD state.
- Branch Does Not Exist
Handling missing branch names.