How to check out a branch with GitPython
Asked Answered
H

2

39

I have cloned a repository with GitPython, now I would like to checkout a branch and update the local repository's working tree with the contents of that branch. Ideally, I'd also be able to check if the branch exists before doing this. This is what I have so far:

import git

repo_clone_url = "[email protected]:mygithubuser/myrepo.git"
local_repo = "mytestproject"
test_branch = "test-branch"
repo = git.Repo.clone_from(repo_clone_url, local_repo)
# Check out branch test_branch somehow
# write to file in working directory
repo.index.add(["test.txt"])
commit = repo.index.commit("Commit test")

I am not sure what to put in the place of the comments above. The documentation seems to give an example of how to detach the HEAD, but not how to checkout an named branch.

Hochstetler answered 18/12, 2017 at 16:0 Comment(1)
Very useful question. Not sure how the author managed to overlooked this.Mirilla
E
61

If the branch exists:

repo.git.checkout('branchename')

If not:

repo.git.checkout('-b', 'branchename')

Basically, with GitPython, if you know how to do it within command line, but not within the API, just use repo.git.action("your command without leading 'git' and 'action'"), example: git log --reverse => repo.git.log('--reverse')

Elgon answered 18/12, 2017 at 16:13 Comment(5)
Thanks, do you know how to check if a local branch exists?Hochstetler
Yes. I even know two of them! And I surelly can imagine even more with some minutes! But, they are all not that good because all based on the same thing. GitPython only compute and forward git command lines to OS and parse result. The best you can is to try something and expect a git.exc.GitCommandError. If it happen the branch does not exists OR you messed your code.. What to check: repo.git.checkout('branchename') or repo.git.rev_parse('--verify', 'branchname'). It does not make the same git thing, but gives you the exact same result.Elgon
@Elgon I am applying the first command you list, and while I am not getting an error. It does not appear that the repo is switching over the branch? What might I be missing?Jakob
@Elgon wrote up my question here -> #55623324Jakob
I can't say this enough, but thank you. The last part of your answer finally made me understand this package. I'm trying to understand this 2 days straight. Should be the first thing in the GitPython documentation.Chalcography
C
3

The same checkout of a branch in gitpython (as repo.git.checkout(..)) can be achieved by "Advanced Repo Usage" (low level API), like this:

new_branch = repo.create_head("new_branch")
assert repo.active_branch != new_branch  # It's not checked out yet

repo.head.reference = new_branch  # this makes the ckeckout
assert not repo.head.is_detached
Chrysoberyl answered 5/10, 2023 at 12:48 Comment(2)
Thanks. But why would you do this rather than repo.git.checkout('new_branch')?Hochstetler
@AlexSpurling I'm sure there are developers that may have a reason. In this case, I suppose that such a call is much more lightweight, doesn't call sub-process underneath?Chrysoberyl

© 2022 - 2024 — McMap. All rights reserved.