Pradeep
Pradeep

Reputation: 1398

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

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?

Upvotes: 1

Views: 1416

Answers (1)

blami
blami

Reputation: 7431

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)

Upvotes: 5

Related Questions