When you’re working with AI background agents, it’s not unusual to end up with a flood of pull requests. Each PR lives on its own branch, and while that’s great for automation, it creates a new problem: keeping all those branches in sync with your dev branch. Manually checking, merging, and rebasing them one by one is nobody’s idea of fun.
That’s exactly why I wrote this PowerShell script. It automatically rebases your PR branches by taking the commit they were based on and updating them against the latest dev branch. The result? You save time, avoid merge drift, and keep your PRs clean and ready to review.
In this post, I’ll walk through what the script does, how to set it up, and how you can adapt it for your own projects. Whether you’re experimenting with AI agents that generate code or just juggling a lot of PRs, this simple automation can make your GitHub workflow a whole lot smoother.
To see the full gist: Rebase-PRs
Script Parameters and Usage
-OldDevCommit : The commit hash of the old dev branch from which PR branches were originally branched.
-DryRun : Optional switch to simulate the process without making any changes.
Safety Checks
The script ensures it never operates on the 'main' branch for safety reasons. - It checks the current branch and exits if it is 'main'.
Fetching Latest Dev Branch
The script checks out the dev branch, fetches the latest changes from origin, and pulls updates.
Processing Remote Branches
The script lists all remote branches except protected ones (dev and main). - For each branch, it checks if the fork point with dev matches the old dev commit.
Rebasing Matching Branches
For branches based on the old dev commit, the script rebases them onto the latest dev branch and force pushes the updates to the remote.
Conflict Handling
If a conflict occurs during rebase, the script stops and alerts for manual resolution.
Final Steps
After successful rebase, the branch is force pushed with lease to update the PR branch on the remote.