简体   繁体   中英

Git2Go Fetch or Hard Pull

I have a repository, however in some situations the files locally may get changed. I want to do a git pull and over write the changes with git2go. In normal git - ie on the terminal I could do

git fetch --all
git reset --hard origin/master

I am therefore trying to do the same using git2go

func Pull(source string, dest string) error {
    repo, err:= git.OpenRepository(dest)
    remote, err:= repo.LookupRemote("origin")
    log.Println(repo) //print repo address

    cbs := &git.RemoteCallbacks{
        CredentialsCallback:      credentialsCallback,
        CertificateCheckCallback: certificateCheckCallback,
    }

    err = remote.SetCallbacks(cbs)
    err = remote.Fetch([]string{}, "")
    log.Println("fetch error: ", err) //print fetch error
    remote_master, err := repo.LookupReference("refs/remotes/origin/master")
    mergeRemoteHead, err := repo.AnnotatedCommitFromRef(remote_master)
    mergeHeads := make([]*git.AnnotatedCommit, 1)
    mergeHeads[0] = mergeRemoteHead
    err = repo.Merge(mergeHeads, nil, nil)
    log.Println("err: ", err)
    repo.StateCleanup() //print merge error
    return err
}

It compiles fine, and seems to run - it logs the error to be however the overwritten files don't change to what they are remotely. I feel I am missing one key element...../

The output I get is:

2015/05/19 20:40:15 GitHandler.go:44: &{0x7f46cc000a40}
2015/05/19 20:40:22 GitHandler.go:53: fetch error:  <nil>
2015/05/19 20:40:22 GitHandler.go:59: err:  <nil>

The git commands you want to replace and the code you're writing in go server very different purposes. A hard reset cannot be simulated in any way by a merge.

If you do want to merge, you should check the resulting index for conflicts and resolve them if necessary. You then need to create the merge commit and update the current branch.

If what you want is to do the steps of a hard reset, then you need to write those out, perform a checkout of your target tree, read the target tree into the index and write that out, and then update the current branch to the target commit.

These are very different approaches to updates, one of them preserves local commits, the other one doesn't. One creates history, the other one doesn't. I would recomment first figuring out which form of update you wish to perform.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM