如何使用git2go实现以下目标。

$ git checkout -b feature_branch_name
... edit files, add and commit ...
$ git push -u origin feature_branch_name

我被困在这里:
branch, err = repo.CreateBranch("test", headCommit, false,
    signature, "Test branch that I was to push immediately")
if err != nil {
    panic(err)
}

更新

我现在有以下内容,它创建分支并指向正确的分支,但是像git checkout一样,我无法获取它来更新工作目录:
head, err := repository.Head()
if err != nil {
    return err
}

headCommit, err := repository.LookupCommit(head.Target())
if err != nil {
    return err
}

_, err = cs.repository.CreateBranch(name, headCommit, false)
if err != nil {
    return err
}

_, err = cs.repository.References.CreateSymbolic("HEAD","refs/heads/"+name, true, "headOne")
if err != nil {
    return err
}

opts := &git.CheckoutOpts{
    Strategy: git.CheckoutSafe | git.CheckoutRecreateMissing,
}
if err := repository.CheckoutHead(opts); err != nil {
    return err
}

我想我现在正在使用结帐选项

我仍然在 push 部分。

最佳答案

我终于让它工作了。如果有人想知道同一件事,这是代码:

git checkout

head, err := repository.Head()
if err != nil {
    return err
}

headCommit, err := repository.LookupCommit(head.Target())
if err != nil {
    return err
}

_, err = cs.repository.CreateBranch(name, headCommit, false)
if err != nil {
    return err
}

_, err = cs.repository.References.CreateSymbolic("HEAD","refs/heads/"+name, true, "headOne")
if err != nil {
    return err
}

opts := &git.CheckoutOpts{
    Strategy: git.CheckoutSafe | git.CheckoutRecreateMissing,
}
if err := repository.CheckoutHead(opts); err != nil {
    return err
}

git push
// Get remote
remote, err := repo.Remotes.Lookup("origin")
if err != nil {
    remote, err = repo.Remotes.Create("origin", repo.Path())
    if err != nil {
        return err
    }
}

// Get the branch
branch, err := repo.Branch()
if err != nil {
    return err
}

// Get the name
branchName, err := branch.Name()
if err != nil {
    return err
}

if err := remote.Push([]string{"refs/heads/"+branchName}, &git.PushOptions{}); err != nil {
    return err
}

return nil

根据您使用的git2go版本,这可能也会有所帮助:http://blog.gopheracademy.com/advent-2014/git2go-tutorial/(很棒的文章)

从那以后,情况发生了很大变化。

谢谢

关于git - Git2go : Simulate git checkout and an immediate git push,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31496175/

10-11 03:49