Gitpython: Create a new branch and push to remote?

Created on 13 Jun 2016  路  9Comments  路  Source: gitpython-developers/GitPython

Hi,

i really tried reading all over the docs, and tutorial but for some reason i can't find it.

given repo - how can i create a new local branch and push it to remote?

i must say even i really like the API i think a simple demonstration of using the git will help new-comer a lot, after ill finish up my project ill be happy to write some how-to for absolute beginner.

thanks.

wont fix

Most helpful comment

If it's an existing repo, you shouldn't need to create a new remote (there's no indication in the question that an existing repo needs to be connected to a new remote). Using a Remote object via the existing origin is probably the easiest way to push.

Also, worth noting that if you're just creating a new branch and not making any changes to it, there's no need to check it out before pushing:

origin = repo.remote()
repo.create_head('my_new_branch')
origin.push('my_new_branch')

All 9 comments

If you look into the tutorial and search for create_head, you will find the method you seem to be looking for. You might also have a look at the Remote type (or search for push in the tutorial) to see how to interact with remote refs.

You will find that all types are fully documented, in case you need more than is provided in the tutorial.
And of course, if you find the current documentation lacking in any respect, I'd be happy to merge a PR with a fix.

create_head doesn't work for windows

folder = os.path.abspath(folder)
repo = git.Repo.init(folder)
origin = repo.create_remote('origin', repository)
origin.fetch()
repo.create_head('master', origin.refs.master)

the example above is from tutorial, and here is an exeption a get in the last line:

AttributeError: 'IterableList' object has no attribute 'origin/master'

Hi @Byron ,

If you look into the tutorial and search for create_head, you will find the method you seem to be looking for. You might also have a look at the Remote type (or search for push in the tutorial) to see how to interact with remote refs.
You will find that all types are fully documented, in case you need more than is provided in the tutorial.
And of course, if you find the current documentation lacking in any respect, I'd be happy to merge a PR with a fix.

I have been looking and I have not found anything for creating a new local branch and push it to the remote, where the remote branch did not yet exist. (and do it cleanly). This would be an equivalent to a --set-upstream during a push or a --set-upstream-to on a branch.

All examples do an initial fetch on an origin, which already contains the remote branch, and a local branch is created from the remote one.

I would love to provide a PR to improve the doc, but I have not found how to do it.

Could you help?

Here is one solution I found:

my_new_branch = repo.create_head('my_new_branch')
my_new_branch.checkout()
origin = repo.create_remote('origin', my_remote_repo_url)
repo.git.push("--set-upstream", origin, repo.head.ref)

Correct? Any other ways?

@esciara I believe that would be the way to do it, even though it means using the git command-line. A local branch setup to track a remote one is identified by an entry in the repository's config file. This can be done manually as well, but it seems easier to add an argument as shown above.

To my mind, this would be a nice addition to the documentation.

If it's an existing repo, you shouldn't need to create a new remote (there's no indication in the question that an existing repo needs to be connected to a new remote). Using a Remote object via the existing origin is probably the easiest way to push.

Also, worth noting that if you're just creating a new branch and not making any changes to it, there's no need to check it out before pushing:

origin = repo.remote()
repo.create_head('my_new_branch')
origin.push('my_new_branch')

Because I've read through this thread multiple times and was confused each time, here is the code I ended up using:

from pathlib import Path

branch_name = "test1"

repo_path = Path("~/git/sandboxes/git-sandbox").expanduser()
repo = git.Repo(repo_path)
origin = repo.remote(name="origin")

repo.head.reference = repo.create_head(branch_name)
repo.head.reference.set_tracking_branch(origin.refs.master).checkout()

#  do stuff to files and add them using repo.index.add

push_res = origin.push(branch_name)[0]
print(push_res.summary)

I was trying to do this too and finding it difficult (or I should say lack of examples).

I think this is the way to do it, just need to create a new RemoteReference.

from git import Repo, RemoteReference

branch_name = "new_branch"
remote_name = "origin"

# Get origin remote
origin = repo.remote(remote_name)

# Create new branch
repo.head.reference = repo.create_head(branch_name)

# Create new remote ref and set it to track.
rem_ref = RemoteReference(repo, f"refs/remotes/{remote_name}/{branch_name}")
repo.head.reference.set_tracking_branch(rem_ref)

I was trying to do this too and finding it difficult (or I should say lack of examples).

I think this is the way to do it, just need to create a new RemoteReference.

from git import Repo, RemoteReference

branch_name = "new_branch"
remote_name = "origin"

# Get origin remote
origin = repo.remote(remote_name)

# Create new branch
repo.head.reference = repo.create_head(branch_name)

# Create new remote ref and set it to track.
rem_ref = RemoteReference(repo, f"refs/remotes/{remote_name}/{branch_name}")
repo.head.reference.set_tracking_branch(rem_ref)

Thank you, this was most elegant and readable for me.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tucked picture tucked  路  5Comments

cool-RR picture cool-RR  路  4Comments

crazyhouse33 picture crazyhouse33  路  3Comments

NiQ1 picture NiQ1  路  4Comments

catskul picture catskul  路  5Comments