git-add / git-reset

Add file contents to the index / Stage

We can add a new (untracked) file or a modified file to the index.

$ git add foo.txt
>>> index = repo.index
>>> index.add(path)
>>> index.write()

Restore the entry in the index / Unstage

$ git reset HEAD src/tree.c
>>> index = repo.index

# Remove path from the index
>>> path = 'src/tree.c'
>>> index.remove(path)

# Restore object from db
>>> obj = repo.revparse_single('HEAD').tree[path] # Get object from db
>>> index.add(pygit2.IndexEntry(path, obj.id, obj.filemode)) # Add to index

# Write index
>>> index.write()

Query the index state / Is file staged ?

$ git status foo.txt
# Return True is the file is modified in the working tree
>>> repo.status_file(path) & pygit2.enums.FileStatus.WT_MODIFIED

References