When working on a Drupal site (or any web project), it’s a good idea to separate your code into different branches for each environment:
- main: production (live site)
- dev: development (new features)
- test: testing (before going to production)
This makes it easier to manage releases, test safely, and collaborate with others.
Let’s walk through how to do that using Git and GitHub.
✅ Step 1: Clone Your GitHub Repository
If you haven’t already, clone your Drupal project repository:
git clone https://github.com/your-github-username/your-repo.git
cd your-repo
✅ Step 2: Create and Push the dev and test Branches
You’ll create these two new branches (dev and test) from your current main branch, and push them to GitHub.
# Make sure you're on the main branch
git checkout main
# Create dev branch from main
git checkout -b dev
git push -u origin dev
# Switch back to main to create test branch
git checkout main
# Create test branch from main (or from dev, if preferred)
git checkout -b test
git push -u origin test
Now GitHub will show main, dev, and test as available branches in your repository.
✅ Step 3: Checkout Local Branches That Track the Remote Ones
If you (or someone else) want to clone the repo and start working with all three branches locally, here’s how:
git clone https://github.com/your-github-username/your-repo.git
cd your-repo
# Make sure you have the latest remote branches
git fetch origin
# Checkout each branch locally
git checkout -b main origin/main
git checkout -b dev origin/dev
git checkout -b test origin/test
Each of these local branches will now track their remote versions. You can work on them separately and push changes like this:
git push origin dev
git push origin test
💡 Why This Is a Good Practice for Drupal Projects
- 🧪 Safe Testing: You can test modules, config changes, or updates on dev or test before affecting your live site.
- 🔄 Better Collaboration: Developers can work on the dev branch without disturbing main.
- 🚀 Clean Deployments: Only merge into main when everything is ready for production.
- 🔁 CI/CD Friendly: You can hook up automated deployments for dev, test, and main environments.
🛡️ Bonus Tip: Protect Your Branches
On GitHub, you can go to Settings > Branches and:
- Protect main to prevent accidental force-pushes or deletions
- Require pull requests before merging
This adds an extra layer of safety.
✅ Summary
Here’s a quick cheat sheet:
# Clone your repo
git clone https://github.com/your-github-username/your-repo.git
cd anemia
# Create and push dev & test branches
git checkout main
git checkout -b dev
git push -u origin dev
git checkout main
git checkout -b test
git push -u origin test
Done! 🎉 Your Drupal project now has a clean, organized Git workflow.