gogo-git

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?


Solution

  • 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)