You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过命令行创建TFS pull request?

Creating a TFS Pull Request via 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-org if 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 with refs/heads/)
  • targetRefName: The branch you want to merge into (usually main or develop)
  • title: A clear, concise title for your PR
  • description: 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 -u flag 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 10:38:22