Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to create local branch and track remote branch? #100

Closed
hustlibraco opened this issue Jun 18, 2020 · 5 comments
Closed

How to create local branch and track remote branch? #100

hustlibraco opened this issue Jun 18, 2020 · 5 comments
Labels
stale Issues/PRs that are marked for closure due to inactivity

Comments

@hustlibraco
Copy link

Just like git command: git checkout --track origin/dev
My code:

branchName = "dev"
worktree, err := repo.Worktree()
if err != nil {
	log.Printf("git open worktree err: %v\n", err)
	return false
}

branchRefName := plumbing.NewBranchReferenceName(branchName)
err = worktree.Checkout(&git.CheckoutOptions{Branch: branchRefName, Force: true})
if err == plumbing.ErrReferenceNotFound {
	err = worktree.Checkout(&git.CheckoutOptions{Branch: branchRefName, Force: true, Create: true})
}
if err != nil {
	log.Printf("git checkout -b %s err: %v\n", branchName, err)
	return false
}

err = worktree.Pull(&git.PullOptions{ReferenceName: branchRefName, Auth: auth, Force: true})
if err != nil && err != git.NoErrAlreadyUpToDate {
	log.Printf("git pull err: %v\n", err)
	return false
}
log.Printf("sync repo succ: %v\n", projectNameWithSpace)
return true

Now I can create local branch, but it doesn't track with remote. And when I execute worktree.Pull, it report error:non-fast-forward update.

@yeqown
Copy link

yeqown commented Jun 20, 2020

maybe you should try this:

src-d/go-git#601 (comment)

opts := &git.FetchOptions{
	RefSpecs: []config.RefSpec{"refs/*:refs/*"},
}

@hustlibraco
Copy link
Author

maybe you should try this:

src-d/go-git#601 (comment)

opts := &git.FetchOptions{
	RefSpecs: []config.RefSpec{"refs/*:refs/*"},
}

Can you give me a sample code?

@lsyyff23
Copy link

lsyyff23 commented Nov 3, 2020

maybe you should try this:
src-d/go-git#601 (comment)

opts := &git.FetchOptions{
	RefSpecs: []config.RefSpec{"refs/*:refs/*"},
}

Can you give me a sample code?

try this, it works fine for me.
err = worktree.Checkout(&git.CheckoutOptions{Branch: branchRefName, Force: true, Keep: true,})

@shanestarcher-okta
Copy link

@lsyyff23 my understanding is your example only works if git config --global push.default current is set in your git config.

Copy link

github-actions bot commented Nov 3, 2023

To help us keep things tidy and focus on the active tasks, we've introduced a stale bot to spot issues/PRs that haven't had any activity in a while.

This particular issue hasn't had any updates or activity in the past 90 days, so it's been labeled as 'stale'. If it remains inactive for the next 30 days, it'll be automatically closed.

We understand everyone's busy, but if this issue is still important to you, please feel free to add a comment or make an update to keep it active.

Thanks for your understanding and cooperation!

@github-actions github-actions bot added the stale Issues/PRs that are marked for closure due to inactivity label Nov 3, 2023
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Dec 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale Issues/PRs that are marked for closure due to inactivity
Projects
None yet
Development

No branches or pull requests

4 participants