How to get staged files using GitPython? How to get staged files using GitPython? git git

How to get staged files using GitPython?


You are close, use repo.index.diff("HEAD") to get files in staging area.


Full demo:

First create a test repo:

$ cd test$ mkdir repo && cd repo && touch a b c && git init && git add . && git commit -m "init"$ echo "a" > a && echo "b" > b && echo "c" > c && git add a b$ git statusOn branch masterChanges to be committed:        modified:   a        modified:   bChanges not staged for commit:        modified:   c

Now check in ipython:

$ ipythonIn [1]: import gitIn [2]: repo = git.Repo()In [3]: count_modified_files = len(repo.index.diff(None))In [4]: count_staged_files = len(repo.index.diff("HEAD"))In [5]: print count_modified_files, count_staged_files1 2