Git 中文文档 Git 中文文档
指南
GitHub (opens new window)
指南
GitHub (opens new window)
  • 起步

    • 1.1 关于版本控制
    • 1.2 git 简史
    • 1.3 git 是什么
    • 1.4 命令行
    • 1.5 安装 git
    • 1.6 初次运行git前的配置
    • 1.7 获取帮助
    • 1.8 起步 - 总结
  • git 基础

  • git 分支

  • 服务器上的 git

  • 分布式 git

  • github

  • git 工具

  • 自定义 git

  • git 与其他系统

  • git 内部原理

Build Status

go-gitis 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 Keybase, Gitea or Pulumi, and by many other libraries and tools.

Project Status


After the legal issues with the src-d organization, the lack of update for four months and the requirement to make a hard fork, the project is now back to normality.

The project is currently actively maintained by individual contributors, including several of the original authors, but also backed by a new company, gitsight, where go-git is a critical component used at scale.

Comparison with git


go-gitaims to be fully compatible with git, all the porcelainoperations are implemented to work exactly as gitdoes.

gitis a humongous project with years of development by thousands of contributors, making it challenging for go-gitto implement all the features. You can find a comparison of go-gitvs gitin the compatibility documentation.

Installation


The recommended way to install go-gitis:

  1. ``` go
  2. import "github.com/go-git/go-git/v5" // with go modules enabled (GO111MODULE=on or outside GOPATH)
  3. import "github.com/go-git/go-git" // with go modules disabled
  4. ```

Examples


Please note that the CheckIfError and Info 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

  1. ``` go
  2. // Clone the given repository to the given directory
  3. Info("git clone https://github.com/go-git/go-git")

  4. _, err := git.PlainClone("/tmp/foo", false, &git.CloneOptions{
  5.     URL:      "https://github.com/go-git/go-git",
  6.     Progress: os.Stdout,
  7. })

  8. CheckIfError(err)
  9. ```

Outputs:

  1. ``` sh
  2. Counting objects: 4924, done.
  3. Compressing objects: 100% (1333/1333), done.
  4. Total 4924 (delta 530), reused 6 (delta 6), pack-reused 3533

  5. ```

In-memory example


Cloning a repository into memory and printing the history of HEAD, just like git log does

  1. ``` go
  2. // Clones the given repository in memory, creating the remote, the local
  3. // branches and fetching the objects, exactly as:
  4. Info("git clone https://github.com/go-git/go-billy")

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

  8. CheckIfError(err)

  9. // Gets the HEAD history from HEAD, just like this command:
  10. Info("git log")

  11. // ... retrieves the branch pointed by HEAD
  12. ref, err := r.Head()
  13. CheckIfError(err)

  14. // ... retrieves the commit history
  15. cIter, err := r.Log(&git.LogOptions{From: ref.Hash()})
  16. CheckIfError(err)

  17. // ... just iterates over the commits, printing it
  18. err = cIter.ForEach(func(c *object.Commit) error {
  19. fmt.Println(c)
  20. return nil
  21. })
  22. CheckIfError(err)
  23. ```

Outputs:

  1. ``` sh
  2. commit ded8054fd0c3994453e9c8aacaf48d118d42991e
  3. Author: Santiago M. Mola <santi@mola.io>
  4. Date:   Sat Nov 12 21:18:41 2016 +0100

  5.     index: ReadFrom/WriteTo returns IndexReadError/IndexWriteError. (#9)

  6. commit df707095626f384ce2dc1a83b30f9a21d69b9dfc
  7. Author: Santiago M. Mola <santi@mola.io>
  8. Date:   Fri Nov 11 13:23:22 2016 +0100

  9.     readwriter: fix bug when writing index. (#10)

  10.     When using ReadWriter on an existing siva file, absolute offset for
  11.     index entries was not being calculated correctly.
  12. ...

  13. ```

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
Last Updated: 2023-09-03 19:17:54