| name | git-pr-workflow |
| description | Automates the complete git and GitHub workflow: initializes a git repository, creates a private GitHub repository if needed, commits changes, creates a pull request, monitors GitHub Actions for completion, and merges the PR if all checks pass. |
Git PR Workflow Skill
Description
Automates the complete git and GitHub workflow: initializes a git repository, creates a private GitHub repository if needed, commits changes, creates a pull request, monitors GitHub Actions for completion, and merges the PR if all checks pass.
Usage
Invoke this skill when you need to:
- Initialize a new git repository and push to GitHub
- Create a pull request with automatic CI/CD verification
- Wait for GitHub Actions to complete before merging
- Automatically merge PRs that pass all checks
Instructions
You are an expert at git and GitHub workflows. Your task is to automate the complete process of initializing a repository, creating a PR, and merging it after CI passes.
Step 0: Confirm GitHub CLI Auth
Check that the GitHub CLI is authenticated:
gh auth status
If not authenticated, stop and ask the user to run gh auth login.
Step 1: Initialize Git Repository (if needed)
First, check if the current directory is already a git repository:
git rev-parse --is-inside-work-tree 2>/dev/null
If not a git repository, initialize it:
git init
Step 2: Check for GitHub Remote
Check if a GitHub remote already exists:
git remote -v
If no remote exists, check if a GitHub repository exists for this project:
gh repo view --json name,owner 2>&1
Step 3: Create GitHub Repository (if needed)
If the repository doesn't exist on GitHub, create a new private repository:
gh repo create $(basename "$PWD") --private --source=. --remote=origin --push
This command:
- Creates a private repository with the current directory name
- Sets the current directory as the source
- Adds the remote as 'origin'
- Pushes the initial commit
If the repository already exists but no remote is configured:
gh repo view --json nameWithOwner --jq .nameWithOwner | xargs -I {} git remote add origin "https://github.com/{}.git"
Step 4: Ensure Main Branch Exists
Make sure we have an initial commit on main:
git add .
git commit -m "chore: initial commit" || echo "Already has commits"
git branch -M main
git push -u origin main
If the push fails due to non-fast-forward, stop and ask the user before proceeding.
Step 5: Determine Base Ref
Before creating a feature branch, determine the appropriate base ref:
git fetch origin main || true
BASE_REF="origin/main"
if ! git show-ref --verify --quiet refs/remotes/origin/main; then
BASE_REF="main"
fi
Step 6: Create Feature Branch
Create a new feature branch from the base ref:
BRANCH_NAME="feature/automated-changes-$(date +%s)"
git checkout -b "$BRANCH_NAME" "$BASE_REF"
Step 7: Apply Changes
Commit any uncommitted changes on the feature branch:
if [ -n "$(git status --porcelain)" ]; then
git add .
git commit -m "chore: automated changes"
else
echo "No changes to commit; stop and confirm next steps."
exit 1
fi
Step 8: Push Branch
Push the feature branch to GitHub:
git push -u origin "$BRANCH_NAME"
Step 9: Create Pull Request
Create a pull request using the gh CLI:
PR_URL=$(gh pr create \
--title "Automated changes" \
--body "$(cat <<'EOF'
## Summary
Automated changes created via agent-assisted workflow
## Changes
- Repository initialized and configured
- All current changes committed
🤖 Generated with an agent-assisted workflow.
EOF
)" \
--base main \
--head "$BRANCH_NAME" \
--json url \
--jq .url)
echo "PR created: $PR_URL"
Step 10: Extract PR Number
Extract the PR number from the URL:
PR_NUMBER=$(echo "$PR_URL" | grep -oE '[0-9]+$')
echo "PR Number: $PR_NUMBER"
Step 11: Monitor GitHub Actions
Wait for GitHub Actions to complete. First, wait a few seconds for checks to start:
echo "Waiting for checks to start..."
sleep 10
Then monitor the checks status:
MAX_WAIT=600
ELAPSED=0
CHECK_INTERVAL=15
while [ $ELAPSED -lt $MAX_WAIT ]; do
echo "Checking PR status... (${ELAPSED}s elapsed)"
PENDING=$(gh pr checks "$PR_NUMBER" --json state --jq '[.[] | select(.state == "PENDING" or .state == "IN_PROGRESS")] | length')
if [ "$PENDING" -eq 0 ]; then
echo "All checks completed!"
FAILED=$(gh pr checks "$PR_NUMBER" --json conclusion --jq '[.[] | select(.conclusion == "FAILURE" or .conclusion == "CANCELLED")] | length')
if [ "$FAILED" -gt 0 ]; then
echo "❌ Some checks failed:"
gh pr checks "$PR_NUMBER" --json name,conclusion --jq '.[] | select(.conclusion == "FAILURE" or .conclusion == "CANCELLED") | " - \(.name): \(.conclusion)"'
exit 1
fi
echo "✅ All checks passed!"
break
fi
echo "Checks still running: $PENDING pending"
sleep $CHECK_INTERVAL
ELAPSED=$((ELAPSED + CHECK_INTERVAL))
done
if [ $ELAPSED -ge $MAX_WAIT ]; then
echo "⏱️ Timeout waiting for checks to complete"
exit 1
fi
Step 12: Merge Pull Request
If all checks pass, merge the PR:
gh pr merge "$PR_NUMBER" --auto --squash --delete-branch
echo "✅ PR merged successfully!"
Step 13: Switch Back to Main
Finally, switch back to the main branch and pull the merged changes:
git checkout main
git pull origin main
echo "✅ Workflow complete! All changes merged to main."
Error Handling
Throughout the process:
- Check return codes after each command
- Provide clear error messages if something fails
- If GitHub Actions fail, display which checks failed and why
- If the merge fails, explain what went wrong
Success Criteria
The skill succeeds when:
- Git repository is initialized (or already exists)
- GitHub repository exists and is accessible
- Changes are committed to a feature branch
- Pull request is created
- All GitHub Actions checks pass
- PR is successfully merged to main
- Local main branch is updated
Notes
- The repository created will be private by default
- The skill waits up to 10 minutes for GitHub Actions to complete
- If checks don't start within the timeout period, the skill will fail
- The feature branch is automatically deleted after merge