Skip to content

git diff <local branch> <remote>/<remote branch> possible using gitpython ? #989

Description

@hcheruku

Hi team,

I looked into remote and diff part of the documentation & API but couldn't find a way to achieve git diff between local and remote repo.

Here's what I am trying to achieve:
check if local repo matches remote repo, and ONLY if remote repo has any/some updates then pull and update the local repo.

from git import Repo
Repo(local_repo_path).remote().pull(branch)

The above code works sucessfully to pull the changes, but I only want to pull only if there's any update in the remote repo. Not sure how to achieve this.

Basically I want git diff <local branch> <remote>/<remote branch> and want to compare before I pull. Do I have to do Repo(repo_path).remotes.origin.fetch() as well ?

Thank you.
Have a great day!

Activity

  1. Byron commented on Feb 25, 2020

    @Byron
    Member

    Hi @hcheruku ,

    if I understand you correctly, you would like to diff a local state against the state in a remote repository, without actually fetching data to local. This is not possible with git.

    However, removing that constraint, you can always fetch the data first, then diff or compare the commit hash the local branch and remote branch points to.

    It would certainly help to understand the problem you are trying to solve. In the mean time, I will close this issue, feel free to keep posting in the comments.

  2. hcheruku commented on Feb 25, 2020

    @hcheruku
    Author

    Hi Byron,

    Thank you so much for replying.
    Sorry, I didn't mean to diff without fetch. haha sorry for the confusion. I know I cannot diff without fetch. I have no problem git fetching using gitpython. Repo(repo_path).remotes.origin.fetch() works fine.

    Just want to know how to do git diff local remote using gitpython.

    Thanks

  3. hcheruku commented on Feb 25, 2020

    @hcheruku
    Author

    nevermind! I did it.
    Here's what I did:

    Repo(repo_path).remotes.origin.fetch()
    diff = str(Repo(repo_path).git.diff('origin/master')).splitlines()
    if len(diff) != 0:
        Repo(repo_path).remote().pull(branch)
    

    It works!
    Thanks for replying
    Have a great day ahead :-)

  4. reopened this on Feb 26, 2021
  5. locked and limited conversation to collaborators on Feb 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions