I'm trying to update a Python script that checks the status of a handful of local repositories against remotes from using subprocess to using GitPython. What is the equivalent command in GitPython for git remote show origin
, or what is the better way to check that the local repo is fast-forwardable or out-of-date (etc.)?
$ git remote show origin
* remote origin
Fetch URL: <url>
Push URL: <url>
HEAD branch: master
Remote branches:
XYZ tracked
master tracked
Local branches configured for 'git pull':
XYZ merges with remote XYZ
master merges with remote master
Local refs configured for 'git push':
XYZ pushes to XYZ (up to date)
master pushes to master (up to date)
The last two lines are my primary concern. It looks like this might be possible with GitPython by iterating over git.Repo.heads
and git.Repo.remotes.origin.refs
and comparing .master.commit
(etc.) hashes. This seems a good deal more work than the above single native git command and will require even more work to tell which side(s) is/are outdated. I was expecting something like git.Repo.remotes.origin.status()
. What is the proper way to determine this in GitPython?
The default name (also known as an alias) for that remote repo is origin. If you've copied a project from Github, it already has an origin. You can view that origin with the command git remote -v, which will list the URL of the remote repo.
The origin Remote When you clone a repository with git clone , it automatically creates a remote connection called origin pointing back to the cloned repository. This is useful for developers creating a local copy of a central repository, since it provides an easy way to pull upstream changes or publish local commits.
git remote set-url origin https://github.com/USERNAME/REPOSITORY.git. you can verify that the remote URL has changed, with command git remote -v . note: "origin" is a convention not part of the command. "origin" is the local name of the remote repository. you can use any name instead of "origin".
remote , in git -speak, refers to any remote repository, such as your GitHub or another git server. origin is the, by convention, default remote name in git . When you do a git clone <url> , <url> is automatically added to your local repo under the name origin .
You can use git.cmd.Git()
if gitpython doesn't wrap wanted functionality. It directly calls git so it's quite convenient, although I guess that mostly it's just a subprocess' wrapper:
import git
g = git.cmd.Git("/path/to/git/repo")
print(g.execute("git remote show origin")) # git remote show origin
print(g.execute(["git", "remote", "show", "origin"])) # same as above
print(g.remote(verbose=True)) # git remote --verbose
I'm not aware of anything better than running git remote show origin
as a subprocess if you need a concise report of every branch. If your interest is in a single branch, assuming you've done a fetch, you can check how many commits you are behind
or ahead like this:
commits_behind = list(repo.iter_commits(
'{branch}..{tracking_branch}'.format(
branch=branch,
tracking_branch=repo.heads[branch].tracking_branch())))
commits_ahead = list(repo.iter_commits(
'{tracking_branch}..{branch}'.format(
branch=branch,
tracking_branch=repo.heads[branch].tracking_branch())))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With