Search code examples
gitgitpython

How to list all changed files between two tags in GitPython


I could use Git command git log --oneline tag1 tag2 to list all commits between two tags. Then use git show --pretty="" --name-only commitId to list the changed files in one commit.
How to achieve this using GitPython?


Solution

  • If you know a git command, you can use git directly in GitPython. Taking git show --pretty="" --name-only HEAD for example:

    from git import Repo
    
    # Suppose the current path is the root of the repository
    r = Repo('.')
    o = r.git.show('HEAD', pretty="", name_only=True)
    print(o)
    
    • Arguments like HEAD remain unchanged as non-keyword arguments.
    • Arguments like --pretty="" are converted to keyword arguments by removing the leading --.
    • Flags like --name-only are converted to keyword arguments by removing the leading -- and being assigned the value True.
    • The - in the keys and commands are converted to _. name-only should be name_only. git for-each-ref should be git.for_each_ref.
    • The non-keyword arguments come before the keyword arguments.

    The command git log --oneline tag1 tag2 does not list commits between the two tags. It lists commits reachable from any of the two tags.