Skip to content

Commit 98feb64

Browse files
committed
Merge remote-tracking branch 'FabriceSalvaire/master'
2 parents 7687488 + bdeb037 commit 98feb64

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

docs/recipes.rst

+1
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@ Main porcelain commands
2424
git-tag (Create, list, delete or verify a tag object signed with GPG.) <recipes/git-tag>
2525
git clone --mirror (Clone with a mirroring configuration) <recipes/git-clone-mirror>
2626
git clone username@hostname (Clone over ssh) <recipes/git-clone-ssh>
27+
git-add / git-reset HEAD (Add file contents to the index / Unstage) <recipes/git-add-reset>
2728

2829
.. _git man page: https://www.kernel.org/pub/software/scm/git/docs/git.html

docs/recipes/git-add-reset.rst

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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

Comments
 (0)