References¶
-
Repository.
references
¶
Example:
>>> all_refs = list(repo.references)
>>> master_ref = repo.lookup_reference("refs/heads/master")
>>> commit = master_ref.peel() # or repo[master_ref.target]
# Create a reference
>>> ref = repo.references.create('refs/tags/version1', LAST_COMMIT)
# Delete a reference
>>> repo.references.delete('refs/tags/version1')
Functions¶
Check if the passed string is a valid reference name.
Example:
>>> from pygit2 import reference_is_valid_name >>> reference_is_valid_name("refs/heads/master") True >>> reference_is_valid_name("HEAD") True >>> reference_is_valid_name("refs/heads/..") False
The Reference type¶
The HEAD¶
Example. These two lines are equivalent:
>>> head = repo.lookup_reference('HEAD').resolve()
>>> head = repo.head
Branches¶
-
Repository.
branches
¶
Branches inherit from References, and additionally provide specialized accessors for some unique features.
Example:
>>> # Listing all branches
>>> branches_list = list(repo.branches)
>>> # Local only
>>> local_branches = list(repo.branches.local)
>>> # Remote only
>>> remote_branches = list(repo.branches.remote)
>>> # Get a branch
>>> branch = repo.branches['master']
>>> other_branch = repo.branches['does-not-exist'] # Will raise a KeyError
>>> other_branch = repo.branches.get('does-not-exist') # Returns None
>>> remote_branch = repo.branches.remote['upstream/feature']
>>> # Create a local branch
>>> new_branch = repo.branches.local.create('new-branch')
>>> And delete it
>>> repo.branches.delete('new-branch')
The Branch type¶
The reference log¶
Example:
>>> head = repo.references.get('refs/heads/master') # Returns None if not found
>>> # Almost equivalent to
>>> head = repo.references['refs/heads/master'] # Raises KeyError if not found
>>> for entry in head.log():
... print(entry.message)