Chapters ▾
  1. 1. Getting Started

    1. 1.1 About Version Control
    2. 1.2 A Short History of Git
    3. 1.3 What is Git?
    4. 1.4 The Command Line
    5. 1.5 Installing Git
    6. 1.6 First-Time Git Setup
    7. 1.7 Getting Help
    8. 1.8 Summary
  2. 2. Git Basics

    1. 2.1 Getting a Git Repository
    2. 2.2 Recording Changes to the Repository
    3. 2.3 Viewing the Commit History
    4. 2.4 Undoing Things
    5. 2.5 Working with Remotes
    6. 2.6 Tagging
    7. 2.7 Git Aliases
    8. 2.8 Summary
  3. 3. Git Branching

    1. 3.1 Branches in a Nutshell
    2. 3.2 Basic Branching and Merging
    3. 3.3 Branch Management
    4. 3.4 Branching Workflows
    5. 3.5 Remote Branches
    6. 3.6 Rebasing
    7. 3.7 Summary
  4. 4. Git on the Server

    1. 4.1 The Protocols
    2. 4.2 Getting Git on a Server
    3. 4.3 Generating Your SSH Public Key
    4. 4.4 Setting Up the Server
    5. 4.5 Git Daemon
    6. 4.6 Smart HTTP
    7. 4.7 GitWeb
    8. 4.8 GitLab
    9. 4.9 Third Party Hosted Options
    10. 4.10 Summary
  5. 5. Distributed Git

    1. 5.1 Distributed Workflows
    2. 5.2 Contributing to a Project
    3. 5.3 Maintaining a Project
    4. 5.4 Summary
  1. 6. GitHub

    1. 6.1 Account Setup and Configuration
    2. 6.2 Contributing to a Project
    3. 6.3 Maintaining a Project
    4. 6.4 Managing an organization
    5. 6.5 Scripting GitHub
    6. 6.6 Summary
  2. 7. Git Tools

    1. 7.1 Revision Selection
    2. 7.2 Interactive Staging
    3. 7.3 Stashing and Cleaning
    4. 7.4 Signing Your Work
    5. 7.5 Searching
    6. 7.6 Rewriting History
    7. 7.7 Reset Demystified
    8. 7.8 Advanced Merging
    9. 7.9 Rerere
    10. 7.10 Debugging with Git
    11. 7.11 Submodules
    12. 7.12 Bundling
    13. 7.13 Replace
    14. 7.14 Credential Storage
    15. 7.15 Summary
  3. 8. Customizing Git

    1. 8.1 Git Configuration
    2. 8.2 Git Attributes
    3. 8.3 Git Hooks
    4. 8.4 An Example Git-Enforced Policy
    5. 8.5 Summary
  4. 9. Git and Other Systems

    1. 9.1 Git as a Client
    2. 9.2 Migrating to Git
    3. 9.3 Summary
  5. 10. Git Internals

    1. 10.1 Plumbing and Porcelain
    2. 10.2 Git Objects
    3. 10.3 Git References
    4. 10.4 Packfiles
    5. 10.5 The Refspec
    6. 10.6 Transfer Protocols
    7. 10.7 Maintenance and Data Recovery
    8. 10.8 Environment Variables
    9. 10.9 Summary
2nd Edition

A2.4 Appendix B: Embedding Git in your Applications - go-git

go-git

In case you want to integrate Git into a service written in Golang, there also is a pure Go library implementation. This implementation does not have any native dependencies and thus is not prone to manual memory management errors. It is also transparent for the standard Golang performance analysis tooling like CPU, Memory profilers, race detector, etc.

go-git is focused on extensibility, compatibility and supports most of the plumbing APIs, which is documented at https://github.com/go-git/go-git/blob/master/COMPATIBILITY.md.

Here is a basic example of using Go APIs:

import "github.com/go-git/go-git/v5"
r, err := git.PlainClone("/tmp/foo", false, &git.CloneOptions{
 URL: "https://github.com/go-git/go-git",
 Progress: os.Stdout,
})

As soon as you have a Repository instance, you can access information and perform mutations on it:

// retrieves the branch pointed by HEAD
ref, err := r.Head()
// get the commit object, pointed by ref
commit, err := r.CommitObject(ref.Hash())
// retrieves the commit history
history, err := commit.History()
// iterates over the commits and print each
for _, c := range history {
 fmt.Println(c)
}

Advanced Functionality

go-git has few notable advanced features, one of which is a pluggable storage system, which is similar to Libgit2 backends. The default implementation is in-memory storage, which is very fast.

r, err := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
 URL: "https://github.com/go-git/go-git",
})

Pluggable storage provides many interesting options. For instance, https://github.com/go-git/go-git/tree/master/_examples/storage allows you to store references, objects, and configuration in an Aerospike database.

Another feature is a flexible filesystem abstraction. Using https://pkg.go.dev/github.com/go-git/go-billy/v5?tab=doc#Filesystem it is easy to store all the files in different way i.e by packing all of them to a single archive on disk or by keeping them all in-memory.

Another advanced use-case includes a fine-tunable HTTP client, such as the one found at https://github.com/go-git/go-git/blob/master/_examples/custom_http/main.go.

customClient := &http.Client{
 Transport: &http.Transport{ // accept any certificate (might be useful for testing)
 TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
 },
 Timeout: 15 * time.Second, // 15 second timeout
 CheckRedirect: func(req *http.Request, via []*http.Request) error {
 return http.ErrUseLastResponse // don't follow redirect
 },
}
// Override http(s) default protocol to use our custom client
client.InstallProtocol("https", githttp.NewClient(customClient))
// Clone repository using the new client if the protocol is https://
r, err := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{URL: url})

Further Reading

A full treatment of go-git’s capabilities is outside the scope of this book. If you want more information on go-git, there’s API documentation at https://pkg.go.dev/github.com/go-git/go-git/v5, and a set of usage examples at https://github.com/go-git/go-git/tree/master/_examples.

AltStyle によって変換されたページ (->オリジナル) /