Search code examples
pythongitgitpython

GitPython equivalent to git diff HEAD


I have:

  • 5 modified but unstaged files
  • 2 modified and staged files
  • thus 7 modified files.

I know the following GitPython equivalents to git commands:

  • repo.index.diff(None) gives the same result as git diff - 5 modified but unstaged files
  • repo.index.diff('HEAD') surprisingly gives the same result as git diff --staged - 2 modified and staged files

Thus my question is, what is GitPython equivalent to git diff HEAD?

P.S. I can merge results of repo.index.diff(None) and repo.index.diff('HEAD') to get the desired output but it looks quite stupid...


Solution

  • The following worked for me:

    repo = git.Repo('path/to/my/repo')
    print(repo.git.diff(repo.commit()))
    

    This showed the diff for both staged and unstaged modifications.