REST API Endpoints For Git Tags
Last Updated :
23 Aug, 2024
In Git, tags are used to mark specific commits as important, typically signifying a release. Unlike branches, tags are immutable references, making them perfect for marking stable points in your repository’s history, such as version releases.
Why Use REST API for Git Tags?
Interacting with Git tags via REST APIs allows for easy integration with CI/CD pipelines, automated deployment processes, or custom tooling that requires reading or creating tags programmatically.
Understanding the REST API Endpoints for Git Tags
Here’s a look at the main REST API endpoints provided by popular platforms like GitHub, GitLab, and Bitbucket for working with tags.
1. Listing Tags
To fetch a list of all tags in a repository, you can use the following endpoint:
GitHub:
GET /repos/{owner}/{repo}/tags
Example:
curl -H "Authorization: token YOUR_GITHUB_TOKEN" \
https://api.github.com/repos/username/repository/tags
This request returns a JSON array with information about each tag, including the tag name, commit SHA, and associated URLs.
GitLab:
GET /projects/{id}/repository/tags
Example:
curl --header "PRIVATE-TOKEN: YOUR_GITLAB_TOKEN" \
"https://gitlab.com/api/v4/projects/your_project_id/repository/tags"
The response contains an array of tag details, such as tag names, message, and commit information.
Bitbucket:
GET /repositories/{workspace}/{repo_slug}/refs/tags
Example:
curl -u username:password \
https://api.bitbucket.org/2.0/repositories/workspace/repo_slug/refs/tags
The response includes metadata about each tag, including the commit hash, tagger details, and more.
2. Fetching a Specific Tag
To retrieve information about a specific tag by its name:
GitHub:
GET /repos/{owner}/{repo}/git/ref/tags/{tag_name}
Example:
curl -H "Authorization: token YOUR_GITHUB_TOKEN" \
https://api.github.com/repos/username/repository/git/ref/tags/v1.0.0
GitLab:
GET /projects/{id}/repository/tags/{tag_name}
Example:
curl --header "PRIVATE-TOKEN: YOUR_GITLAB_TOKEN" \
"https://gitlab.com/api/v4/projects/your_project_id/repository/tags/v1.0.0"
This endpoint fetches details about a specific tag, including the commit SHA and release notes (if available).
3. Creating a New Tag
To create a new tag in your repository, you can make a POST request:
GitHub:
POST /repos/{owner}/{repo}/git/tags
Example:
curl -X POST -H "Authorization: token YOUR_GITHUB_TOKEN" \
-d '{
"tag": "v1.0.0",
"message": "Release version 1.0.0",
"object": "commit_sha",
"type": "commit",
"tagger": {
"name": "Your Name",
"email": "[email protected]",
"date": "2024-08-22T14:00:00Z"
}
}' https://api.github.com/repos/username/repository/git/tags
After creating the tag object, you need to create the reference:
POST /repos/{owner}/{repo}/git/refs
Example:
curl -X POST -H "Authorization: token YOUR_GITHUB_TOKEN" \
-d '{
"ref": "refs/tags/v1.0.0",
"sha": "commit_sha"
}' https://api.github.com/repos/username/repository/git/refs
GitLab:
POST /projects/{id}/repository/tags
Example:
curl --header "PRIVATE-TOKEN: YOUR_GITLAB_TOKEN" \
-X POST "https://gitlab.com/api/v4/projects/your_project_id/repository/tags" \
-d "tag_name=v1.0.0&ref=main&message=Release version 1.0.0"
Bitbucket:
POST /repositories/{workspace}/{repo_slug}/refs/tags
Example:
curl -X POST -u username:password \
-H "Content-Type: application/json" \
-d '{
"name": "v1.0.0",
"target": {
"hash": "commit_sha"
}
}' https://api.bitbucket.org/2.0/repositories/workspace/repo_slug/refs/tags
4. Deleting a Tag
To delete a tag programmatically:
GitHub:
DELETE /repos/{owner}/{repo}/git/refs/tags/{tag_name}
Example:
curl -X DELETE -H "Authorization: token YOUR_GITHUB_TOKEN" \
https://api.github.com/repos/username/repository/git/refs/tags/v1.0.0
GitLab:
DELETE /projects/{id}/repository/tags/{tag_name}
Example:
curl --header "PRIVATE-TOKEN: YOUR_GITLAB_TOKEN" \
-X DELETE "https://gitlab.com/api/v4/projects/your_project_id/repository/tags/v1.0.0"
Bitbucket:
DELETE /repositories/{workspace}/{repo_slug}/refs/tags/{tag_name}
Example:
curl -X DELETE -u username:password \
https://api.bitbucket.org/2.0/repositories/workspace/repo_slug/refs/tags/v1.0.0
Best Practices for Working with Git Tags via REST API
- Use Semantic Versioning: Consistently using semantic versioning (e.g., v1.0.0) makes it easier to manage and automate version control workflows.
- Secure API Requests: Always use tokens (like GitHub's personal access tokens) for secure API requests. Never expose sensitive data in your scripts.
- Automate Tagging in CI/CD: Integrate tag creation and management in your CI/CD pipelines to automate versioning during releases.
Similar Reads
REST API Endpoints For GitHub Actions Variables
GitHub Actions is used to automate workflows, build, test, and deploy code. To make workflows more dynamic and secure, GitHub Actions allows you to use variables, which can store data like configuration values, secrets, or other necessary information. GitHub exposes a REST API to manage these variab
5 min read
What is an API Endpoint ?
The API endpoint is the specific URL where requests are sent to interact with the API. In this article, we will discuss API Endpoint their working and the differences between REST API and GraphQL endpoints. Table of Content What is an API Endpoint?How do API endpoints work?What are some best practic
7 min read
Best Practices For REST API Testing
REST, or Representational State Transfer, is a type of software architecture that is commonly used for building web services and APIs. A REST API is an application programming interface (API) that uses REST principles to expose data and functionality for client applications to access. REST APIs are
8 min read
Getting Started With GitHub REST API
The GitHub REST API is a powerful tool that allows developers to interact with a list of features of GitHub. Whether you're automating tasks, building integrations, or simply managing your GitHub resources more efficiently, the REST API provides a versatile and accessible entry point. In this articl
5 min read
How to test API Endpoints with Postman and Express ?
Postman, a popular API development and testing tool allowing developers to interact with APIs. In this guide, we'll explore the basics of testing API endpoints using Postman and Express, providing clear steps and examples. Prerequisites:Basics of Express JS and Node JS.Postman should be installed.St
2 min read
Best Coding Practices For Rest API Design
JSON, Endpoints, Postman, CRUD, Curl, HTTP, Status Code, Request, Response, Authentication, All these words are familiar to you if you are in backend development and you have worked on API (Application Programming Interface). Being a developer you might have worked on some kind of APIs (especially t
10 min read
How to add remote origin in git?
Git, most popular version control system, revolutionized the way developers collaborate and manage code. One important feature of Git is remote repositories, which serve as centralized hubs for code collaboration. In this article, we'll explore the process of adding a remote origin to your Git repos
2 min read
What is REST API in Node.js ?
NodeJS is an ideal choice for developers who aim to build fast and efficient web applications with RESTful APIs. It is widely adopted in web development due to its non-blocking, event-driven architecture, making it suitable for handling numerous simultaneous requests efficiently. But what makes Node
7 min read
Why REST API is Important to Learn?
API... Being a developer what comes to your mind first when you listen to this word... JSON, Endpoints, Postman, CRUD, Curl, HTTP, Status Code, Request, Response, Authentication, or something else... If you're familiar with the above word then surely you might have worked on some kinds of APIs (espe
8 min read
REST API Introduction
REST API stands for REpresentational State Transfer API. It is a type of API (Application Programming Interface) that allows communication between different systems over the internet. REST APIs work by sending requests and receiving responses, typically in JSON format, between the client and server.
7 min read