gitpython and git diff

后端 未结 6 1513
终归单人心
终归单人心 2021-02-12 21:48

I am looking to get only the diff of a file changed from a git repo. Right now, I am using gitpython to actually get the commit objects and the files of git changes, but I want

6条回答
  •  天命终不由人
    2021-02-12 22:35

    You can use GitPython with the git command "diff", just need to use the "tree" object of each commit or the branch for that you want to see the diffs, for example:

    repo = Repo('/git/repository')
    t = repo.head.commit.tree
    repo.git.diff(t)
    

    This will print "all" the diffs for all files included in this commit, so if you want each one you must iterate over them.

    With the actual branch it's:

    repo.git.diff('HEAD~1')
    

    Hope this help, regards.

提交回复
热议问题