Search code examples
pythongitgitpython

how to do a git diff of current commit with last commit using gitpython?


I am trying to grasp gitpython module,

hcommit = repo.head.commit
tdiff = hcommit.diff('HEAD~1')

but tdiff = hcommit.diff('HEAD^ HEAD') doesn't work !! neither does ('HEAD~ HEAD').,

I am trying to get the diff output !


Solution

  • I figured out how to get the git diff using gitPython.

    import git
    repo = git.Repo("path/of/repo/")
    
    # the below gives us all commits
    repo.commits()
    
    # take the first and last commit
    
    a_commit = repo.commits()[0]
    b_commit = repo.commits()[1]
    
    # now get the diff
    repo.diff(a_commit,b_commit)
    

    Voila !!!