[Python] Checking out a branch with GitPython. making commits, and then switching back to the...

Discussão em 'Python' iniciado por Stack, Fevereiro 4, 2025.

  1. Stack

    Stack Membro Participativo

    I'm using the GitPython library to do some simple Git manipulation and I'd like to checkout a branch, make a commit, and then checkout the previous branch. The docs are a little confusing on how to do this. So far, I have this:

    import git
    repo = git.Repo()
    previous_branch = repo.active_branch
    new_branch_name = "foo"
    new_branch = repo.create_head(new_branch_name)
    new_branch.checkout()
    repo.index.commit("my commit message") # this seems wrong
    ## ?????


    I can tell that this works by verifying it via git commands, but I get the feeling that I'm doing this incorrectly. I'm not how to switch back to the previous branch safely sort of using the raw git commands (from within the library directly).

    Continue reading...

Compartilhe esta Página