Git Usecases & Scenarios
Real situations encountered in this repository with documented solutions. Future scenarios will be logged here.
Usecase #1: Non-Linear Branch History (Unwanted Merge Commit)
Date: 2026-01-05
Status: ✅ RESOLVED
Branch: feature/localstack-crossplane
Category: Branch Management
Scenario
Feature branch had a merge commit from main, creating non-linear history. When attempting to push and create a PR, the history looked messy with unnecessary merge commits instead of a clean linear progression.
Problem Statement
- Expected: Clean linear commits on top of main
- Actual: Merge commit created history with branching
- Impact: PR merge to main would preserve merge commit unnecessarily
- Root: Used
git merge origin/maininstead ofgit rebase
Solution
Step 1: Create clean temporary branch from origin/main
git checkout -b temp/feature-clean origin/mainStep 2: Squash all feature changes into one commit
git merge --squash feature/localstack-crossplaneStep 3: Commit squashed changes
git commit -m "feat(crossplane): add EC2 instance support and update documentation (squashed)"Step 4: Delete old remote branch
git push origin --delete feature/localstack-crossplaneStep 5: Push clean branch to origin
git push origin HEAD:feature/localstack-crossplane -uStep 6: Replace local branch
git branch -D feature/localstack-crossplane
git branch -m feature/localstack-crossplaneResult
- ✅ Linear history: Single commit on top of main
- ✅ No merge commits
- ✅ Clean repository ready for PR and merge
Key Lessons
- Use
git merge --squashfor combining all changes into one commit - Use
git rebasefor replaying commits on top of main (linear history) - Avoid
git mergewhen you want linear history - Safe force push: Always use
--force-with-leaseinstead of--force
Related Commands
# View commits on your branch but not in main
git log --oneline origin/main..HEAD
# Safe force push after history rewrite
git push origin branch --force-with-leaseUsecase #2: Creating Pull Requests
Date: 2026-01-05
Status: ✅ DOCUMENTED
Category: GitHub Workflow
Scenario
After completing feature work with clean commits, need to create a Pull Request to propose changes for review and eventual merge into main. This usecase covers both Web UI and CLI approaches.
Problem Statement
- Need: Consistent way to create PRs
- Options: GitHub Web UI or GitHub CLI
- Goal: Fast, documented PR creation process
Solution
Method 1: GitHub Web UI (Most Visual)
Step 1: Navigate to Pull Requests
- Go to: https://github.com/OpsAlchemy/kubequest
- Click "Pull Requests" tab
- Click "New Pull Request"
Step 2: Select Branches
- Base:
main(destination branch) - Compare: Your feature branch (e.g.,
feature/git-management-and-workflows)
Step 3: Review Diff
- Check all changes are correct
- Ensure only intended files are modified
Step 4: Fill PR Details
- Title: Follow convention (feat:, fix:, docs:, chore:)
- Description: Explain what changed and why
- Reviewers: Add team members
- Labels: Add if needed (bug, feature, documentation)
Step 5: Create PR
- Click "Create Pull Request"
Method 2: GitHub CLI (Faster for Terminal Users)
Setup (first time):
# Install gh
sudo apt install gh
# Authenticate
gh auth loginCreate PR:
# Simple PR
gh pr create --title "feat: add feature" --body "Description"
# With options
gh pr create \
--title "feat: add git management docs and refactor deploy workflow" \
--body "Adds incident tracking, command reference, and dynamic reusable workflow" \
--base main
# Create as draft (not ready for review yet)
gh pr create --title "WIP: feature" --body "Draft for feedback" --draft
# Open in browser after creation
gh pr create --title "My PR" --body "Details" --webPR Template Example
Title Format:
feat: add git management docs and refactor deploy workflowDescription Template:
## Changes
- Added Git usecases tracking system
- Added command reference guide
- Refactored deploy workflow with reusable actions
## Why
- Team needs centralized Git problem-solving documentation
- Deploy workflow was duplicating code
- Easier to add new docs in future
## Related
- Fixes: Non-linear branch history issue
- Closes: #XX (if applicable)Checklist Before Submitting
- ✅ Branch has clean, linear history
- ✅ All commits are pushed to origin
- ✅ Branch is up-to-date with main (no conflicts)
- ✅ Code follows project conventions
- ✅ Documentation updated if needed
- ✅ Related scenarios logged in this file
- ✅ PR title follows convention (feat:, fix:, docs:)
- ✅ Description is clear and concise
- ✅ All related issues mentioned
After PR Creation
Monitor PR Status:
# View your PR
gh pr view
# View in browser
gh pr view --web
# List all open PRs
gh pr list
# Check comments
gh pr view --commentsResponding to Reviews:
- Make changes in local branch
- Commit:
git add . && git commit -m "address review feedback" - Push:
git push origin feature-branch - PR auto-updates with new commits
- Reply to review comments on GitHub
- Request re-review when ready
Merge PR:
Via CLI (recommended for squash + linear history):
# Squash merge (combines all commits into one)
gh pr merge --squash
# Merge and delete branch automatically
gh pr merge --squash --delete-branchVia Web UI:
- Click "Merge pull request"
- Choose "Squash and merge" strategy
- Click "Confirm merge"
- Delete branch
Result
- ✅ PR created and visible on GitHub
- ✅ Team can review and comment
- ✅ Clear documentation of changes
- ✅ Ready for merge after approval
Key Lessons
- Use descriptive PR titles following conventions
- Write clear, concise descriptions
- Squash merge preserves linear history
- Always use
--delete-branchto clean up after merge - GitHub CLI is faster for repetitive PR creation
Related Resources
Usecase #3: Linearizing Messy History with Multiple Branches & Merges
Date: 2026-01-09
Status: ✅ RESOLVED
Branch: main
Category: History Management
Scenario
After multiple feature branches were merged into main with nested merges and branch-backs, the commit history became non-linear and confusing. Multiple GitHub Actions fix branches (PR #11, #13, #14) created a complex branching pattern with merge commits instead of a clean linear history.
Problem Statement
- Expected: Clean linear history on main
- Actual: Complex branching with nested merge commits and back-merges
- Graph pattern showed:
|\,|/,/|patterns (branches diverging and rejoining) - Impact: Difficult to understand what actually changed, hard to review history
- Root: Multiple parallel fixes merged in sequence without squashing or rebasing
Solution
Key Insight: Use git reset --soft to rewind history while keeping all changes, then create a single clean commit.
Step 1: Identify the base commit (where the mess started)
git log --oneline --graph -15 # Find where branches startedStep 2: Create a backup branch (safety)
git branch backup-before-squashStep 3: Reset to base commit, keeping all changes
git reset --soft <base-commit-hash>In our case: git reset --soft c2562b4
This is the magic:
--softrewinds your branch pointer- All your changes stay staged and ready to commit
- All the messy merge commits disappear
- No code is lost
Step 4: Create one clean commit with all changes
git commit -m "fix: consolidate all GitHub Actions improvements and fixes"Step 5: Force push the linearized history
git push origin main --force-with-leaseThe --force-with-lease flag is safe because it:
- Allows rewriting local history
- Prevents overwriting others' work
- Only force-pushes if remote hasn't changed
Result
- ✅ Linear history: All commits in a straight line
- ✅ No merge commits: Cleaner graph
- ✅ All changes preserved: Nothing lost
- ✅ Remote updated: Pushed to GitHub successfully
- ✅ Backup available: Can always revert if needed
Before:
* 189650d Merge pull request #11
|\
| * 1eab8af Merge branch 'main' into fix/github-actions-deploy
| |\
| |/
|/|
* | 50ab2d0 Fix GitHub Actions workflowsAfter:
* 8c1656e fix: consolidate all GitHub Actions improvements and fixes
* addf2e8 Add Kustomize and Helm practice files
* 321eca8 feat: refactor deploy workflowKey Lessons
git reset --soft <commit>is your friend for squashing history- Goes back to any commit
- Keeps all changes
- Lets you rewrite commits however you want
Three types of reset:
--soft: Keep changes staged (ready to commit)--mixed: Keep changes unstaged (in working directory)--hard: Delete changes completely (⚠️ dangerous)
--force-with-leaseis safe:- Always use instead of
--force - Prevents accidentally overwriting others' work
- Default for rewriting pushed history
- Always use instead of
Create backups before major rewrites:
bashgit branch backup-before-squashIf something goes wrong:
git reset --hard backup-before-squashYou can reset to any point:
bashgit reset --soft HEAD~3 # Last 3 commits git reset --soft c2562b4 # Specific commit git reset --soft origin/main # Match remote exactly
Common Use Cases for This Technique
- Squash messy feature branch commits before merging to main
- Linearize complex merge histories (like we just did)
- Reorganize commits in any way you want
- Undo multiple commits but keep the work
- Combine parallel work from multiple branches
Related Commands
# See what commits you'll lose/gain
git log --oneline origin/main..HEAD
# After reset, before commit - review staged changes
git diff --cached
# View the reflog to recover anything
git reflogReferences
How to Log a New Usecase
When you encounter a new Git scenario:
Add a new section:
## Usecase #N: [Title]Fill in: Date, Status, Category
Include these subsections:
- Scenario: What happened
- Problem Statement: What was wrong/unexpected
- Solution: Step-by-step fix with code blocks
- Result: What changed/improved
- Key Lessons: Takeaways for future
- Related Commands/Resources: Links to other docs
Commit and create a PR to promote to main
Categories
- Branch Management: Creating, switching, deleting branches
- History Management: Rebase, squash, merge strategies
- GitHub Workflow: PRs, reviews, merges
- Conflict Resolution: Handling merge conflicts
- Remote Sync: Fetch, pull, push issues
- Undo/Recovery: Reverting commits, recovering deleted code