The repository

Everything starts either by creating a new repository, or by opening an existing one.


pygit2.init_repository(path, bare=False, flags=16, mode=0, workdir_path=None, description=None, template_path=None, initial_head=None, origin_url=None)

Creates a new Git repository in the given path.

If bare is True the repository will be bare, i.e. it will not have a working copy.

The flags may be a combination of:

  • GIT_REPOSITORY_INIT_BARE (overriden by the bare parameter)
  • GIT_REPOSITORY_INIT_MKPATH (set by default)

The mode parameter may be any of GIT_REPOSITORY_SHARED_UMASK (default), GIT_REPOSITORY_SHARED_GROUP or GIT_REPOSITORY_INIT_SHARED_ALL, or a custom value.

The workdir_path, description, template_path, initial_head and origin_url are all strings.

See libgit2’s documentation on git_repository_init_ext for further details.


>>> from pygit2 import init_repository
>>> repo = init_repository('test')            # Creates a non-bare repository
>>> repo = init_repository('test', bare=True) # Creates a bare repository
pygit2.clone_repository(url, path, bare=False, repository=None, remote=None, checkout_branch=None, callbacks=None)

Clones a new Git repository from url in the given path.

Returns a Repository class pointing to the newly cloned repository.

  • url (str) – URL of the repository to clone
  • path (str) – Local path to clone into
  • bare (bool) – Whether the local repository should be bare
  • remote (callable) – Callback for the remote to use.
  • repository (callable) – Callback for the repository to use.
  • checkout_branch (str) – Branch to checkout after the clone. The default is to use the remote’s default branch.
  • callbacks (RemoteCallbacks) – object which implements the callbacks as methods.
Return type:


The repository callback has (path, bare) -> Repository as a signature. The Repository it returns will be used instead of creating a new one.

The remote callback has (Repository, name, url) -> Remote as a signature. The Remote it returns will be used instead of the default one.

The callbacks should be an object which inherits from pyclass:RemoteCallbacks.


>>> from pygit2 import clone_repository
>>> repo_url = 'git://'
>>> repo_path = '/path/to/create/repository'
>>> repo = clone_repository(repo_url, repo_path) # Clones a non-bare repository
>>> repo = clone_repository(repo_url, repo_path, bare=True) # Clones a bare repository
pygit2.discover_repository(path[, across_fs[, ceiling_dirs]]) → str

Look for a git repository and return its path.


>>> current_working_directory = os.getcwd()
>>> repository_path = discover_repository(current_working_directory)
>>> repo = Repository(repository_path)

The Repository class

class pygit2.Repository(path)

The Repository constructor only takes one argument, the path of the repository to open.


>>> from pygit2 import Repository
>>> repo = Repository('pygit2/.git')

The API of the Repository class is quite large. Since this documentation is orgaized by features, the related bits are explained in the related chapters, for instance the pygit2.Repository.checkout() method are explained in the Checkout section.

Below there are some general attributes and methods:


The normalized path to the git repository.


The normalized path to the working directory of the repository. If the repository is bare, None will be returned.


Check if a repository is a bare repository.


Check if a repository is empty.


Return the signature according to the repository’s configuration → type, data, size

Read raw object data from the repository.

Repository.write(type, data) → Oid

Write raw object data into the repository. First arg is the object type, the second one a buffer with data. Return the Oid of the created object.

Repository.reset(oid, reset_type)

Resets current head to the provided oid. reset_type: GIT_RESET_SOFT: resets head to point to oid, but does not modify working copy, and leaves the changes in the index. GIT_RESET_MIXED: resets head to point to oid, but does not modify working copy. It empties the index too. GIT_RESET_HARD: resets head to point to oid, and resets too the working copy and the content of the index.


Remove all the metadata associated with an ongoing command like merge, revert, cherry-pick, etc. For example: MERGE_HEAD, MERGE_MSG, etc.

Repository.write_archive(treeish, archive, timestamp=None, prefix='')

Write treeish into an archive

If no timestamp is provided and ‘treeish’ is a commit, its committer timestamp will be used. Otherwise the current time will be used.

All path names in the archive are added to ‘prefix’, which defaults to an empty string.


The treeish to write.
An archive from the ‘tarfile’ module
Timestamp to use for the files in the archive.
Extra prefix to add to the path names in the archive.


>>> import tarfile, pygit2
>>>> with'foo.tar', 'w') as archive:
>>>>     repo = pygit2.Repsitory('.')
>>>>     repo.write_archive(, archive)
Repository.ahead_behind(local, upstream)

Calculate how many different commits are in the non-common parts of the history between the two given ids.

Ahead is how many commits are in the ancestry of the ‘local’ commit which are not in the ‘upstream’ commit. Behind is the opposite.


The commit which is considered the local or current state
The commit which is considered the upstream

Returns a tuple of two integers with the number of commits ahead and behind respectively.

Repository.describe(committish=None, max_candidates_tags=None, describe_strategy=None, pattern=None, only_follow_first_parent=None, show_commit_oid_as_fallback=None, abbreviated_size=None, always_use_long_format=None, dirty_suffix=None)

Describe a commit-ish or the current working tree.

  • committish (str, Reference, or Commit) – Commit-ish object or object name to describe, or None to describe the current working tree.
  • max_candidates_tags (int) – The number of candidate tags to consider. Increasing above 10 will take slightly longer but may produce a more accurate result. A value of 0 will cause only exact matches to be output.
  • describe_strategy (int) – A GIT_DESCRIBE_* constant.
  • pattern (str) – Only consider tags matching the given glob(7) pattern, excluding the “refs/tags/” prefix.
  • only_follow_first_parent (bool) – Follow only the first parent commit upon seeing a merge commit.
  • show_commit_oid_as_fallback (bool) – Show uniquely abbreviated commit object as fallback.
  • abbreviated_size (int) – The minimum number of hexadecimal digits to show for abbreviated object names. A value of 0 will suppress long format, only showing the closest tag.
  • always_use_long_format (bool) – Always output the long format (the nearest tag, the number of commits, and the abbrevated commit name) even when the committish matches a tag.
  • dirty_suffix (str) – A string to append if the working tree is dirty.

The description.

Return type:



repo.describe(pattern='public/*', dirty_suffix='-dirty')

Check if a path is ignored in the repository.

Repository.create_reference(name, target, force=False)

Create a new reference “name” which points to an object or to another reference.

Based on the type and value of the target parameter, this method tries to guess whether it is a direct or a symbolic reference.

Keyword arguments:

If True references will be overridden, otherwise (the default) an exception is raised.


repo.create_reference('refs/tags/foo', 'refs/heads/master')
repo.create_reference('refs/tags/foo', 'bbb78a9cec580')