|
| 1 | +********************************************************************** |
| 2 | +git-add / git-reset |
| 3 | +********************************************************************** |
| 4 | + |
| 5 | +---------------------------------------------------------------------- |
| 6 | +Add file contents to the index / Stage |
| 7 | +---------------------------------------------------------------------- |
| 8 | + |
| 9 | +We can add a new (untracked) file or a modified file to the index. |
| 10 | + |
| 11 | +.. code-block:: bash |
| 12 | +
|
| 13 | + $> git add foo.txt |
| 14 | +
|
| 15 | +.. code-block:: python |
| 16 | +
|
| 17 | + >>> index = repo.index |
| 18 | + >>> index.add(path) |
| 19 | + >>> index.write() |
| 20 | +
|
| 21 | +---------------------------------------------------------------------- |
| 22 | +Restore the entry in the index / Unstage |
| 23 | +---------------------------------------------------------------------- |
| 24 | + |
| 25 | +.. code-block:: bash |
| 26 | +
|
| 27 | + $> git reset HEAD foo.txt |
| 28 | +
|
| 29 | +.. code-block:: python |
| 30 | +
|
| 31 | + >>> index = repo.index |
| 32 | + >>> index.remove(path) |
| 33 | + >>> # Skip the following lines if the file is new, go to "Write index" |
| 34 | + >>> ## Get the entry for this file in HEAD |
| 35 | + >>> tree_entry = repo.revparse_single('HEAD').tree[path] |
| 36 | + >>> ## Restore the entry in the index |
| 37 | + >>> index_entry = pygit2.IndexEntry(tree_entry.name, tree_entry.oid, tree_entry.filemode) |
| 38 | + >>> index.add(index_entry) |
| 39 | + >>> # Write index |
| 40 | + >>> index.write() |
| 41 | +
|
| 42 | +---------------------------------------------------------------------- |
| 43 | +Query the index state / Is file staged ? |
| 44 | +---------------------------------------------------------------------- |
| 45 | + |
| 46 | +.. code-block:: bash |
| 47 | +
|
| 48 | + $> git status foo.txt |
| 49 | +
|
| 50 | +.. code-block:: python |
| 51 | +
|
| 52 | + >>> # Return True is the file was not updated in the index (same hash) |
| 53 | + >>> index[path].oid == repo.revparse_single('HEAD').tree[path].oid |
| 54 | +
|
| 55 | +---------------------------------------------------------------------- |
| 56 | +References |
| 57 | +---------------------------------------------------------------------- |
| 58 | + |
| 59 | +- git-add_. |
| 60 | + |
| 61 | +.. _git-add: https://www.kernel.org/pub/software/scm/git/docs/git-add.html |
| 62 | + |
| 63 | +- git-reset_. |
| 64 | + |
| 65 | +.. _git-reset: https://www.kernel.org/pub/software/scm/git/docs/git-reset.html |
0 commit comments