如何通过命令行创建TFS pull request?
curl (No Browser Needed!) Absolutely, ditching the web UI for command-line PR creation makes total sense when you're already working in the terminal. Let's walk through exactly how to do this with curl against the TFS REST API.
Step 1: Generate a Personal Access Token (PAT)
First, you'll need a PAT to authenticate your request—this replaces logging into the browser. Head to your TFS instance's User Settings > Personal Access Tokens, create a new token with at least the following permissions:
- Code: Read & write
- Pull Requests: Read & write
Make sure to save the token somewhere safe once it's generated (you won't see it again!).
Step 2: Build the curl Request
The TFS REST API uses a POST endpoint to create pull requests. Here's the full breakdown:
API Endpoint
Replace the placeholders with your TFS details:
https://<your-tfs-server>/<project-collection>/<team-project>/_apis/git/repositories/<repo-id>/pullrequests?api-version=7.1-preview.1
<your-tfs-server>: Your TFS instance URL (e.g.,dev.azure.com/your-orgif using Azure DevOps, the modern TFS variant)<project-collection>: Your project collection name<team-project>: Your specific team project name<repo-id>: The ID of your target repository (see the pro tip below to find this)
Request Body (JSON)
You need to send a JSON payload with core PR details. Here's a minimal, usable example:
{ "sourceRefName": "refs/heads/feature/your-branch-name", "targetRefName": "refs/heads/main", "title": "Add new user authentication flow", "description": "This PR implements JWT-based auth for the user login endpoint. Includes unit tests and updated docs." }
sourceRefName: The branch you want to merge from (must be the full ref path starting withrefs/heads/)targetRefName: The branch you want to merge into (usuallymainordevelop)title: A clear, concise title for your PRdescription: Optional but recommended—add context, test steps, or linked work items here
Full curl Command
Put it all together with authentication (we use basic auth here: the username can be anything, and the password is your PAT):
curl -X POST \ -u :<your-pat> \ -H "Content-Type: application/json" \ -d '{ "sourceRefName": "refs/heads/feature/your-branch-name", "targetRefName": "refs/heads/main", "title": "Add new user authentication flow", "description": "This PR implements JWT-based auth for the user login endpoint. Includes unit tests and updated docs." }' \ "https://<your-tfs-server>/<project-collection>/<team-project>/_apis/git/repositories/<repo-id>/pullrequests?api-version=7.1-preview.1"
Pro Tip: Find Your Repository ID
If you don't know your repo ID, fetch it with a quick curl request:
curl -u :<your-pat> \ "https://<your-tfs-server>/<project-collection>/<team-project>/_apis/git/repositories?api-version=7.1-preview.1"
Look for the id field in the response for your target repository.
Troubleshooting Common Issues
- 401 Unauthorized: Double-check your PAT is correct and has the right permissions. Remember, the username part in the
-uflag can be empty (just:<your-pat>). - 404 Not Found: Verify all URL placeholders are filled correctly (server, collection, project, repo ID) and that the API version is valid.
- 400 Bad Request: Make sure your JSON payload is valid—check for typos in branch names (they must match exactly, including case) and missing required fields (sourceRefName, targetRefName, title are mandatory).
Once you run the command successfully, you'll get a JSON response with all details of your newly created PR, including its ID and URL if you ever need to jump to it later.
内容的提问来源于stack exchange,提问作者John Henckel

