0
# Index and Staging
1
2
Complete control over Git's staging area with support for adding, removing, and committing changes. Includes advanced staging operations and conflict resolution capabilities.
3
4
## Capabilities
5
6
### Index File Operations
7
8
```python { .api }
9
from typing import List, Callable
10
class IndexFile:
11
def __init__(self, repo: "Repo", file_path: PathLike = None):
12
"""Initialize index file."""
13
14
def add(self, items: list, force: bool = True, fprogress: Callable = None, path_rewriter: Callable = None, write: bool = True, write_extension_data: bool = False) -> "IndexFile":
15
"""Add files to index."""
16
17
def remove(self, items: list, working_tree: bool = False, **kwargs) -> "IndexFile":
18
"""Remove files from index."""
19
20
def commit(self, message: str, parent_commits: list = None, head: bool = True, author: "Actor" = None, committer: "Actor" = None, author_date: str = None, commit_date: str = None, skip_hooks: bool = False) -> "Commit":
21
"""Create commit from index."""
22
23
def checkout(self, paths: list = None, index: bool = True, working_tree: bool = True, **kwargs) -> "IndexFile":
24
"""Checkout files from index."""
25
26
def reset(self, commit: "Commit" = None, working_tree: bool = False, paths: list = None, **kwargs) -> "IndexFile":
27
"""Reset index."""
28
29
class IndexEntry:
30
def __init__(self, binsha: bytes, mode: int, flags: int, path: str, stage: int = 0):
31
"""Index entry."""
32
33
@property
34
def binsha(self) -> bytes:
35
"""Binary SHA-1."""
36
37
@property
38
def hexsha(self) -> str:
39
"""Hex SHA-1."""
40
41
@property
42
def mode(self) -> int:
43
"""File mode."""
44
45
@property
46
def path(self) -> str:
47
"""File path."""
48
49
@property
50
def stage(self) -> int:
51
"""Merge stage (0=normal, 1=base, 2=ours, 3=theirs)."""
52
53
class BaseIndexEntry:
54
"""Base class for index entries with minimal required information."""
55
56
def __init__(self, mode: int, binsha: bytes, flags: int, path: str):
57
"""
58
Initialize base index entry.
59
60
Args:
61
mode: File mode
62
binsha: Binary SHA-1 hash
63
flags: Index flags
64
path: File path
65
"""
66
67
@property
68
def mode(self) -> int:
69
"""File mode."""
70
71
@property
72
def binsha(self) -> bytes:
73
"""Binary SHA-1."""
74
75
@property
76
def flags(self) -> int:
77
"""Index flags."""
78
79
@property
80
def path(self) -> str:
81
"""File path."""
82
83
class BlobFilter:
84
"""Filter predicate for selecting blobs by path patterns."""
85
86
def __init__(self, paths: List[str]):
87
"""
88
Initialize blob filter.
89
90
Args:
91
paths: List of paths to filter by
92
"""
93
94
def __call__(self, path: str) -> bool:
95
"""Check if path matches filter."""
96
97
StageType = int # 0=base, 1=ours, 2=theirs, 3=merge
98
```
99
100
## Usage Examples
101
102
```python
103
from git import Repo, Actor
104
105
repo = Repo('/path/to/repo')
106
107
# Stage files
108
repo.index.add(['file1.txt', 'file2.txt'])
109
110
# Commit changes
111
author = Actor("John Doe", "john@example.com")
112
commit = repo.index.commit("Added files", author=author)
113
114
# Remove files from staging
115
repo.index.remove(['old_file.txt'])
116
117
# Reset index
118
repo.index.reset()
119
120
# Check staging status
121
for item in repo.index.entries:
122
print(f"Staged: {item}")
123
```