Low-level Git operations for blobs, commits, references, tags, and trees.
The GitService provides methods for low-level Git database operations.
type GitService struct{}// GetBlob fetches a blob from a repository
func (s *GitService) GetBlob(ctx context.Context, owner, repo, sha string) (*Blob, *Response, error)
// CreateBlob creates a new blob in a repository
func (s *GitService) CreateBlob(ctx context.Context, owner, repo string, blob *Blob) (*Blob, *Response, error)
type Blob struct {
Content *string
Encoding *string // "utf-8" or "base64"
SHA *string
Size *int
URL *string
NodeID *string
}Usage:
// Create a blob
blob := &github.Blob{
Content: github.Ptr("Hello, World!"),
Encoding: github.Ptr("utf-8"),
}
createdBlob, _, err := client.Git.CreateBlob(ctx, "owner", "repo", blob)
// Get a blob
blob, _, err := client.Git.GetBlob(ctx, "owner", "repo", *createdBlob.SHA)// GetCommit fetches a single commit
func (s *GitService) GetCommit(ctx context.Context, owner, repo, sha string) (*Commit, *Response, error)
// CreateCommit creates a new Git commit object
func (s *GitService) CreateCommit(ctx context.Context, owner, repo string, commit *Commit, opts *CreateCommitOptions) (*Commit, *Response, error)
type Commit struct {
SHA *string
Author *CommitAuthor
Committer *CommitAuthor
Message *string
Tree *Tree
Parents []*Commit
Stats *CommitStats
HTMLURL *string
URL *string
Verification *SignatureVerification
NodeID *string
}
type CommitAuthor struct {
Date *Timestamp
Name *string
Email *string
Login *string
}
type CreateCommitOptions struct {
Author *CommitAuthor
Committer *CommitAuthor
}Usage:
// Create a commit
commit := &github.Commit{
Message: github.Ptr("Initial commit"),
Tree: &github.Tree{SHA: github.Ptr("tree-sha")},
Parents: []*github.Commit{
{SHA: github.Ptr("parent-commit-sha")},
},
}
opts := &github.CreateCommitOptions{
Author: &github.CommitAuthor{
Name: github.Ptr("John Doe"),
Email: github.Ptr("john@example.com"),
Date: &github.Timestamp{time.Now()},
},
}
createdCommit, _, err := client.Git.CreateCommit(ctx, "owner", "repo", commit, opts)// GetRef fetches a single reference
func (s *GitService) GetRef(ctx context.Context, owner, repo, ref string) (*Reference, *Response, error)
// ListMatchingRefs lists references matching a pattern
func (s *GitService) ListMatchingRefs(ctx context.Context, owner, repo string, opts *ReferenceListOptions) ([]*Reference, *Response, error)
// CreateRef creates a new reference
func (s *GitService) CreateRef(ctx context.Context, owner, repo string, ref *Reference) (*Reference, *Response, error)
// UpdateRef updates an existing reference
func (s *GitService) UpdateRef(ctx context.Context, owner, repo string, ref *Reference, force bool) (*Reference, *Response, error)
// DeleteRef deletes a reference
func (s *GitService) DeleteRef(ctx context.Context, owner, repo, ref string) (*Response, error)
type Reference struct {
Ref *string
URL *string
Object *GitObject
NodeID *string
}
type GitObject struct {
Type *string
SHA *string
URL *string
}Usage:
// Get a reference
ref, _, err := client.Git.GetRef(ctx, "owner", "repo", "refs/heads/main")
// Create a new branch (reference)
newRef := &github.Reference{
Ref: github.Ptr("refs/heads/new-branch"),
Object: &github.GitObject{
SHA: github.Ptr("commit-sha"),
},
}
ref, _, err := client.Git.CreateRef(ctx, "owner", "repo", newRef)
// Update a reference (force push)
updateRef := &github.Reference{
Ref: github.Ptr("refs/heads/main"),
Object: &github.GitObject{
SHA: github.Ptr("new-commit-sha"),
},
}
ref, _, err := client.Git.UpdateRef(ctx, "owner", "repo", updateRef, true)
// Delete a branch (reference)
_, err := client.Git.DeleteRef(ctx, "owner", "repo", "refs/heads/old-branch")// GetTag fetches a tag
func (s *GitService) GetTag(ctx context.Context, owner, repo, sha string) (*Tag, *Response, error)
// CreateTag creates a tag object
func (s *GitService) CreateTag(ctx context.Context, owner, repo string, tag *Tag) (*Tag, *Response, error)
type Tag struct {
Tag *string
SHA *string
URL *string
Message *string
Tagger *CommitAuthor
Object *GitObject
Verification *SignatureVerification
NodeID *string
}Usage:
// Create an annotated tag
tag := &github.Tag{
Tag: github.Ptr("v1.0.0"),
Message: github.Ptr("Release version 1.0.0"),
Object: &github.GitObject{
Type: github.Ptr("commit"),
SHA: github.Ptr("commit-sha"),
},
Tagger: &github.CommitAuthor{
Name: github.Ptr("John Doe"),
Email: github.Ptr("john@example.com"),
Date: &github.Timestamp{time.Now()},
},
}
createdTag, _, err := client.Git.CreateTag(ctx, "owner", "repo", tag)
// Create reference for the tag
tagRef := &github.Reference{
Ref: github.Ptr("refs/tags/v1.0.0"),
Object: &github.GitObject{
SHA: createdTag.SHA,
},
}
_, _, err = client.Git.CreateRef(ctx, "owner", "repo", tagRef)// GetTree fetches a tree
func (s *GitService) GetTree(ctx context.Context, owner, repo, sha string, recursive bool) (*Tree, *Response, error)
// CreateTree creates a new tree object
func (s *GitService) CreateTree(ctx context.Context, owner, repo string, baseTree string, entries []*TreeEntry) (*Tree, *Response, error)
type Tree struct {
SHA *string
Entries []*TreeEntry
Truncated *bool
}
type TreeEntry struct {
SHA *string
Path *string
Mode *string
Type *string
Size *int
Content *string
URL *string
}Usage:
// Get a tree
tree, _, err := client.Git.GetTree(ctx, "owner", "repo", "tree-sha", false)
// Get a tree recursively (includes all nested trees)
tree, _, err := client.Git.GetTree(ctx, "owner", "repo", "tree-sha", true)
// Create a new tree
entries := []*github.TreeEntry{
{
Path: github.Ptr("README.md"),
Mode: github.Ptr("100644"), // File mode
Type: github.Ptr("blob"),
Content: github.Ptr("# My Project\n\nProject description"),
},
{
Path: github.Ptr("src/main.go"),
Mode: github.Ptr("100644"),
Type: github.Ptr("blob"),
SHA: github.Ptr("blob-sha"), // Reference existing blob
},
}
tree, _, err := client.Git.CreateTree(ctx, "owner", "repo", "base-tree-sha", entries)Creating a commit with the Git Data API:
// 1. Get the reference (branch) you want to update
ref, _, err := client.Git.GetRef(ctx, "owner", "repo", "refs/heads/main")
if err != nil {
return err
}
// 2. Get the commit object that the reference points to
commit, _, err := client.Git.GetCommit(ctx, "owner", "repo", *ref.Object.SHA)
if err != nil {
return err
}
// 3. Create a new blob for your file content
blob := &github.Blob{
Content: github.Ptr("New file content"),
Encoding: github.Ptr("utf-8"),
}
newBlob, _, err := client.Git.CreateBlob(ctx, "owner", "repo", blob)
if err != nil {
return err
}
// 4. Create a new tree with your changes
entries := []*github.TreeEntry{
{
Path: github.Ptr("newfile.txt"),
Mode: github.Ptr("100644"),
Type: github.Ptr("blob"),
SHA: newBlob.SHA,
},
}
newTree, _, err := client.Git.CreateTree(
ctx, "owner", "repo", *commit.Tree.SHA, entries)
if err != nil {
return err
}
// 5. Create a new commit
newCommit := &github.Commit{
Message: github.Ptr("Add new file"),
Tree: &github.Tree{SHA: newTree.SHA},
Parents: []*github.Commit{{SHA: commit.SHA}},
}
createdCommit, _, err := client.Git.CreateCommit(ctx, "owner", "repo", newCommit, nil)
if err != nil {
return err
}
// 6. Update the reference to point to the new commit
updateRef := &github.Reference{
Ref: github.Ptr("refs/heads/main"),
Object: &github.GitObject{
SHA: createdCommit.SHA,
},
}
_, _, err = client.Git.UpdateRef(ctx, "owner", "repo", updateRef, false)
if err != nil {
return err
}