5 Star 11 Fork 7

Gitee 极速下载/go-git

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/src-d/go-git
克隆/下载
main.go 1.65 KB
一键复制 编辑 原始数据 按行查看 历史
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"time"
"gopkg.in/src-d/go-git.v4"
. "gopkg.in/src-d/go-git.v4/_examples"
"gopkg.in/src-d/go-git.v4/plumbing/object"
)
// Basic example of how to commit changes to the current branch to an existent
// repository.
func main() {
CheckArgs("<directory>")
directory := os.Args[1]
// Opens an already existent repository.
r, err := git.PlainOpen(directory)
CheckIfError(err)
w, err := r.Worktree()
CheckIfError(err)
// ... we need a file to commit so let's create a new file inside of the
// worktree of the project using the go standard library.
Info("echo \"hellow world!\" > example-git-file")
filename := filepath.Join(directory, "example-git-file")
err = ioutil.WriteFile(filename, []byte("hello world!"), 0644)
CheckIfError(err)
// Adds the new file to the staging area.
Info("git add example-git-file")
_, err = w.Add("example-git-file")
CheckIfError(err)
// We can verify the current status of the worktree using the method Status.
Info("git status --porcelain")
status, err := w.Status()
CheckIfError(err)
fmt.Println(status)
// Commits the current staging are to the repository, with the new file
// just created. We should provide the object.Signature of Author of the
// commit.
Info("git commit -m \"example go-git commit\"")
commit, err := w.Commit("example go-git commit", &git.CommitOptions{
Author: &object.Signature{
Name: "John Doe",
Email: "john@doe.org",
When: time.Now(),
},
})
CheckIfError(err)
// Prints the current HEAD to verify that all worked well.
Info("git show -s")
obj, err := r.CommitObject(commit)
CheckIfError(err)
fmt.Println(obj)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/mirrors/go-git.git
git@gitee.com:mirrors/go-git.git
mirrors
go-git
go-git
v4.7.0

搜索帮助