Using GitPython module to get remote HEAD branch
Asked Answered
T

3

9

I'm trying to use GitPython to write some Python scripts which I can use it to simplify my daily tasks as I manage many branches.

I'm also quite new for Python when it comes to writing complicated scripts.

This is the API I used: GitPython API doc

I want to write it in GitPython that simply does the following and parse out the part which shows me the HEAD remote branch is pointing. In another word, I want to basically get the remotes/origin/HEAD

$ git branch -a
  master
* branch_to_remove
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
  remotes/origin/testing

I browse the API doc many times, at first I'm having trouble to understand the Python format of these API docs, and I couldn't find anything useful to use to do for this other than remote_head in class git.refs.reference.Reference(repo, path, check_path=True)

But I don't even know how to call/initialize it.

Here is what I had so far, and you can tell what I'm trying to do, to simply reset to 'no branch' state and remove the current branch I'm on:

import git
from git import *
repo = git.Repo("/some/path/testing")
repo.git.branch()
[some code to get the remotes/origin/HEAD, set it to remoteHeadBranch ]
repo.git.checkout(remoteHeadBranch)  # this should reset the Git back to 'no branch' state
repo.git.checkout(D="branch_to_remove")

Any help is much appreciated!

Thanks.

Trunnel answered 12/6, 2012 at 6:41 Comment(0)
S
4

I just saw your question I was wondering about this gitPython, looks like really nice tool, and I was looking for this specific question in the GitPython documentation with no lucky but if you search on github you will see a lot of tests there and there's a test for that.

You will see something like this when you search for "remove new branch":

# remove new branch
Head.delete(new_remote_branch.repo, new_remote_branch)

GitPython Reference

Syne answered 12/6, 2012 at 7:24 Comment(2)
Thanks for your comment! I will try this out! Can someone please help me a bit when reading these Python API docs? I meant the doc is very different and hard for me to read for a Java guy like me :-( Thanks!Trunnel
This will NOT work because you cannot delete the branch you are currently on, so the first step is always return back to the 'no branch' state which what I'm trying to do. Error on switching to 'no' branch state. 'git branch -d newMaster' returned exit status 1: error: Cannot delete the branch 'newMaster' which you are currently on.Trunnel
Q
3

To print the current branch:

print(repo.head.ref)

To list branches

print [str(b) for b in repo.heads]

To checkout a branch

repo.heads[branch].checkout()

or repo.git.checkout(branch)

If you're trying to delete a branch, You need to be in a different LOCAL branch, which you can do in several ways

repo.heads['master'].checkout()

or repo.git.checkout('master') or repo.git.checkout('remotes/origin/master')

Hope this helps

Quinquereme answered 17/4, 2015 at 3:29 Comment(1)
No, this has nothing to do with the question. The asker is specifically asking about the HEAD of a remote.Estep
M
1

next(ref for ref in repo.remotes.origin.refs if ref.name == "origin/HEAD").ref.name

repo.remotes.origin.refs will give you a list of the remote branches. Filter on the one that is the HEAD and check towards what it points(ref<.name>).

You are now left with something like "origin/master" or "origin/main".

Muncy answered 30/9, 2021 at 7:42 Comment(1)
For me [ref for ref in repo.remotes.origin.refs if ref.name == "origin/HEAD"] gives an empty list. I use git version 2.36.2.Mango

© 2022 - 2024 — McMap. All rights reserved.