gitpython and git diff

后端 未结 6 1398
终归单人心
终归单人心 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:45

    If you want to do git diff on a file between two commits this is the way to do it:

    import git
    
    repo = git.Repo()
    path_to_a_file = "diff_this_file_across_commits.txt"
    
    commits_touching_path = list(repo.iter_commits(paths=path))
    
    print repo.git.diff(commits_touching_path[0], commits_touching_path[1], path_to_a_file)
    

    This will show you the differences between two latest commits that were done to the file you specify.

    Hope this helped.

提交回复
热议问题