You can create a script to automate this workflow and integrate it into your main development process. Below is a shell script (push.sh
) that implements the described setup. It requires the GitHub CLI (gh
) and assumes you are comfortable with shell scripting and basic Git operations.
#!/bin/bash
# Exit on error
set -e
# Function to display usage
usage() {
echo "Usage: $0 \"Pull Request Title (with optional #issue reference)\""
exit 1
}
# Check if a title was provided
if [ -z "$1" ]; then
usage
fi
# Variables
PR_TITLE="$1" # Pull Request title from argument
BRANCH_NAME="temp-branch-$(date +%s)" # Unique branch name with timestamp
MAIN_BRANCH="main" # Main branch name
REMOTE="origin" # Remote name (default is 'origin')
# Step 1: Create and checkout a new branch
git checkout -b "$BRANCH_NAME"
# Step 2: Add all changes and commit
git add .
if git diff-index --quiet HEAD --; then
echo "No changes to commit."
exit 0
fi
git commit -m "$PR_TITLE"
# Step 3: Push the new branch to GitHub
git push "$REMOTE" "$BRANCH_NAME"
# Step 4: Open a Pull Request using GitHub CLI
gh pr create --base "$MAIN_BRANCH" --title "$PR_TITLE" --body "Automated PR: $PR_TITLE"
# Step 5: Merge the Pull Request (requires GitHub CLI)
gh pr merge --auto --delete-branch
# Step 6: Cleanup local branch
git checkout "$MAIN_BRANCH"
git branch -d "$BRANCH_NAME"
# Done
echo "Pull request created, merged, and cleaned up successfully!"
-
Save the Script
Save the script above to a file, e.g.,push.sh
. Make it executable:chmod +x push.sh
-
Install Dependencies
- Install the GitHub CLI: GitHub CLI Installation Guide.
- Ensure you are authenticated in
gh
by running:gh auth login
-
Set Up Your Git Repository
Ensure you have a Git repository with themain
branch set up. You should also have permissions to push to and manage branches. -
Usage
When you're ready to push changes, use the script like this:./push.sh "Fixes #123: Correct login logic and improve performance"
- This will:
- Create a branch.
- Commit all staged changes with the specified message.
- Push the branch to GitHub.
- Create a PR with the title.
- Automatically merge the PR and clean up the branch.
- This will:
-
Optional Integration
- Add the script to your
PATH
for easy use from anywhere:mv push.sh /usr/local/bin/push
- Now, you can run
push
from any Git repository:push "Closes #456: Implement caching for API responses"
- Add the script to your
-
Script Customization:
Adjust the branch naming convention or add more options if needed, such as setting a PR description. -
Release Notes:
With this setup, every PR title will become part of your commit history, and tools like GitHub’s release note generator will automatically use them. -
Handling Merge Conflicts:
If a merge conflict occurs duringgh pr merge
, you will need to resolve it manually. -
Working Off
main
:
This script aligns with your strategy of minimizing branch management while still leveraging PRs for tracking and linking to issues.
Let me know if you need further help refining or expanding this script!