Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

how to pull? #545

Closed
Gluoo opened this issue Aug 12, 2017 · 2 comments
Closed

how to pull? #545

Gluoo opened this issue Aug 12, 2017 · 2 comments
Labels

Comments

@Gluoo
Copy link

Gluoo commented Aug 12, 2017

I want to pull the latest files from git repo, but I don't know how to do this, can anybody tell me? thanks.

I can clone a repo:
rep, cloneErr := git.PlainClone(LOCAL_CLONE_PATH, false, &git.CloneOptions{
URL: GIT_REP_URL,
Progress: os.Stdout,
})

but did not find any API of pull request, just find a fetch API, but not work well

repo, openErr := git.PlainOpen(LOCAL_CLONE_PATH)
pullError := repo.Fetch(&git.FetchOptions{
RemoteName: "origin",
Progress: os.Stdout,
})

after run the fetch API, nothing changed in my local repo.

@orirawlings
Copy link
Contributor

orirawlings commented Aug 14, 2017

Hey @Gluoo,

Repository.Fetch is analogous to git fetch, so it will update references under refs/remotes/, but will leave you local branches and working directory unmodified.

Worktree.Pull is analogous to git pull and will fetch changes from the remote, update references in refs/remotes/, AND merge the changes into your current branch.

Here is an example of how to use the API:

package main

import (
	"fmt"
	"os"

	"gopkg.in/src-d/go-git.v4"
	. "gopkg.in/src-d/go-git.v4/_examples"
)

// Pull changes from a remote repository
func main() {
	CheckArgs("<path>")
	path := os.Args[1]

	// We instance a new repository targeting the given path (the .git folder)
	r, err := git.PlainOpen(path)
	CheckIfError(err)

	// Get the working directory for the repository
	w, err := r.Worktree()
	CheckIfError(err)

	// Pull the latest changes from the origin remote and merge into the current branch
	Info("git pull origin")
	err = w.Pull(&git.PullOptions{RemoteName: "origin"})
	CheckIfError(err)

	// Print the latest commit that was just pulled
	ref, err := r.Head()
	CheckIfError(err)
	commit, err := r.CommitObject(ref.Hash())
	CheckIfError(err)

	fmt.Println(commit)
}

@Gluoo
Copy link
Author

Gluoo commented Aug 15, 2017

@orirawlings thank you very much

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants