pygit2 icon indicating copy to clipboard operation
pygit2 copied to clipboard

Create new branch without a commit?

Open PUYUP opened this issue 5 years ago • 4 comments

I read code here and found each create new branch required a commit.

def create(self, name, commit, force=False):
        return self._repository.create_branch(name, commit, force)

How create new branch without commit?

PUYUP avatar Mar 27 '20 14:03 PUYUP

you can use this code to create a branch without commit

repo = pygit2.Repository(repo_path)
index = repo.index
tree = index.write_tree()
repo.create_reference_direct('refs/heads/branch_name',tree,False)

yuuri avatar Oct 27 '20 08:10 yuuri

Somehow that propose solution is not trivial, isn't there a way to get a commit from an existing branch and use that as the commit parameter to create_branch?

new_branch = repoObj.create_branch(new_branch, repoObj.branches[ws.getRepoTargetBranch(repoName))

I am unclear how to get a Commit object the API, the above give the error that the 2nd param is a branch not a Commit, but can't find how to convert a branch which normally points to a commit to a Commit object, seems something is missing in the API.

hpe-ykoehler avatar May 18 '23 02:05 hpe-ykoehler

I was able to create a branch this way:

commit = repoObj.revparse_single('HEAD') new_branch = repoObj.create_branch(new_branch, commit)

Somehow there should be a default value to "create_branch" which is to take the current commit we are at in the repo...

hpe-ykoehler avatar May 18 '23 02:05 hpe-ykoehler