英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论