Skip to content

Instantly share code, notes, and snippets.

@pizofreude
Created April 16, 2025 18:24
Show Gist options
  • Save pizofreude/2152ecf5d7948ce7a046adc30676b123 to your computer and use it in GitHub Desktop.
Save pizofreude/2152ecf5d7948ce7a046adc30676b123 to your computer and use it in GitHub Desktop.
Git and Terraform Workspace Workflow. All things git & terraform.

Git and Terraform Workspace Workflow

When using Terraform with Git, it's common to associate a Terraform workspace with a specific Git branch. This allows you to manage different environments and configurations for each branch.

Here's a general workflow:

  1. Create a new Git branch: Create a new branch from the main branch, e.g., git branch dev.
  2. Create a new Terraform workspace: Run terraform workspace new dev to create a new workspace named "dev".
  3. Switch to the new Terraform workspace: Run terraform workspace select dev to switch to the "dev" workspace.
  4. Make changes and commit to the Git branch: Make changes to your infrastructure configuration files and commit them to the "dev" branch using git add and git commit.
  5. Apply the changes to the Terraform workspace: Run terraform apply to apply the changes to the "dev" workspace.
  6. Repeat for other branches: Create new workspaces for other branches, e.g., terraform workspace new prod, and switch to them using terraform workspace select prod.

Associating Git branches with Terraform workspaces

To associate a Git branch with a Terraform workspace, you can use a naming convention, such as:

  • dev branch -> dev workspace
  • main branch -> prod workspace

Alternatively, you can use a more explicit approach by creating a file, e.g., .gitignore, with a rule that associates the workspace name with the Git branch name.

Example

Assuming you have a Git repository with main and dev branches, and you want to create a new Terraform workspace for the dev branch:

  1. git branch dev (create a new branch)
  2. terraform workspace new dev (create a new workspace)
  3. terraform workspace select dev (switch to the new workspace)
  4. Make changes and commit to the dev branch
  5. terraform apply (apply the changes to the dev workspace)

When switching between branches, you can use terraform workspace select to switch to the corresponding workspace. For example, when switching to the main branch, you can run terraform workspace select prod to switch to the prod workspace.

@pizofreude
Copy link
Author

pizofreude commented Oct 12, 2025

Forgot to create new branch before making changes?

No worries! This is a common mistake and easily fixable. Here's how to move your commits to a new branch:

🔧 Fix: Move Commits to New Branch

Option 1: Create Branch from Current State (Recommended)

# Create a new branch with all your current commits
git checkout -b feat/prometheus-metrics

# Now you're on the new branch with all your commits!
# Push to remote
git push -u origin feat/prometheus-metrics

That's it! Your commits are now on a feature branch.


Option 2: If You Already Pushed to main

If you already pushed commits to main and want to move them:

# 1. Create new branch with current commits
git checkout -b feat/prometheus-metrics

# 2. Push the new branch
git push -u origin feat/prometheus-metrics

# 3. Go back to main
git checkout main

# 4. Reset main to match remote (removes your commits from main)
git reset --hard origin/main

# 5. Push main (if you have permission to force push)
git push --force-with-lease origin main

⚠️ Warning: Only use --force-with-lease if you're the only one working on main or have coordinated with your team!


Option 3: Soft Reset (Keep Changes Uncommitted)

If you want to uncommit everything and start fresh:

# 1. See how many commits you made
git log --oneline

# 2. Reset to before your commits (e.g., 9 commits back)
git reset --soft HEAD~9

# 3. Create new branch
git checkout -b feat/prometheus-metrics

# 4. Re-commit everything
git add .
git commit -m "feat(metrics): add Prometheus metrics endpoint

Complete implementation of Prometheus metrics collection with:
- Metrics middleware for HTTP tracking
- Prometheus and Grafana Docker setup
- Configuration support
- Comprehensive tests and documentation

Closes #11"

# 5. Push
git push -u origin feat/prometheus-metrics

✅ Recommended Approach for Your Situation

EXAMPLE: If you've made some commits already but haven't push any, I recommend Option 1:

# Create branch with all your commits
git checkout -b feat/prometheus-metrics

# Push to remote
git push -u origin feat/prometheus-metrics

# Verify you're on the new branch
git branch

# Your commits are safe!
git log --oneline

🎯 After Creating the Branch

Now you can:

  1. Create PR from the new branch:

    • Go to GitHub
    • You'll see a prompt to create PR from feat/prometheus-metrics
    • Create PR targeting main
  2. Continue working:

    # Make more changes if needed
    git add .
    git commit -m "docs: update README troubleshooting section"
    git push

📝 Summary

The easiest fix:

git checkout -b feat/prometheus-metrics
git push -u origin feat/prometheus-metrics

Then create your PR from this new branch! All your commits are preserved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment