You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Clarify detached HEAD access and attach writable test fixtures (#2230)
<!-- agent -->
Reading head.reference or active_branch raises TypeError when HEAD points
directly to a commit, but the public documentation did not clearly explain
how to access that commit. Document head.commit.hexsha for attached and
detached HEADs, explain the reference setter/getter asymmetry, and clarify
that active_branch requires an attached HEAD. Preserve the exception type
and existing message prefix while adding a hint to use .commit or .object.
Writable test fixtures assumed cloning produced an attached HEAD, so their
branch access could fail when the source checkout was detached. Have
with_rw_repo create and attach master at the requested revision when its
clone is detached, retaining the clone's branch and tracking configuration
otherwise. Explicitly attach the temporary bare remote to its own master
branch before cloning it for remote tests.
Assisted-by: GPT 6.0
Co-authored-by: GPT 6.0 <codex@openai.com>
:class:`Heads <git.refs.head.Head>` Heads are branches in git-speak. :class:`References <git.refs.reference.Reference>` are pointers to a specific commit or to other references. Heads and :class:`Tags <git.refs.tag.TagReference>` are a kind of references. GitPython allows you to query them rather intuitively.
80
80
81
+
To obtain the current commit ID, use ``repo.head.commit.hexsha``. This works both
82
+
on a branch and with a detached HEAD, provided HEAD resolves to an existing commit.
83
+
When ``repo.head.is_detached`` is true, HEAD points directly to a commit and there
84
+
is no active branch: reading ``repo.head.reference`` or ``repo.active_branch``
85
+
raises :exc:`TypeError`. The branch examples below assume an attached HEAD.
86
+
81
87
.. literalinclude:: ../../test/test_docs.py
82
88
:language: python
83
89
:dedent: 8
@@ -152,7 +158,7 @@ Examining References
152
158
:start-after: # [2-test_references_and_objects]
153
159
:end-before: # ![2-test_references_and_objects]
154
160
155
-
A :class:`symbolic reference <git.refs.symbolic.SymbolicReference>` is a special case of a reference as it points to another reference instead of a commit.
161
+
A :class:`symbolic reference <git.refs.symbolic.SymbolicReference>` can point to another reference. When detached, it points directly to a commit instead. Reading its ``commit`` property resolves the commit in either state. Assigning a commit to ``reference`` detaches it; reading ``reference`` then raises :exc:`TypeError`.
0 commit comments