golang git – Is there a way to pull latest from remote branch if the repo is already cloned rather than cloning it again

huangapple go评论79阅读模式
英文:

golang git - Is there a way to pull latest from remote branch if the repo is already cloned rather than cloning it again

问题

我有一个需要每天克隆的存储库,用于获取一些数据。在使用go-git库的golang中,是否有一种方法只克隆一次存储库,并使用git pull更新存储库呢?

英文:

I have a repo which needs to be cloned daily for some data. Is there a way in golang using go-git library to clone the repo only once and update the repo using git pull?

答案1

得分: 2

当然,有一个Worktree.Pull()方法可以实现这个功能。

    // 打开已经克隆的仓库路径
	r, err := git.PlainOpen(path)
	
    // 获取工作目录
	w, err := r.Worktree()
    
    // 从远程仓库拉取
	err = w.Pull(&git.PullOptions{RemoteName: "origin"})

(为了更好的可读性,省略了错误检查)

英文:

Sure, there's Worktree.Pull() method exactly for that.

    // Open already cloned repo in path
	r, err := git.PlainOpen(path)
	
    // Get the working directory
	w, err := r.Worktree()
    
    // Pull from origin
	err = w.Pull(&git.PullOptions{RemoteName: "origin"})

(skipped error checking for better readability)

huangapple
  • 本文由 发表于 2022年2月2日 12:31:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/70950536.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定