Top Related Projects
Git with a cup of tea! Painless self-hosted all-in-one software development service, including Git hosting, code review, team collaboration, package registry and CI/CD
Git to Go; bindings for libgit2. Like McDonald's but tastier.
Go library for accessing the GitHub v3 API
A command-line tool that makes git easier to use with GitHub.
Quick Overview
go-git is a highly extensible, pure Go implementation of Git. It provides a comprehensive set of features for working with Git repositories, including reading, writing, and manipulating Git objects, without requiring external Git binaries.
Pros
- Pure Go implementation, making it easy to embed in Go applications
- Extensive feature set covering most Git operations
- Cross-platform compatibility
- Active development and community support
Cons
- Performance may be slower compared to native Git in some scenarios
- Some advanced Git features might not be fully implemented
- Learning curve for developers used to command-line Git
Code Examples
- Cloning a repository:
import "github.com/go-git/go-git/v5"
r, err := git.PlainClone("/path/to/repo", false, &git.CloneOptions{
URL: "https://github.com/go-git/go-git",
})
- Adding and committing changes:
w, err := r.Worktree()
_, err = w.Add(".")
commit, err := w.Commit("Add new files", &git.CommitOptions{
Author: &object.Signature{
Name: "John Doe",
Email: "john@example.com",
When: time.Now(),
},
})
- Listing branches:
branches, err := r.Branches()
err = branches.ForEach(func(b *plumbing.Reference) error {
fmt.Println(b.Name())
return nil
})
Getting Started
To start using go-git in your Go project:
-
Install the package:
go get github.com/go-git/go-git/v5
-
Import the package in your Go code:
import "github.com/go-git/go-git/v5"
-
Use go-git functions to interact with Git repositories:
r, err := git.PlainOpen("/path/to/repo") if err != nil { log.Fatal(err) } // Perform Git operations using the repository object
For more detailed usage and examples, refer to the official documentation and examples in the repository.
Competitor Comparisons
Git with a cup of tea! Painless self-hosted all-in-one software development service, including Git hosting, code review, team collaboration, package registry and CI/CD
Pros of Gitea
- Full-featured Git hosting solution with web interface, user management, and more
- Supports multiple databases (SQLite, MySQL, PostgreSQL)
- Lightweight and easy to deploy, suitable for self-hosting
Cons of Gitea
- Larger codebase and more complex setup compared to go-git
- Requires more system resources to run
- May be overkill for projects that only need Git operations without a web interface
Code Comparison
go-git (basic cloning):
r, err := git.PlainClone("/path/to/repo", false, &git.CloneOptions{
URL: "https://github.com/src-d/go-git",
})
Gitea (creating a repository):
repo, err := models.CreateRepository(user, models.CreateRepoOptions{
Name: "my-repo",
Description: "A new repository",
Private: true,
})
Summary
go-git is a pure Go implementation of Git, focusing on providing Git functionality as a library. Gitea, on the other hand, is a complete Git hosting solution with a web interface and additional features. Choose go-git for embedding Git operations in Go applications, and Gitea for setting up a full-featured Git server with a web interface.
Git to Go; bindings for libgit2. Like McDonald's but tastier.
Pros of git2go
- Higher performance due to C bindings to libgit2
- More comprehensive Git functionality coverage
- Better support for low-level Git operations
Cons of git2go
- Requires CGo, which can complicate cross-compilation
- Steeper learning curve due to lower-level API
- Less idiomatic Go code compared to go-git
Code Comparison
git2go example:
repo, err := git.OpenRepository("path/to/repo")
if err != nil {
log.Fatal(err)
}
defer repo.Free()
go-git example:
repo, err := git.PlainOpen("path/to/repo")
if err != nil {
log.Fatal(err)
}
Both git2go and go-git are popular Git libraries for Go, but they have different approaches. git2go provides bindings to libgit2, offering high performance and extensive Git functionality. However, it requires CGo and has a steeper learning curve. go-git is a pure Go implementation, making it easier to use and cross-compile, but it may not cover all Git features and could be slower for some operations. The choice between them depends on specific project requirements, performance needs, and developer preferences.
Go library for accessing the GitHub v3 API
Pros of go-github
- Comprehensive coverage of GitHub API endpoints
- Well-maintained with frequent updates and active community support
- Extensive documentation and examples
Cons of go-github
- Limited to GitHub-specific functionality
- Requires authentication for most operations
Code comparison
go-github:
client := github.NewClient(nil)
repo, _, err := client.Repositories.Get(context.Background(), "google", "go-github")
go-git:
r, err := git.PlainClone("/path/to/repo", false, &git.CloneOptions{
URL: "https://github.com/src-d/go-git",
})
Key differences
- go-github focuses on interacting with GitHub's API, while go-git is a Git implementation
- go-git allows local Git operations without network access, whereas go-github requires internet connectivity
- go-github is specific to GitHub, while go-git can work with any Git repository
Use cases
- Use go-github for GitHub-specific tasks like managing issues, pull requests, and repository settings
- Choose go-git for Git operations like cloning, committing, and branching, regardless of the hosting platform
A command-line tool that makes git easier to use with GitHub.
Pros of hub
- Provides a command-line interface for GitHub, extending git functionality
- Offers features like creating pull requests and managing issues directly from the terminal
- Simplifies common GitHub workflows, improving developer productivity
Cons of hub
- Limited to GitHub-specific operations, not a general-purpose git library
- Requires separate installation and may not integrate as seamlessly with existing git workflows
- Less flexibility for custom git operations or non-GitHub remote repositories
Code comparison
hub:
def create_pull_request(base = nil, head = nil)
base ||= master_branch
head ||= current_branch
title, body = pulls_message(base)
# ... (additional code to create PR)
end
go-git:
func (r *Repository) CreateRemote(config *config.RemoteConfig) (*Remote, error) {
if err := config.Validate(); err != nil {
return nil, err
}
return newRemote(r, config), nil
}
The hub code snippet shows a method for creating pull requests, highlighting its GitHub-specific functionality. In contrast, the go-git code demonstrates a more general-purpose git operation for creating remotes, showcasing its flexibility as a git library.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
WE CONTINUE THE DEVELOPMENT AT go-git/go-git. This repository is abandoned, and no further updates will be done on the code base, nor issue/prs will be answered or attended.
go-git is a highly extensible git implementation library written in pure Go.
It can be used to manipulate git repositories at low level (plumbing) or high level (porcelain), through an idiomatic Go API. It also supports several types of storage, such as in-memory filesystems, or custom implementations thanks to the Storer
interface.
It's being actively developed since 2015 and is being used extensively by source{d} and Keybase, and by many other libraries and tools.
Comparison with git
go-git aims to be fully compatible with git, all the porcelain operations are implemented to work exactly as git does.
git is a humongous project with years of development by thousands of contributors, making it challenging for go-git to implement all the features. You can find a comparison of go-git vs git in the compatibility documentation.
Installation
The recommended way to install go-git is:
go get -u gopkg.in/src-d/go-git.v4/...
We use gopkg.in to version the API, this means that when
go get
clones the package, it's the latest tag matchingv4.*
that is cloned and not the master branch.
Examples
Please note that the
CheckIfError
andInfo
functions used in the examples are from the examples package just to be used in the examples.
Basic example
A basic example that mimics the standard git clone
command
// Clone the given repository to the given directory
Info("git clone https://github.com/src-d/go-git")
_, err := git.PlainClone("/tmp/foo", false, &git.CloneOptions{
URL: "https://github.com/src-d/go-git",
Progress: os.Stdout,
})
CheckIfError(err)
Outputs:
Counting objects: 4924, done.
Compressing objects: 100% (1333/1333), done.
Total 4924 (delta 530), reused 6 (delta 6), pack-reused 3533
In-memory example
Cloning a repository into memory and printing the history of HEAD, just like git log
does
// Clones the given repository in memory, creating the remote, the local
// branches and fetching the objects, exactly as:
Info("git clone https://github.com/src-d/go-siva")
r, err := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
URL: "https://github.com/src-d/go-siva",
})
CheckIfError(err)
// Gets the HEAD history from HEAD, just like this command:
Info("git log")
// ... retrieves the branch pointed by HEAD
ref, err := r.Head()
CheckIfError(err)
// ... retrieves the commit history
cIter, err := r.Log(&git.LogOptions{From: ref.Hash()})
CheckIfError(err)
// ... just iterates over the commits, printing it
err = cIter.ForEach(func(c *object.Commit) error {
fmt.Println(c)
return nil
})
CheckIfError(err)
Outputs:
commit ded8054fd0c3994453e9c8aacaf48d118d42991e
Author: Santiago M. Mola <santi@mola.io>
Date: Sat Nov 12 21:18:41 2016 +0100
index: ReadFrom/WriteTo returns IndexReadError/IndexWriteError. (#9)
commit df707095626f384ce2dc1a83b30f9a21d69b9dfc
Author: Santiago M. Mola <santi@mola.io>
Date: Fri Nov 11 13:23:22 2016 +0100
readwriter: fix bug when writing index. (#10)
When using ReadWriter on an existing siva file, absolute offset for
index entries was not being calculated correctly.
...
You can find this example and many others in the examples folder.
Contribute
Contributions are more than welcome, if you are interested please take a look to our Contributing Guidelines.
License
Apache License Version 2.0, see LICENSE
Top Related Projects
Git with a cup of tea! Painless self-hosted all-in-one software development service, including Git hosting, code review, team collaboration, package registry and CI/CD
Git to Go; bindings for libgit2. Like McDonald's but tastier.
Go library for accessing the GitHub v3 API
A command-line tool that makes git easier to use with GitHub.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot