Revert or cherry-pick a pull request in Azure DevOps
Azure Repos has two buttons that reuse a pull request's changes. Revert undoes them; Cherry-pick copies them onto another branch, for example a hotfix for a release branch. Neither one changes a branch directly: each makes a new topic branch and asks you to open a new pull request from it, so the usual reviews and branch policies still apply. Below are the steps, the REST calls, and what to do when Azure cannot do it, from Microsoft's Complete, abandon, or revert pull requests, Copy changes with cherry-pick and the REST reference.
Revert a completed pull request
Open the completed pull request and select Revert. In the Revert pull request pane, choose the Target branch where the changes should be undone, change the Topic branch name if you like, and select Revert. On the New pull request screen, select Create, then complete that pull request to finish the revert.
The new branch has a single commit that undoes all the file changes of the original pull request. It does not revert the original commits one by one.
Cherry-pick into another branch
On a completed pull request, Cherry-pick is on the Overview page; on an active one, it is in the More options menu. Azure makes a new topic branch, cherry-picks all of the pull request's changes onto it, and asks you to create a pull request from it into the branch you chose. Microsoft calls this limited support, meant for applying a hotfix to a target branch; for anything finer, such as a single commit, use git (below).
From the REST API
Both are operations that run in the background. Start one with the pull request as the source, the branch to apply it to (ontoRefName) and a name for the new branch (generatedRefName):
POST https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repository}/reverts?api-version=7.1
{"source": {"pullRequestId": 21},
"ontoRefName": "refs/heads/main",
"generatedRefName": "refs/heads/revert-pr-21"}
For a cherry-pick, send the same body to .../cherryPicks?api-version=7.1 with the other branch as ontoRefName. The answer has an id and a status of queued or inProgress; read .../reverts/{id} (or .../cherryPicks/{id}) until it is completed or failed, then open a pull request from the new branch (az repos pr create does that from the CLI).
When it fails, detailedStatus says why: conflict is true when the changes did not apply cleanly, timedout when it ran too long, and failureMessage has the text. Its status code names the other reasons, among them createBranchPermissionRequired (you may not create branches), refNameConflict (the new name differs only in case from an existing branch), invalidRefName and targetBranchDeleted.
When Azure cannot do it: git on your machine
A conflict has to be resolved by hand. Make a branch from the target, then revert or cherry-pick the commit the completion made there and push the branch:
git fetch origin
git switch -c revert-pr-21 origin/main
git revert -m 1 <merge commit> # completed with Merge or Semi-linear merge
git revert <squash commit> # completed with Squash commit
git push -u origin revert-pr-21
A merge commit has two parents, and -m 1 tells git to keep the target branch's side (git revert). A pull request completed with Rebase and fast-forward left its commits on the target one by one, so revert those commits instead. For a cherry-pick, use git cherry-pick the same way on a branch made from the other target. Each completion is explained under merge types.
Bringing reverted changes back
After a pull request completed with Merge (no fast-forward) is reverted, opening a new pull request from the original source branch does not bring the changes back: its commits are already in the target branch's history, so git finds nothing new to merge. git's documentation describes the same effect for a reverted merge. Revert the revert instead: select Revert on the revert's own completed pull request.
In IntelliJ IDEA and Rider
Azure DevOps Pull Requests, a plugin by Lesstep for JetBrains IDEs 2024.1 or later and Android Studio, lists the pull requests of Azure DevOps Services and Azure DevOps Server in an IDE tool window. Revert and Cherry-pick are buttons on the web page and are not in the plugin. When you do it with git instead, the IDE's own Git log has Revert Commit and Cherry-Pick (JetBrains: undo changes), and once the branch is pushed the plugin creates the pull request from it, with the repository's template and reviewers from its recent pull requests. Creating pull requests is a paid feature with a 30-day free trial; reading stays free.
Get it on JetBrains Marketplace
Azure DevOps is a trademark of Microsoft Corporation; IntelliJ IDEA and Rider are trademarks of JetBrains s.r.o. This page is not affiliated with or endorsed by Microsoft or JetBrains.
More: merge types · abandon and reactivate · change the target branch · merge conflicts · 日本語