API Reference

Objects.Base

class git.objects.base.Object(repo, binsha)

Implements an Object which may be Blobs, Trees, Commits and Tags

NULL_BIN_SHA = '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
NULL_HEX_SHA = '0000000000000000000000000000000000000000'
TYPES = ('blob', 'tree', 'commit', 'tag')
__eq__(other)
Returns:True if the objects have the same SHA1
__hash__()
Returns:Hash of our id allowing objects to be used in dicts and sets
__init__(repo, binsha)

Initialize an object by identifying it by its binary sha. All keyword arguments will be set on demand if None.

Parameters:
  • repo – repository this object is located in
  • binsha – 20 byte SHA1
__module__ = 'git.objects.base'
__ne__(other)
Returns:True if the objects do not have the same SHA1
__repr__()
Returns:string with pythonic representation of our object
__slots__ = ('repo', 'binsha', 'size')
__str__()
Returns:string of our SHA1 as understood by all git commands
binsha
data_stream
Returns:File Object compatible stream to the uncompressed raw data of the object
Note:returned streams must be read in order
hexsha
Returns:40 byte hex version of our 20 byte binary sha
classmethod new(repo, id)
Returns:New Object instance of a type appropriate to the object type behind id. The id of the newly created object will be a binsha even though the input id may have been a Reference or Rev-Spec
Parameters:id – reference, rev-spec, or hexsha
Note:This cannot be a __new__ method as it would always call __init__ with the input id which is not necessarily a binsha.
classmethod new_from_sha(repo, sha1)
Returns:new object instance of a type appropriate to represent the given binary sha1
Parameters:sha1 – 20 byte binary sha1
repo
size
stream_data(ostream)

Writes our data directly to the given output stream :param ostream: File object compatible stream object. :return: self

type = None
class git.objects.base.IndexObject(repo, binsha, mode=None, path=None)

Base for all objects that can be part of the index file , namely Tree, Blob and SubModule objects

__hash__()
Returns:Hash of our path as index items are uniquely identifyable by path, not by their data !
__init__(repo, binsha, mode=None, path=None)

Initialize a newly instanced IndexObject

Parameters:
  • repo – is the Repo we are located in
  • binsha – 20 byte sha1
  • mode – is the stat compatible file mode as int, use the stat module to evaluate the information
  • path – is the path to the file in the file system, relative to the git repository root, i.e. file.ext or folder/other.ext
Note:

Path may not be set of the index object has been created directly as it cannot be retrieved without knowing the parent tree.

__module__ = 'git.objects.base'
__slots__ = ('path', 'mode')
abspath
Returns:Absolute path to this index object in the file system ( as opposed to the .path field which is a path relative to the git repository ).

The returned path will be native to the system and contains ‘’ on windows.

mode
name
Returns:Name portion of the path, effectively being the basename
path

Objects.Blob

class git.objects.blob.Blob(repo, binsha, mode=None, path=None)

A Blob encapsulates a git blob object

DEFAULT_MIME_TYPE = 'text/plain'
__module__ = 'git.objects.blob'
__slots__ = ()
executable_mode = 33261
file_mode = 33188
mime_type
Returns:String describing the mime type of this file (based on the filename)
Note:Defaults to ‘text/plain’ in case the actual file type is unknown.
type = 'blob'

Objects.Commit

class git.objects.commit.Commit(repo, binsha, tree=None, author=None, authored_date=None, author_tz_offset=None, committer=None, committed_date=None, committer_tz_offset=None, message=None, parents=None, encoding=None, gpgsig=None)

Wraps a git Commit object.

This class will act lazily on some of its attributes and will query the value on demand only if it involves calling the git binary.

__init__(repo, binsha, tree=None, author=None, authored_date=None, author_tz_offset=None, committer=None, committed_date=None, committer_tz_offset=None, message=None, parents=None, encoding=None, gpgsig=None)

Instantiate a new Commit. All keyword arguments taking None as default will be implicitly set on first query.

Parameters:
  • binsha – 20 byte sha1
  • parents – tuple( Commit, ... ) is a tuple of commit ids or actual Commits
  • tree – Tree Tree object
  • author – Actor is the author string ( will be implicitly converted into an Actor object )
  • authored_date – int_seconds_since_epoch is the authored DateTime - use time.gmtime() to convert it into a different format
  • author_tz_offset – int_seconds_west_of_utc is the timezone that the authored_date is in
  • committer – Actor is the committer string
  • committed_date – int_seconds_since_epoch is the committed DateTime - use time.gmtime() to convert it into a different format
  • committer_tz_offset – int_seconds_west_of_utc is the timezone that the committed_date is in
  • message – string is the commit message
  • encoding – string encoding of the message, defaults to UTF-8
  • parents – List or tuple of Commit objects which are our parent(s) in the commit dependency graph
Returns:

git.Commit

Note:

Timezone information is in the same format and in the same sign as what time.altzone returns. The sign is inverted compared to git’s UTC timezone.

__module__ = 'git.objects.commit'
__slots__ = ('tree', 'author', 'authored_date', 'author_tz_offset', 'committer', 'committed_date', 'committer_tz_offset', 'message', 'parents', 'encoding', 'gpgsig')
author
author_tz_offset
authored_date
authored_datetime
committed_date
committed_datetime
committer
committer_tz_offset
conf_encoding = 'i18n.commitencoding'
count(paths='', **kwargs)

Count the number of commits reachable from this commit

Parameters:
  • paths – is an optional path or a list of paths restricting the return value to commits actually containing the paths
  • kwargs – Additional options to be passed to git-rev-list. They must not alter the ouput style of the command, or parsing will yield incorrect results
Returns:

int defining the number of reachable commits

classmethod create_from_tree(repo, tree, message, parent_commits=None, head=False, author=None, committer=None, author_date=None, commit_date=None)

Commit the given tree, creating a commit object.

Parameters:
  • repo – Repo object the commit should be part of
  • tree – Tree object or hex or bin sha the tree of the new commit
  • message – Commit message. It may be an empty string if no message is provided. It will be converted to a string in any case.
  • parent_commits – Optional Commit objects to use as parents for the new commit. If empty list, the commit will have no parents at all and become a root commit. If None , the current head commit will be the parent of the new commit object
  • head – If True, the HEAD will be advanced to the new commit automatically. Else the HEAD will remain pointing on the previous commit. This could lead to undesired results when diffing files.
  • author – The name of the author, optional. If unset, the repository configuration is used to obtain this value.
  • committer – The name of the committer, optional. If unset, the repository configuration is used to obtain this value.
  • author_date – The timestamp for the author field
  • commit_date – The timestamp for the committer field
Returns:

Commit object representing the new commit

Note:

Additional information about the committer and Author are taken from the environment or from the git configuration, see git-commit-tree for more information

default_encoding = 'UTF-8'
encoding
env_author_date = 'GIT_AUTHOR_DATE'
env_committer_date = 'GIT_COMMITTER_DATE'
gpgsig
classmethod iter_items(repo, rev, paths='', **kwargs)

Find all commits matching the given criteria.

Parameters:
  • repo – is the Repo
  • rev – revision specifier, see git-rev-parse for viable options
  • paths – is an optional path or list of paths, if set only Commits that include the path or paths will be considered
  • kwargs – optional keyword arguments to git rev-list where max_count is the maximum number of commits to fetch skip is the number of commits to skip since all commits since i.e. ‘1970-01-01’
Returns:

iterator yielding Commit items

iter_parents(paths='', **kwargs)

Iterate _all_ parents of this commit.

Parameters:
  • paths – Optional path or list of paths limiting the Commits to those that contain at least one of the paths
  • kwargs – All arguments allowed by git-rev-list
Returns:

Iterator yielding Commit objects which are parents of self

message
name_rev
Returns:String describing the commits hex sha based on the closest Reference. Mostly useful for UI purposes
parents
stats

Create a git stat from changes between this commit and its first parent or from all changes done if this is the very first commit.

Returns:git.Stats
summary
Returns:First line of the commit message
tree
type = 'commit'

Objects.Tag

Module containing all object based types.

class git.objects.tag.TagObject(repo, binsha, object=None, tag=None, tagger=None, tagged_date=None, tagger_tz_offset=None, message=None)

Non-Lightweight tag carrying additional information about an object we are pointing to.

__init__(repo, binsha, object=None, tag=None, tagger=None, tagged_date=None, tagger_tz_offset=None, message=None)

Initialize a tag object with additional data

Parameters:
  • repo – repository this object is located in
  • binsha – 20 byte SHA1
  • object – Object instance of object we are pointing to
  • tag – name of this tag
  • tagger – Actor identifying the tagger
  • tagged_date – int_seconds_since_epoch is the DateTime of the tag creation - use time.gmtime to convert it into a different format
  • tagged_tz_offset – int_seconds_west_of_utc is the timezone that the authored_date is in, in a format similar to time.altzone
__module__ = 'git.objects.tag'
__slots__ = ('object', 'tag', 'tagger', 'tagged_date', 'tagger_tz_offset', 'message')
message
object
tag
tagged_date
tagger
tagger_tz_offset
type = 'tag'

Objects.Tree

class git.objects.tree.TreeModifier(cache)

A utility class providing methods to alter the underlying cache in a list-like fashion.

Once all adjustments are complete, the _cache, which really is a reference to the cache of a tree, will be sorted. Assuring it will be in a serializable state

__delitem__(name)

Deletes an item with the given name if it exists

__init__(cache)
__module__ = 'git.objects.tree'
__slots__ = '_cache'
add(sha, mode, name, force=False)

Add the given item to the tree. If an item with the given name already exists, nothing will be done, but a ValueError will be raised if the sha and mode of the existing item do not match the one you add, unless force is True

Parameters:
  • sha – The 20 or 40 byte sha of the item to add
  • mode – int representing the stat compatible mode of the item
  • force – If True, an item with your name and information will overwrite any existing item with the same name, no matter which information it has
Returns:

self

add_unchecked(binsha, mode, name)

Add the given item to the tree, its correctness is assumed, which puts the caller into responsibility to assure the input is correct. For more information on the parameters, see add :param binsha: 20 byte binary sha

set_done()

Call this method once you are done modifying the tree information. It may be called several times, but be aware that each call will cause a sort operation :return self:

class git.objects.tree.Tree(repo, binsha, mode=16384, path=None)

Tree objects represent an ordered list of Blobs and other Trees.

Tree as a list:

Access a specific blob using the
tree['filename'] notation.

You may as well access by index
blob = tree[0]
__contains__(item)
__div__(file)

For PY2 only

__getitem__(item)
__getslice__(i, j)
__init__(repo, binsha, mode=16384, path=None)
__iter__()
__len__()
__module__ = 'git.objects.tree'
__reversed__()
__slots__ = '_cache'
__truediv__(file)

For PY3 only

blob_id = 8
blobs
Returns:list(Blob, ...) list of blobs directly below this tree
cache
Returns:An object allowing to modify the internal cache. This can be used to change the tree’s contents. When done, make sure you call set_done on the tree modifier, or serialization behaviour will be incorrect. See the TreeModifier for more information on how to alter the cache
commit_id = 14
join(file)

Find the named object in this tree’s contents :return: git.Blob or git.Tree or git.Submodule

Raises:KeyError – if given file or tree does not exist in tree
traverse(predicate=<function <lambda>>, prune=<function <lambda>>, depth=-1, branch_first=True, visit_once=False, ignore_self=1)

For documentation, see util.Traversable.traverse Trees are set to visit_once = False to gain more performance in the traversal

tree_id = 4
trees
Returns:list(Tree, ...) list of trees directly below this tree
type = 'tree'

Objects.Functions

Module with functions which are supposed to be as fast as possible

git.objects.fun.tree_to_stream(entries, write)

Write the give list of entries into a stream using its write method :param entries: sorted list of tuples with (binsha, mode, name) :param write: write method which takes a data string

git.objects.fun.tree_entries_from_data(data)

Reads the binary representation of a tree and returns tuples of Tree items :param data: data block with tree data (as bytes) :return: list(tuple(binsha, mode, tree_relative_path), ...)

git.objects.fun.traverse_trees_recursive(odb, tree_shas, path_prefix)
Returns:

list with entries according to the given binary tree-shas. The result is encoded in a list of n tuple|None per blob/commit, (n == len(tree_shas)), where * [0] == 20 byte sha * [1] == mode as int * [2] == path relative to working tree root The entry tuple is None if the respective blob/commit did not exist in the given tree.

Parameters:
  • tree_shas – iterable of shas pointing to trees. All trees must be on the same level. A tree-sha may be None in which case None
  • path_prefix – a prefix to be added to the returned paths on this level, set it ‘’ for the first iteration
Note:

The ordering of the returned items will be partially lost

git.objects.fun.traverse_tree_recursive(odb, tree_sha, path_prefix)
Returns:list of entries of the tree pointed to by the binary tree_sha. An entry has the following format: * [0] 20 byte sha * [1] mode as int * [2] path relative to the repository
Parameters:path_prefix – prefix to prepend to the front of all returned paths

Objects.Submodule.base

class git.objects.submodule.base.Submodule(repo, binsha, mode=None, path=None, name=None, parent_commit=None, url=None, branch_path=None)

Implements access to a git submodule. They are special in that their sha represents a commit in the submodule’s repository which is to be checked out at the path of this instance. The submodule type does not have a string type associated with it, as it exists solely as a marker in the tree and index.

All methods work in bare and non-bare repositories.

__eq__(other)

Compare with another submodule

__hash__()

Hash this instance using its logical id, not the sha

__init__(repo, binsha, mode=None, path=None, name=None, parent_commit=None, url=None, branch_path=None)

Initialize this instance with its attributes. We only document the ones that differ from IndexObject

Parameters:
  • repo – Our parent repository
  • binsha – binary sha referring to a commit in the remote repository, see url parameter
  • parent_commit – see set_parent_commit()
  • url – The url to the remote repository which is the submodule
  • branch_path – full (relative) path to ref to checkout when cloning the remote repository
__module__ = 'git.objects.submodule.base'
__ne__(other)

Compare with another submodule for inequality

__repr__()
__slots__ = ('_parent_commit', '_url', '_branch_path', '_name', '__weakref__')
__str__()
__weakref__

list of weak references to the object (if defined)

classmethod add(repo, name, path, url=None, branch=None, no_checkout=False)

Add a new submodule to the given repository. This will alter the index as well as the .gitmodules file, but will not create a new commit. If the submodule already exists, no matter if the configuration differs from the one provided, the existing submodule will be returned.

Parameters:
  • repo – Repository instance which should receive the submodule
  • name – The name/identifier for the submodule
  • path – repository-relative or absolute path at which the submodule should be located It will be created as required during the repository initialization.
  • url – git-clone compatible URL, see git-clone reference for more information If None, the repository is assumed to exist, and the url of the first remote is taken instead. This is useful if you want to make an existing repository a submodule of anotherone.
  • branch – name of branch at which the submodule should (later) be checked out. The given branch must exist in the remote repository, and will be checked out locally as a tracking branch. It will only be written into the configuration if it not None, which is when the checked out branch will be the one the remote HEAD pointed to. The result you get in these situation is somewhat fuzzy, and it is recommended to specify at least ‘master’ here. Examples are ‘master’ or ‘feature/new’
  • no_checkout – if True, and if the repository has to be cloned manually, no checkout will be performed
Returns:

The newly created submodule instance

Note:

works atomically, such that no change will be done if the repository update fails for instance

branch
Returns:The branch instance that we are to checkout
Raises:InvalidGitRepositoryError – if our module is not yet checked out
branch_name
Returns:the name of the branch, which is the shortest possible branch name
branch_path
Returns:full (relative) path as string to the branch we would checkout from the remote and track
children()
Returns:IterableList(Submodule, ...) an iterable list of submodules instances which are children of this submodule or 0 if the submodule is not checked out
config_reader()
Returns:ConfigReader instance which allows you to qurey the configuration values of this submodule, as provided by the .gitmodules file
Note:The config reader will actually read the data directly from the repository and thus does not need nor care about your working tree.
Note:Should be cached by the caller and only kept as long as needed
Raises:IOError – If the .gitmodules file/blob could not be read
config_writer(*args, **kwargs)
Returns:a config writer instance allowing you to read and write the data

belonging to this submodule into the .gitmodules file.

Parameters:
  • index – if not None, an IndexFile instance which should be written. defaults to the index of the Submodule’s parent repository.
  • write – if True, the index will be written each time a configuration value changes.
Note:

the parameters allow for a more efficient writing of the index, as you can pass in a modified index on your own, prevent automatic writing, and write yourself once the whole operation is complete

Raises:
  • ValueError – if trying to get a writer on a parent_commit which does not match the current head commit
  • IOError – If the .gitmodules file/blob could not be read
exists()
Returns:True if the submodule exists, False otherwise. Please note that a submodule may exist (in the .gitmodules file) even though its module doesn’t exist on disk
classmethod iter_items(repo, parent_commit='HEAD')
Returns:iterator yielding Submodule instances available in the given repository
k_default_mode = 57344
k_head_default = 'master'
k_head_option = 'branch'
k_modules_file = '.gitmodules'
module(*args, **kwargs)
Returns:Repo instance initialized from the repository at our submodule path
Raises:InvalidGitRepositoryError – if a repository was not available. This could also mean that it was not yet initialized
module_exists()
Returns:True if our module exists and is a valid git repository. See module() method
move(*args, **kwargs)

Move the submodule to a another module path. This involves physically moving the repository at our current path, changing the configuration, as well as adjusting our index entry accordingly.

Parameters:
  • module_path – the path to which to move our module in the parent repostory’s working tree, given as repository-relative or absolute path. Intermediate directories will be created accordingly. If the path already exists, it must be empty. Trailing (back)slashes are removed automatically
  • configuration – if True, the configuration will be adjusted to let the submodule point to the given path.
  • module – if True, the repository managed by this submodule will be moved as well. If False, we don’t move the submodule’s checkout, which may leave the parent repository in an inconsistent state.
Returns:

self

Raises:

ValueError – if the module path existed and was not empty, or was a file

Note:

Currently the method is not atomic, and it could leave the repository in an inconsistent state if a sub-step fails for some reason

name
Returns:The name of this submodule. It is used to identify it within the .gitmodules file.
Note:by default, the name is the path at which to find the submodule, but in git-python it should be a unique identifier similar to the identifiers used for remotes, which allows to change the path of the submodule easily
parent_commit
Returns:Commit instance with the tree containing the .gitmodules file
Note:will always point to the current head’s commit if it was not set explicitly
remove(*args, **kwargs)

Remove this submodule from the repository. This will remove our entry from the .gitmodules file and the entry in the .git/config file.

Parameters:
  • module – If True, the module checkout we point to will be deleted as well. If the module is currently on a commit which is not part of any branch in the remote, if the currently checked out branch working tree, or untracked files, is ahead of its tracking branch, if you have modifications in the In case the removal of the repository fails for these reasons, the submodule status will not have been altered. If this submodule has child-modules on its own, these will be deleted prior to touching the own module.
  • force – Enforces the deletion of the module even though it contains modifications. This basically enforces a brute-force file system based deletion.
  • configuration – if True, the submodule is deleted from the configuration, otherwise it isn’t. Although this should be enabled most of the times, this flag enables you to safely delete the repository of your submodule.
  • dry_run – if True, we will not actually do anything, but throw the errors we would usually throw
Returns:

self

Note:

doesn’t work in bare repositories

Note:

doesn’t work atomically, as failure to remove any part of the submodule will leave an inconsistent state

Raises:
  • InvalidGitRepositoryError – thrown if the repository cannot be deleted
  • OSError – if directories or files could not be removed
rename(*args, **kwargs)

Rename this submodule :note: This method takes care of renaming the submodule in various places, such as

  • $parent_git_dir/config
  • $working_tree_dir/.gitmodules
  • (git >=v1.8.0: move submodule repository to new name)

As .gitmodules will be changed, you would need to make a commit afterwards. The changed .gitmodules file will already be added to the index

Returns:this submodule instance
set_parent_commit(commit, check=True)

Set this instance to use the given commit whose tree is supposed to contain the .gitmodules blob.

Parameters:
  • commit – Commit’ish reference pointing at the root_tree, or None to always point to the most recent commit
  • check – if True, relatively expensive checks will be performed to verify validity of the submodule.
Raises:
  • ValueError – if the commit’s tree didn’t contain the .gitmodules blob.
  • ValueError – if the parent commit didn’t store this submodule under the current path
Returns:

self

type = 'submodule'
update(recursive=False, init=True, to_latest_revision=False, progress=None, dry_run=False, force=False, keep_going=False)

Update the repository of this submodule to point to the checkout we point at with the binsha of this instance.

Parameters:
  • recursive – if True, we will operate recursively and update child- modules as well.
  • init – if True, the module repository will be cloned into place if necessary
  • to_latest_revision – if True, the submodule’s sha will be ignored during checkout. Instead, the remote will be fetched, and the local tracking branch updated. This only works if we have a local tracking branch, which is the case if the remote repository had a master branch, or of the ‘branch’ option was specified for this submodule and the branch existed remotely
  • progress – UpdateProgress instance or None if no progress should be shown
  • dry_run – if True, the operation will only be simulated, but not performed. All performed operations are read-only
  • force – If True, we may reset heads even if the repository in question is dirty. Additinoally we will be allowed to set a tracking branch which is ahead of its remote branch back into the past or the location of the remote branch. This will essentially ‘forget’ commits. If False, local tracking branches that are in the future of their respective remote branches will simply not be moved.
  • keep_going – if True, we will ignore but log all errors, and keep going recursively. Unless dry_run is set as well, keep_going could cause subsequent/inherited errors you wouldn’t see otherwise. In conjunction with dry_run, it can be useful to anticipate all errors when updating submodules
Note:

does nothing in bare repositories

Note:

method is definitely not atomic if recurisve is True

Returns:

self

url
Returns:The url to the repository which our module-repository refers to
class git.objects.submodule.base.UpdateProgress

Class providing detailed progress information to the caller who should derive from it and implement the update(...) message

CLONE = 512
FETCH = 1024
UPDWKTREE = 2048
__module__ = 'git.objects.submodule.base'
__slots__ = ()
x = 11

Objects.Submodule.root

class git.objects.submodule.root.RootModule(repo)

A (virtual) Root of all submodules in the given repository. It can be used to more easily traverse all submodules of the master repository

__init__(repo)
__module__ = 'git.objects.submodule.root'
__slots__ = ()
k_root_name = '__ROOT__'
module()
Returns:the actual repository containing the submodules
update(previous_commit=None, recursive=True, force_remove=False, init=True, to_latest_revision=False, progress=None, dry_run=False, force_reset=False, keep_going=False)

Update the submodules of this repository to the current HEAD commit. This method behaves smartly by determining changes of the path of a submodules repository, next to changes to the to-be-checked-out commit or the branch to be checked out. This works if the submodules ID does not change. Additionally it will detect addition and removal of submodules, which will be handled gracefully.

Parameters:
  • previous_commit – If set to a commit’ish, the commit we should use as the previous commit the HEAD pointed to before it was set to the commit it points to now. If None, it defaults to HEAD@{1} otherwise
  • recursive – if True, the children of submodules will be updated as well using the same technique
  • force_remove – If submodules have been deleted, they will be forcibly removed. Otherwise the update may fail if a submodule’s repository cannot be deleted as changes have been made to it (see Submodule.update() for more information)
  • init – If we encounter a new module which would need to be initialized, then do it.
  • to_latest_revision – If True, instead of checking out the revision pointed to by this submodule’s sha, the checked out tracking branch will be merged with the latest remote branch fetched from the repository’s origin. Unless force_reset is specified, a local tracking branch will never be reset into its past, therefore the remote branch must be in the future for this to have an effect.
  • force_reset – if True, submodules may checkout or reset their branch even if the repository has pending changes that would be overwritten, or if the local tracking branch is in the future of the remote tracking branch and would be reset into its past.
  • progress – RootUpdateProgress instance or None if no progress should be sent
  • dry_run – if True, operations will not actually be performed. Progress messages will change accordingly to indicate the WOULD DO state of the operation.
  • keep_going – if True, we will ignore but log all errors, and keep going recursively. Unless dry_run is set as well, keep_going could cause subsequent/inherited errors you wouldn’t see otherwise. In conjunction with dry_run, it can be useful to anticipate all errors when updating submodules
Returns:

self

class git.objects.submodule.root.RootUpdateProgress

Utility class which adds more opcodes to the UpdateProgress

BRANCHCHANGE = 16384
PATHCHANGE = 8192
REMOVE = 4096
URLCHANGE = 32768
__module__ = 'git.objects.submodule.root'
__slots__ = ()
x = 15

Objects.Submodule.util

git.objects.submodule.util.sm_section(name)
Returns:section title used in .gitmodules configuration file
git.objects.submodule.util.sm_name(section)
Returns:name of the submodule as parsed from the section name
git.objects.submodule.util.mkhead(repo, path)
Returns:New branch/head instance
git.objects.submodule.util.find_first_remote_branch(remotes, branch_name)

Find the remote branch matching the name of the given branch or raise InvalidGitRepositoryError

class git.objects.submodule.util.SubmoduleConfigParser(*args, **kwargs)

Catches calls to _write, and updates the .gitmodules blob in the index with the new data, if we have written into a stream. Otherwise it will add the local file to the index to make it correspond with the working tree. Additionally, the cache must be cleared

Please note that no mutating method will work in bare mode

__abstractmethods__ = frozenset([])
__init__(*args, **kwargs)
__module__ = 'git.objects.submodule.util'
flush_to_index()

Flush changes in our configuration file to the index

set_submodule(submodule)

Set this instance’s submodule. It must be called before the first write operation begins

write()

Objects.Util

Module for general utility functions

git.objects.util.get_object_type_by_name(object_type_name)
Returns:type suitable to handle the given object type name. Use the type to create new instances.
Parameters:object_type_name – Member of TYPES
Raises:ValueError – In case object_type_name is unknown
git.objects.util.parse_date(string_date)

Parse the given date as one of the following

  • Git internal format: timestamp offset
  • RFC 2822: Thu, 07 Apr 2005 22:13:13 +0200.
  • ISO 8601 2005-04-07T22:13:13
    The T can be a space as well
Returns:Tuple(int(timestamp_UTC), int(offset)), both in seconds since epoch
Raises:ValueError – If the format could not be understood
Note:Date can also be YYYY.MM.DD, MM/DD/YYYY and DD.MM.YYYY.
git.objects.util.parse_actor_and_date(line)

Parse out the actor (author or committer) info from a line like:

author Tom Preston-Werner <tom@mojombo.com> 1191999972 -0700
Returns:[Actor, int_seconds_since_epoch, int_timezone_offset]
class git.objects.util.ProcessStreamAdapter(process, stream_name)

Class wireing all calls to the contained Process instance.

Use this type to hide the underlying process to provide access only to a specified stream. The process is usually wrapped into an AutoInterrupt class to kill it if the instance goes out of scope.

__getattr__(attr)
__init__(process, stream_name)
__module__ = 'git.objects.util'
__slots__ = ('_proc', '_stream')
class git.objects.util.Traversable

Simple interface to perform depth-first or breadth-first traversals into one direction. Subclasses only need to implement one function. Instances of the Subclass must be hashable

__module__ = 'git.objects.util'
__slots__ = ()
list_traverse(*args, **kwargs)
Returns:IterableList with the results of the traversal as produced by traverse()
traverse(predicate=<function <lambda>>, prune=<function <lambda>>, depth=-1, branch_first=True, visit_once=True, ignore_self=1, as_edge=False)
Returns:

iterator yielding of items found when traversing self

Parameters:
  • predicate – f(i,d) returns False if item i at depth d should not be included in the result
  • prune – f(i,d) return True if the search should stop at item i at depth d. Item i will not be returned.
  • depth – define at which level the iteration should not go deeper if -1, there is no limit if 0, you would effectively only get self, the root of the iteration i.e. if 1, you would only get the first level of predecessors/successors
  • branch_first – if True, items will be returned branch first, otherwise depth first
  • visit_once – if True, items will only be returned once, although they might be encountered several times. Loops are prevented that way.
  • ignore_self – if True, self will be ignored and automatically pruned from the result. Otherwise it will be the first item to be returned. If as_edge is True, the source of the first edge is None
  • as_edge – if True, return a pair of items, first being the source, second the destination, i.e. tuple(src, dest) with the edge spanning from source to destination
git.objects.util.altz_to_utctz_str(altz)

As above, but inverses the operation, returning a string that can be used in commit objects

git.objects.util.utctz_to_altz(utctz)

we convert utctz to the timezone in seconds, it is the format time.altzone returns. Git stores it as UTC timezone which has the opposite sign as well, which explains the -1 * ( that was made explicit here ) :param utctz: git utc timezone string, i.e. +0200

git.objects.util.verify_utctz(offset)
Raises:ValueError – if offset is incorrect
Returns:offset
class git.objects.util.Actor(name, email)

Actors hold information about a person acting on the repository. They can be committers and authors or anything with a name and an email as mentioned in the git log entries.

__eq__(other)
__hash__()
__init__(name, email)
__module__ = 'git.util'
__ne__(other)
__repr__()
__slots__ = ('name', 'email')
__str__()
classmethod author(config_reader=None)

Same as committer(), but defines the main author. It may be specified in the environment, but defaults to the committer

classmethod committer(config_reader=None)
Returns:Actor instance corresponding to the configured committer. It behaves similar to the git implementation, such that the environment will override configuration values of config_reader. If no value is set at all, it will be generated
Parameters:config_reader – ConfigReader to use to retrieve the values from in case they are not set in the environment
conf_email = 'email'
conf_name = 'name'
email
env_author_email = 'GIT_AUTHOR_EMAIL'
env_author_name = 'GIT_AUTHOR_NAME'
env_committer_email = 'GIT_COMMITTER_EMAIL'
env_committer_name = 'GIT_COMMITTER_NAME'
name
name_email_regex = <_sre.SRE_Pattern object>
name_only_regex = <_sre.SRE_Pattern object>
class git.objects.util.tzoffset(secs_west_of_utc, name=None)
__dict__ = dict_proxy({'__module__': 'git.objects.util', 'dst': <function dst>, 'utcoffset': <function utcoffset>, 'tzname': <function tzname>, '__dict__': <attribute '__dict__' of 'tzoffset' objects>, '__weakref__': <attribute '__weakref__' of 'tzoffset' objects>, '__doc__': None, '__init__': <function __init__>})
__init__(secs_west_of_utc, name=None)
__module__ = 'git.objects.util'
__weakref__

list of weak references to the object (if defined)

dst(dt)
tzname(dt)
utcoffset(dt)

Index.Base

class git.index.base.IndexFile(repo, file_path=None)

Implements an Index that can be manipulated using a native implementation in order to save git command function calls wherever possible.

It provides custom merging facilities allowing to merge without actually changing your index or your working tree. This way you can perform own test-merges based on the index only without having to deal with the working copy. This is useful in case of partial working trees.

Entries

The index contains an entries dict whose keys are tuples of type IndexEntry to facilitate access.

You may read the entries dict or manipulate it using IndexEntry instance, i.e.:

index.entries[index.entry_key(index_entry_instance)] = index_entry_instance

Make sure you use index.write() once you are done manipulating the index directly before operating on it using the git command

__init__(repo, file_path=None)

Initialize this Index instance, optionally from the given file_path. If no file_path is given, we will be created from the current index file.

If a stream is not given, the stream will be initialized from the current repository’s index on demand.

__module__ = 'git.index.base'
__slots__ = ('repo', 'version', 'entries', '_extension_data', '_file_path')
add(items, force=True, fprogress=<function <lambda>>, path_rewriter=None, write=True, write_extension_data=False)

Add files from the working tree, specific blobs or BaseIndexEntries to the index.

Parameters:
  • items

    Multiple types of items are supported, types can be mixed within one call. Different types imply a different handling. File paths may generally be relative or absolute.

    • path string
      strings denote a relative or absolute path into the repository pointing to an existing file, i.e. CHANGES, lib/myfile.ext, ‘/home/gitrepo/lib/myfile.ext’.

      Absolute paths must start with working tree directory of this index’s repository to be considered valid. For example, if it was initialized with a non-normalized path, like /root/repo/../repo, absolute paths to be added must start with /root/repo/../repo.

      Paths provided like this must exist. When added, they will be written into the object database.

      PathStrings may contain globs, such as ‘lib/__init__*’ or can be directories like ‘lib’, the latter ones will add all the files within the dirctory and subdirectories.

      This equals a straight git-add.

      They are added at stage 0

    • Blob or Submodule object
      Blobs are added as they are assuming a valid mode is set. The file they refer to may or may not exist in the file system, but must be a path relative to our repository.

      If their sha is null ( 40*0 ), their path must exist in the file system relative to the git repository as an object will be created from the data at the path. The handling now very much equals the way string paths are processed, except that the mode you have set will be kept. This allows you to create symlinks by settings the mode respectively and writing the target of the symlink directly into the file. This equals a default Linux-Symlink which is not dereferenced automatically, except that it can be created on filesystems not supporting it as well.

      Please note that globs or directories are not allowed in Blob objects.

      They are added at stage 0

    • BaseIndexEntry or type
      Handling equals the one of Blob objects, but the stage may be explicitly set. Please note that Index Entries require binary sha’s.
  • forceCURRENTLY INEFFECTIVE If True, otherwise ignored or excluded files will be added anyway. As opposed to the git-add command, we enable this flag by default as the API user usually wants the item to be added even though they might be excluded.
  • fprogress – Function with signature f(path, done=False, item=item) called for each path to be added, one time once it is about to be added where done==False and once after it was added where done=True. item is set to the actual item we handle, either a Path or a BaseIndexEntry Please note that the processed path is not guaranteed to be present in the index already as the index is currently being processed.
  • path_rewriter – Function with signature (string) func(BaseIndexEntry) function returning a path for each passed entry which is the path to be actually recorded for the object created from entry.path. This allows you to write an index which is not identical to the layout of the actual files on your hard-disk. If not None and items contain plain paths, these paths will be converted to Entries beforehand and passed to the path_rewriter. Please note that entry.path is relative to the git repository.
  • write – If True, the index will be written once it was altered. Otherwise the changes only exist in memory and are not available to git commands.
  • write_extension_data – If True, extension data will be written back to the index. This can lead to issues in case it is containing the ‘TREE’ extension, which will cause the git commit command to write an old tree, instead of a new one representing the now changed index. This doesn’t matter if you use IndexFile.commit(), which ignores the TREE extension altogether. You should set it to True if you intend to use IndexFile.commit() exclusively while maintaining support for third-party extensions. Besides that, you can usually safely ignore the built-in extensions when using GitPython on repositories that are not handled manually at all. All current built-in extensions are listed here: http://opensource.apple.com/source/Git/Git-26/src/git-htmldocs/technical/index-format.txt
Returns:

List(BaseIndexEntries) representing the entries just actually added.

Raises:

OSError – if a supplied Path did not exist. Please note that BaseIndexEntry Objects that do not have a null sha will be added even if their paths do not exist.

checkout(*args, **kwargs)

Checkout the given paths or all files from the version known to the index into the working tree.

Note:

Be sure you have written pending changes using the write method in case you have altered the enties dictionary directly

Parameters:
  • paths – If None, all paths in the index will be checked out. Otherwise an iterable of relative or absolute paths or a single path pointing to files or directories in the index is expected.
  • force – If True, existing files will be overwritten even if they contain local modifications. If False, these will trigger a CheckoutError.
  • fprogress – see Index.add_ for signature and explanation. The provided progress information will contain None as path and item if no explicit paths are given. Otherwise progress information will be send prior and after a file has been checked out
  • kwargs – Additional arguments to be passed to git-checkout-index
Returns:

iterable yielding paths to files which have been checked out and are guaranteed to match the version stored in the index

Raises:

CheckoutError – If at least one file failed to be checked out. This is a summary, hence it will checkout as many files as it can anyway. If one of files or directories do not exist in the index ( as opposed to the original git command who ignores them ). Raise GitCommandError if error lines could not be parsed - this truly is an exceptional state

Note

The checkout is limited to checking out the files in the index. Files which are not in the index anymore and exist in the working tree will not be deleted. This behaviour is fundamentally different to head.checkout, i.e. if you want git-checkout like behaviour, use head.checkout instead of index.checkout.

commit(message, parent_commits=None, head=True, author=None, committer=None, author_date=None, commit_date=None, skip_hooks=False)

Commit the current default index file, creating a commit object. For more information on the arguments, see tree.commit.

Note:If you have manually altered the .entries member of this instance, don’t forget to write() your changes to disk beforehand. Passing skip_hooks=True is the equivalent of using -n or –no-verify on the command line.
Returns:Commit object representing the new commit
diff(*args, **kwargs)

Diff this index against the working copy or a Tree or Commit object

For a documentation of the parameters and return values, see Diffable.diff

Note:Will only work with indices that represent the default git index as they have not been initialized with a stream.
entries
classmethod entry_key(*entry)
classmethod from_tree(repo, *treeish, **kwargs)

Merge the given treeish revisions into a new index which is returned. The original index will remain unaltered

Parameters:
  • repo – The repository treeish are located in.
  • treeish – One, two or three Tree Objects, Commits or 40 byte hexshas. The result changes according to the amount of trees. If 1 Tree is given, it will just be read into a new index If 2 Trees are given, they will be merged into a new index using a two way merge algorithm. Tree 1 is the ‘current’ tree, tree 2 is the ‘other’ one. It behaves like a fast-forward. If 3 Trees are given, a 3-way merge will be performed with the first tree being the common ancestor of tree 2 and tree 3. Tree 2 is the ‘current’ tree, tree 3 is the ‘other’ one
  • kwargs – Additional arguments passed to git-read-tree
Returns:

New IndexFile instance. It will point to a temporary index location which does not exist anymore. If you intend to write such a merged Index, supply an alternate file_path to its ‘write’ method.

Note:

In the three-way merge case, –aggressive will be specified to automatically resolve more cases in a commonly correct manner. Specify trivial=True as kwarg to override that.

As the underlying git-read-tree command takes into account the current index, it will be temporarily moved out of the way to assure there are no unsuspected interferences.

iter_blobs(predicate=<function <lambda>>)
Returns:Iterator yielding tuples of Blob objects and stages, tuple(stage, Blob)
Parameters:predicate – Function(t) returning True if tuple(stage, Blob) should be yielded by the iterator. A default filter, the BlobFilter, allows you to yield blobs only if they match a given list of paths.
merge_tree(*args, **kwargs)

Merge the given rhs treeish into the current index, possibly taking a common base treeish into account.

As opposed to the from_tree_ method, this allows you to use an already existing tree as the left side of the merge

Parameters:
  • rhs – treeish reference pointing to the ‘other’ side of the merge.
  • base – optional treeish reference pointing to the common base of ‘rhs’ and this index which equals lhs
Returns:

self ( containing the merge and possibly unmerged entries in case of conflicts )

Raises:

GitCommandError – If there is a merge conflict. The error will be raised at the first conflicting path. If you want to have proper merge resolution to be done by yourself, you have to commit the changed index ( or make a valid tree from it ) and retry with a three-way index.from_tree call.

move(*args, **kwargs)

Rename/move the items, whereas the last item is considered the destination of the move operation. If the destination is a file, the first item ( of two ) must be a file as well. If the destination is a directory, it may be preceded by one or more directories or files.

The working tree will be affected in non-bare repositories.

Parma items:

Multiple types of items are supported, please see the ‘remove’ method for reference.

Parameters:
  • skip_errors – If True, errors such as ones resulting from missing source files will be skipped.
  • kwargs – Additional arguments you would like to pass to git-mv, such as dry_run or force.
:return:List(tuple(source_path_string, destination_path_string), ...)
A list of pairs, containing the source file moved as well as its actual destination. Relative to the repository root.
Raises:ValueError – If only one item was given GitCommandError: If git could not handle your request
classmethod new(repo, *tree_sha)

Merge the given treeish revisions into a new index which is returned. This method behaves like git-read-tree –aggressive when doing the merge.

Parameters:
  • repo – The repository treeish are located in.
  • tree_sha – 20 byte or 40 byte tree sha or tree objects
Returns:

New IndexFile instance. Its path will be undefined. If you intend to write such a merged Index, supply an alternate file_path to its ‘write’ method.

path
Returns:Path to the index file we are representing
remove(*args, **kwargs)

Remove the given items from the index and optionally from the working tree as well.

Parameters:
  • items

    Multiple types of items are supported which may be be freely mixed.

    • path string
      Remove the given path at all stages. If it is a directory, you must specify the r=True keyword argument to remove all file entries below it. If absolute paths are given, they will be converted to a path relative to the git repository directory containing the working tree

      The path string may include globs, such as *.c.

    • Blob Object
      Only the path portion is used in this case.
    • BaseIndexEntry or compatible type
      The only relevant information here Yis the path. The stage is ignored.
  • working_tree – If True, the entry will also be removed from the working tree, physically removing the respective file. This may fail if there are uncommited changes in it.
  • kwargs – Additional keyword arguments to be passed to git-rm, such as ‘r’ to allow recursive removal of
Returns:

List(path_string, ...) list of repository relative paths that have been removed effectively. This is interesting to know in case you have provided a directory or globs. Paths are relative to the repository.

repo
reset(*args, **kwargs)

Reset the index to reflect the tree at the given commit. This will not adjust our HEAD reference as opposed to HEAD.reset by default.

Parameters:
  • commit – Revision, Reference or Commit specifying the commit we should represent. If you want to specify a tree only, use IndexFile.from_tree and overwrite the default index.
  • working_tree – If True, the files in the working tree will reflect the changed index. If False, the working tree will not be touched Please note that changes to the working copy will be discarded without warning !
  • head – If True, the head will be set to the given commit. This is False by default, but if True, this method behaves like HEAD.reset.
  • paths – if given as an iterable of absolute or repository-relative paths, only these will be reset to their state at the given commit’ish. The paths need to exist at the commit, otherwise an exception will be raised.
  • kwargs – Additional keyword arguments passed to git-reset

Note

IndexFile.reset, as opposed to HEAD.reset, will not delete anyfiles in order to maintain a consistent working tree. Instead, it will just checkout the files according to their state in the index. If you want git-reset like behaviour, use HEAD.reset instead.

Returns:self
resolve_blobs(iter_blobs)

Resolve the blobs given in blob iterator. This will effectively remove the index entries of the respective path at all non-null stages and add the given blob as new stage null blob.

For each path there may only be one blob, otherwise a ValueError will be raised claiming the path is already at stage 0.

Raises:ValueError – if one of the blobs already existed at stage 0
Returns:self
Note:You will have to write the index manually once you are done, i.e. index.resolve_blobs(blobs).write()
unmerged_blobs()
Returns:Iterator yielding dict(path : list( tuple( stage, Blob, ...))), being a dictionary associating a path in the index with a list containing sorted stage/blob pairs
Note:Blobs that have been removed in one side simply do not exist in the given stage. I.e. a file removed on the ‘other’ branch whose entries are at stage 3 will not have a stage 3 entry.
update()

Reread the contents of our index file, discarding all cached information we might have.

Note:This is a possibly dangerious operations as it will discard your changes to index.entries
Returns:self
version
write(file_path=None, ignore_extension_data=False)

Write the current state to our file path or to the given one

Parameters:
  • file_path – If None, we will write to our stored file path from which we have been initialized. Otherwise we write to the given file path. Please note that this will change the file_path of this index to the one you gave.
  • ignore_extension_data – If True, the TREE type extension data read in the index will not be written to disk. NOTE that no extension data is actually written. Use this if you have altered the index and would like to use git-write-tree afterwards to create a tree representing your written changes. If this data is present in the written index, git-write-tree will instead write the stored/cached tree. Alternatively, use IndexFile.write_tree() to handle this case automatically
Returns:

self

write_tree()

Writes this index to a corresponding Tree object into the repository’s object database and return it.

Returns:

Tree object representing this index

Note:

The tree will be written even if one or more objects the tree refers to does not yet exist in the object database. This could happen if you added Entries to the index directly.

Raises:
exception git.index.base.CheckoutError(message, failed_files, valid_files, failed_reasons)

Thrown if a file could not be checked out from the index as it contained changes.

The .failed_files attribute contains a list of relative paths that failed to be checked out as they contained changes that did not exist in the index.

The .failed_reasons attribute contains a string informing about the actual cause of the issue.

The .valid_files attribute contains a list of relative paths to files that were checked out successfully and hence match the version stored in the index

__init__(message, failed_files, valid_files, failed_reasons)
__module__ = 'git.exc'
__str__()
__weakref__

list of weak references to the object (if defined)

Index.Functions

git.index.fun.write_cache(entries, stream, extension_data=None, ShaStreamCls=<class 'git.util.IndexFileSHA1Writer'>)

Write the cache represented by entries to a stream

Parameters:
  • entriessorted list of entries
  • stream – stream to wrap into the AdapterStreamCls - it is used for final output.
  • ShaStreamCls – Type to use when writing to the stream. It produces a sha while writing to it, before the data is passed on to the wrapped stream
  • extension_data – any kind of data to write as a trailer, it must begin a 4 byte identifier, followed by its size ( 4 bytes )
git.index.fun.read_cache(stream)

Read a cache file from the given stream :return: tuple(version, entries_dict, extension_data, content_sha) * version is the integer version number * entries dict is a dictionary which maps IndexEntry instances to a path at a stage * extension_data is ‘’ or 4 bytes of type + 4 bytes of size + size bytes * content_sha is a 20 byte sha on all cache file contents

git.index.fun.write_tree_from_cache(entries, odb, sl, si=0)

Create a tree from the given sorted list of entries and put the respective trees into the given object database

Parameters:
  • entriessorted list of IndexEntries
  • odb – object database to store the trees in
  • si – start index at which we should start creating subtrees
  • sl – slice indicating the range we should process on the entries list
Returns:

tuple(binsha, list(tree_entry, ...)) a tuple of a sha and a list of tree entries being a tuple of hexsha, mode, name

git.index.fun.entry_key(*entry)
Returns:Key suitable to be used for the index.entries dictionary
Parameters:entry – One instance of type BaseIndexEntry or the path and the stage
git.index.fun.stat_mode_to_index_mode(mode)

Convert the given mode from a stat call to the corresponding index mode and return it

git.index.fun.run_commit_hook(name, index)

Run the commit hook of the given name. Silently ignores hooks that do not exist. :param name: name of hook, like ‘pre-commit’ :param index: IndexFile instance :raises HookExecutionError:

git.index.fun.hook_path(name, git_dir)
Returns:path to the given named hook in the given git repository directory

Index.Types

Module with additional types used by the index

class git.index.typ.BlobFilter(paths)

Predicate to be used by iter_blobs allowing to filter only return blobs which match the given list of directories or files.

The given paths are given relative to the repository.

__call__(stage_blob)
__init__(paths)
Parameters:paths – tuple or list of paths which are either pointing to directories or to files relative to the current repository
__module__ = 'git.index.typ'
__slots__ = 'paths'
paths
class git.index.typ.BaseIndexEntry

Small Brother of an index entry which can be created to describe changes done to the index in which case plenty of additional information is not requried.

As the first 4 data members match exactly to the IndexEntry type, methods expecting a BaseIndexEntry can also handle full IndexEntries even if they use numeric indices for performance reasons.

__dict__ = dict_proxy({'__module__': 'git.index.typ', '__str__': <function __str__>, 'binsha': <property object>, 'mode': <property object>, 'hexsha': <property object>, 'to_blob': <function to_blob>, '__repr__': <function __repr__>, 'path': <property object>, '__dict__': <attribute '__dict__' of 'BaseIndexEntry' objects>, 'flags': <property object>, 'stage': <property object>, '__doc__': 'Small Brother of an index entry which can be created to describe changes\n done to the index in which case plenty of additional information is not requried.\n\n As the first 4 data members match exactly to the IndexEntry type, methods\n expecting a BaseIndexEntry can also handle full IndexEntries even if they\n use numeric indices for performance reasons. ', 'from_blob': <classmethod object>})
__module__ = 'git.index.typ'
__repr__()
__str__()
binsha

binary sha of the blob

flags
Returns:flags stored with this entry
classmethod from_blob(blob, stage=0)
Returns:Fully equipped BaseIndexEntry at the given stage
hexsha

hex version of our sha

mode

File Mode, compatible to stat module constants

path
Returns:our path relative to the repository working tree root
stage

Stage of the entry, either:

  • 0 = default stage
  • 1 = stage before a merge or common ancestor entry in case of a 3 way merge
  • 2 = stage of entries from the ‘left’ side of the merge
  • 3 = stage of entries from the right side of the merge
Note:For more information, see http://www.kernel.org/pub/software/scm/git/docs/git-read-tree.html
to_blob(repo)
Returns:Blob using the information of this index entry
class git.index.typ.IndexEntry

Allows convenient access to IndexEntry data without completely unpacking it.

Attributes usully accessed often are cached in the tuple whereas others are unpacked on demand.

See the properties for a mapping between names and tuple indices.

__module__ = 'git.index.typ'
ctime
Returns:Tuple(int_time_seconds_since_epoch, int_nano_seconds) of the file’s creation time
dev

Device ID

classmethod from_base(base)
Returns:Minimal entry as created from the given BaseIndexEntry instance. Missing values will be set to null-like values
Parameters:base – Instance of type BaseIndexEntry
classmethod from_blob(blob, stage=0)
Returns:Minimal entry resembling the given blob object
gid

Group ID

inode

Inode ID

mtime

See ctime property, but returns modification time

size
Returns:Uncompressed size of the blob
uid

User ID

Index.Util

Module containing index utilities

class git.index.util.TemporaryFileSwap(file_path)

Utility class moving a file to a temporary location within the same directory and moving it back on to where on object deletion.

__del__()
__init__(file_path)
__module__ = 'git.index.util'
__slots__ = ('file_path', 'tmp_file_path')
file_path
tmp_file_path
git.index.util.post_clear_cache(func)

Decorator for functions that alter the index using the git command. This would invalidate our possibly existing entries dictionary which is why it must be deleted to allow it to be lazily reread later.

Note:This decorator will not be required once all functions are implemented natively which in fact is possible, but probably not feasible performance wise.
git.index.util.default_index(func)

Decorator assuring the wrapped method may only run if we are the default repository index. This is as we rely on git commands that operate on that index only.

git.index.util.git_working_dir(func)

Decorator which changes the current working dir to the one of the git repository in order to assure relative paths are handled correctly

GitCmd

class git.cmd.Git(working_dir=None)

The Git class manages communication with the Git binary.

It provides a convenient interface to calling the Git binary, such as in:

g = Git( git_dir )
g.init()                   # calls 'git init' program
rval = g.ls_files()        # calls 'git ls-files' program
Debugging
Set the GIT_PYTHON_TRACE environment variable print each invocation of the command to stdout. Set its value to ‘full’ to see details about the returned values.
class AutoInterrupt(proc, args)

Kill/Interrupt the stored process instance once this instance goes out of scope. It is used to prevent processes piling up in case iterators stop reading. Besides all attributes are wired through to the contained process object.

The wait method was overridden to perform automatic status code checking and possibly raise.

__del__()
__getattr__(attr)
__init__(proc, args)
__module__ = 'git.cmd'
__slots__ = ('proc', 'args')
args
proc
wait(stderr='')

Wait for the process and return its status code.

Parameters:stderr – Previously read value of stderr, in case stderr is already closed.
Warn:may deadlock if output or error pipes are used and not handled separately.
Raises:GitCommandError – if the return status is not 0
class Git.CatFileContentStream(size, stream)

Object representing a sized read-only stream returning the contents of an object. It behaves like a stream, but counts the data read and simulates an empty stream once our sized content region is empty. If not all data is read to the end of the objects’s lifetime, we read the rest to assure the underlying stream continues to work

__del__()
__init__(size, stream)
__iter__()
__module__ = 'git.cmd'
__slots__ = ('_stream', '_nbr', '_size')
next()
read(size=-1)
readline(size=-1)
readlines(size=-1)
Git.GIT_PYTHON_GIT_EXECUTABLE = 'git'
Git.GIT_PYTHON_TRACE = False
Git.USE_SHELL = False
Git.__call__(**kwargs)

Specify command line options to the git executable for a subcommand call

Parameters:kwargs – is a dict of keyword arguments. these arguments are passed as in _call_process but will be passed to the git command rather than the subcommand.
Examples::
git(work_tree=’/tmp’).difftool()
Git.__getattr__(name)

A convenience method as it allows to call the command as if it was an object. :return: Callable object that will execute call _call_process with your arguments.

Git.__getstate__()
Git.__init__(working_dir=None)

Initialize this instance with:

Parameters:working_dir – Git directory we should work in. If None, we always work in the current directory as returned by os.getcwd(). It is meant to be the working tree directory if available, or the .git directory in case of bare repositories.
Git.__module__ = 'git.cmd'
Git.__setstate__(d)
Git.__slots__ = ('_working_dir', 'cat_file_all', 'cat_file_header', '_version_info', '_git_options', '_persistent_git_options', '_environment')
Git.cat_file_all
Git.cat_file_header
Git.clear_cache()

Clear all kinds of internal caches to release resources.

Currently persistent commands will be interrupted.

Returns:self
Git.custom_environment(*args, **kwds)

A context manager around the above update_environment method to restore the environment back to its previous state after operation.

Examples:

with self.custom_environment(GIT_SSH='/bin/ssh_wrapper'):
    repo.remotes.origin.fetch()
Parameters:kwargs – see update_environment
Git.environment()
Git.execute(command, istream=None, with_extended_output=False, with_exceptions=True, as_process=False, output_stream=None, stdout_as_string=True, kill_after_timeout=None, with_stdout=True, universal_newlines=False, shell=None, **subprocess_kwargs)

Handles executing the command on the shell and consumes and returns the returned information (stdout)

Parameters:
  • command – The command argument list to execute. It should be a string, or a sequence of program arguments. The program to execute is the first item in the args sequence or string.
  • istream – Standard input filehandle passed to subprocess.Popen.
  • with_extended_output – Whether to return a (status, stdout, stderr) tuple.
  • with_exceptions – Whether to raise an exception when git returns a non-zero status.
  • as_process – Whether to return the created process instance directly from which streams can be read on demand. This will render with_extended_output and with_exceptions ineffective - the caller will have to deal with the details himself. It is important to note that the process will be placed into an AutoInterrupt wrapper that will interrupt the process once it goes out of scope. If you use the command in iterators, you should pass the whole process instance instead of a single stream.
  • output_stream – If set to a file-like object, data produced by the git command will be output to the given stream directly. This feature only has any effect if as_process is False. Processes will always be created with a pipe due to issues with subprocess. This merely is a workaround as data will be copied from the output pipe to the given output stream directly. Judging from the implementation, you shouldn’t use this flag !
  • stdout_as_string – if False, the commands standard output will be bytes. Otherwise, it will be decoded into a string using the default encoding (usually utf-8). The latter can fail, if the output contains binary data.
  • subprocess_kwargs – Keyword arguments to be passed to subprocess.Popen. Please note that some of the valid kwargs are already set by this method, the ones you specify may not be the same ones.
  • with_stdout – If True, default True, we open stdout on the created process
  • universal_newlines – if True, pipes will be opened as text, and lines are split at all known line endings.
  • shell – Whether to invoke commands through a shell (see Popen(..., shell=True)). It overrides USE_SHELL if it is not None.
  • kill_after_timeout – To specify a timeout in seconds for the git command, after which the process should be killed. This will have no effect if as_process is set to True. It is set to None by default and will let the process run until the timeout is explicitly specified. This feature is not supported on Windows. It’s also worth noting that kill_after_timeout uses SIGKILL, which can have negative side effects on a repository. For example, stale locks in case of git gc could render the repository incapable of accepting changes until the lock is manually removed.
Returns:

  • str(output) if extended_output = False (Default)
  • tuple(int(status), str(stdout), str(stderr)) if extended_output = True

if ouput_stream is True, the stdout value will be your output stream: * output_stream if extended_output = False * tuple(int(status), output_stream, str(stderr)) if extended_output = True

Note git is executed with LC_MESSAGES=”C” to ensure consistent output regardless of system language.

Raises:

GitCommandError

Note:

If you add additional keyword arguments to the signature of this method, you must update the execute_kwargs tuple housed in this module.

Git.get_object_data(ref)

As get_object_header, but returns object data as well :return: (hexsha, type_string, size_as_int,data_string) :note: not threadsafe

Git.get_object_header(ref)

Use this method to quickly examine the type and size of the object behind the given ref.

Note:The method will only suffer from the costs of command invocation once and reuses the command in subsequent calls.
Returns:(hexsha, type_string, size_as_int)
Git.git_exec_name = 'git'
classmethod Git.is_cygwin()
Git.max_chunk_size = 8192
classmethod Git.polish_url(url, is_cygwin=None)
Git.set_persistent_git_options(**kwargs)

Specify command line options to the git executable for subsequent subcommand calls

Parameters:kwargs – is a dict of keyword arguments. these arguments are passed as in _call_process but will be passed to the git command rather than the subcommand.
Git.stream_object_data(ref)

As get_object_header, but returns the data as a stream

Returns:(hexsha, type_string, size_as_int, stream)
Note:This method is not threadsafe, you need one independent Command instance per thread to be safe !
Git.transform_kwarg(name, value, split_single_char_options)
Git.transform_kwargs(split_single_char_options=True, **kwargs)

Transforms Python style kwargs into git command line options.

Git.update_environment(**kwargs)

Set environment variables for future git invocations. Return all changed values in a format that can be passed back into this function to revert the changes:

Examples:

old_env = self.update_environment(PWD='/tmp')
self.update_environment(**old_env)
Parameters:kwargs – environment variables to use for git processes
Returns:dict that maps environment variables to their old values
Git.version_info
Returns:tuple(int, int, int, int) tuple with integers representing the major, minor and additional version numbers as parsed from git version. This value is generated on demand and is cached
Git.working_dir
Returns:Git directory we are working on

Config

Module containing module parser implementation able to properly read and write configuration files

git.config.GitConfigParser

alias of write

class git.config.SectionConstraint(config, section)

Constrains a ConfigParser to only option commands which are constrained to always use the section we have been initialized with.

It supports all ConfigParser methods that operate on an option.

Note:If used as a context manager, will release the wrapped ConfigParser.
__del__()
__enter__()
__exit__(exception_type, exception_value, traceback)
__getattr__(attr)
__init__(config, section)
__module__ = 'git.config'
__slots__ = ('_config', '_section_name')
config

return: Configparser instance we constrain

release()

Equivalent to GitConfigParser.release(), which is called on our underlying parser instance

Diff

class git.diff.Diffable

Common interface for all object that can be diffed against another object of compatible type.

Note:Subclasses require a repo member as it is the case for Object instances, for practical reasons we do not derive from Object.
class Index
__dict__ = dict_proxy({'__dict__': <attribute '__dict__' of 'Index' objects>, '__module__': 'git.diff', '__weakref__': <attribute '__weakref__' of 'Index' objects>, '__doc__': None})
__module__ = 'git.diff'
__weakref__

list of weak references to the object (if defined)

Diffable.__module__ = 'git.diff'
Diffable.__slots__ = ()
Diffable.diff(other=<class 'git.diff.Index'>, paths=None, create_patch=False, **kwargs)

Creates diffs between two items being trees, trees and index or an index and the working tree. It will detect renames automatically.

Parameters:
  • other – Is the item to compare us with. If None, we will be compared to the working tree. If Treeish, it will be compared against the respective tree If Index ( type ), it will be compared against the index. If git.NULL_TREE, it will compare against the empty tree. It defaults to Index to assure the method will not by-default fail on bare repositories.
  • paths – is a list of paths or a single path to limit the diff to. It will only include at least one of the given path or paths.
  • create_patch – If True, the returned Diff contains a detailed patch that if applied makes the self to other. Patches are somewhat costly as blobs have to be read and diffed.
  • kwargs – Additional arguments passed to git-diff, such as R=True to swap both sides of the diff.
Returns:

git.DiffIndex

Note:

On a bare repository, ‘other’ needs to be provided as Index or as as Tree/Commit, or a git command error will occur

class git.diff.DiffIndex

Implements an Index for diffs, allowing a list of Diffs to be queried by the diff properties.

The class improves the diff handling convenience

__dict__ = dict_proxy({'iter_change_type': <function iter_change_type>, '__module__': 'git.diff', 'change_type': ('A', 'D', 'R', 'M'), '__dict__': <attribute '__dict__' of 'DiffIndex' objects>, '__weakref__': <attribute '__weakref__' of 'DiffIndex' objects>, '__doc__': 'Implements an Index for diffs, allowing a list of Diffs to be queried by\n the diff properties.\n\n The class improves the diff handling convenience'})
__module__ = 'git.diff'
__weakref__

list of weak references to the object (if defined)

change_type = ('A', 'D', 'R', 'M')
iter_change_type(change_type)
Returns:iterator yielding Diff instances that match the given change_type
Parameters:change_type

Member of DiffIndex.change_type, namely:

  • ‘A’ for added paths
  • ‘D’ for deleted paths
  • ‘R’ for renamed paths
  • ‘M’ for paths with modified data
class git.diff.Diff(repo, a_rawpath, b_rawpath, a_blob_id, b_blob_id, a_mode, b_mode, new_file, deleted_file, raw_rename_from, raw_rename_to, diff, change_type)

A Diff contains diff information between two Trees.

It contains two sides a and b of the diff, members are prefixed with “a” and “b” respectively to inidcate that.

Diffs keep information about the changed blob objects, the file mode, renames, deletions and new files.

There are a few cases where None has to be expected as member variable value:

New File:

a_mode is None
a_blob is None
a_path is None

Deleted File:

b_mode is None
b_blob is None
b_path is None

Working Tree Blobs

When comparing to working trees, the working tree blob will have a null hexsha as a corresponding object does not yet exist. The mode will be null as well. But the path will be available though. If it is listed in a diff the working tree version of the file must be different to the version in the index or tree, and hence has been modified.
NULL_BIN_SHA = '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
NULL_HEX_SHA = '0000000000000000000000000000000000000000'
__eq__(other)
__hash__()
__init__(repo, a_rawpath, b_rawpath, a_blob_id, b_blob_id, a_mode, b_mode, new_file, deleted_file, raw_rename_from, raw_rename_to, diff, change_type)
__module__ = 'git.diff'
__ne__(other)
__slots__ = ('a_blob', 'b_blob', 'a_mode', 'b_mode', 'a_rawpath', 'b_rawpath', 'new_file', 'deleted_file', 'raw_rename_from', 'raw_rename_to', 'diff', 'change_type')
__str__()
a_blob
a_mode
a_path
a_rawpath
b_blob
b_mode
b_path
b_rawpath
change_type
deleted_file
diff
new_file
raw_rename_from
raw_rename_to
re_header = <_sre.SRE_Pattern object at 0x2ebc0c0>
rename_from
rename_to
renamed
Returns:True if the blob of our diff has been renamed
Note:This property is deprecated, please use renamed_file instead.
renamed_file
Returns:True if the blob of our diff has been renamed
Note:This property is deprecated, please use renamed_file instead.

Exceptions

Module containing all exceptions thrown throughout the git package,

exception git.exc.CacheError

Base for all errors related to the git index, which is called cache internally

__module__ = 'git.exc'
__weakref__

list of weak references to the object (if defined)

exception git.exc.CheckoutError(message, failed_files, valid_files, failed_reasons)

Thrown if a file could not be checked out from the index as it contained changes.

The .failed_files attribute contains a list of relative paths that failed to be checked out as they contained changes that did not exist in the index.

The .failed_reasons attribute contains a string informing about the actual cause of the issue.

The .valid_files attribute contains a list of relative paths to files that were checked out successfully and hence match the version stored in the index

__init__(message, failed_files, valid_files, failed_reasons)
__module__ = 'git.exc'
__str__()
__weakref__

list of weak references to the object (if defined)

exception git.exc.CommandError(command, status=None, stderr=None, stdout=None)

Base class for exceptions thrown at every stage of Popen() execution.

Parameters:command – A non-empty list of argv comprising the command-line.
__init__(command, status=None, stderr=None, stdout=None)
__module__ = 'git.exc'
__unicode__()
__weakref__

list of weak references to the object (if defined)

exception git.exc.GitCommandError(command, status, stderr=None, stdout=None)

Thrown if execution of the git command fails with non-zero status code.

__init__(command, status, stderr=None, stdout=None)
__module__ = 'git.exc'
exception git.exc.GitCommandNotFound(command, cause)

Thrown if we cannot find the git executable in the PATH or at the path given by the GIT_PYTHON_GIT_EXECUTABLE environment variable

__init__(command, cause)
__module__ = 'git.exc'
exception git.exc.HookExecutionError(command, status, stderr=None, stdout=None)

Thrown if a hook exits with a non-zero exit code. It provides access to the exit code and the string returned via standard output

__init__(command, status, stderr=None, stdout=None)
__module__ = 'git.exc'
exception git.exc.InvalidGitRepositoryError

Thrown if the given repository appears to have an invalid format.

__module__ = 'git.exc'
__weakref__

list of weak references to the object (if defined)

exception git.exc.NoSuchPathError

Thrown if a path could not be access by the system.

__module__ = 'git.exc'
__weakref__

list of weak references to the object (if defined)

exception git.exc.RepositoryDirtyError(repo, message)

Thrown whenever an operation on a repository fails as it has uncommited changes that would be overwritten

__init__(repo, message)
__module__ = 'git.exc'
__str__()
__weakref__

list of weak references to the object (if defined)

exception git.exc.UnmergedEntriesError

Thrown if an operation cannot proceed as there are still unmerged entries in the cache

__module__ = 'git.exc'
exception git.exc.WorkTreeRepositoryUnsupported

Thrown to indicate we can’t handle work tree repositories

__module__ = 'git.exc'

Refs.symbolic

class git.refs.symbolic.SymbolicReference(repo, path)

Represents a special case of a reference such that this reference is symbolic. It does not point to a specific commit, but to another Head, which itself specifies a commit.

A typical example for a symbolic reference is HEAD.

__eq__(other)
__hash__()
__init__(repo, path)
__module__ = 'git.refs.symbolic'
__ne__(other)
__repr__()
__slots__ = ('repo', 'path')
__str__()
abspath
commit

Query or set commits directly

classmethod create(repo, path, reference='HEAD', force=False, logmsg=None)

Create a new symbolic reference, hence a reference pointing to another reference.

Parameters:
  • repo – Repository to create the reference in
  • path – full path at which the new symbolic reference is supposed to be created at, i.e. “NEW_HEAD” or “symrefs/my_new_symref”
  • reference – The reference to which the new symbolic reference should point to. If it is a commit’ish, the symbolic ref will be detached.
  • force – if True, force creation even if a symbolic reference with that name already exists. Raise OSError otherwise
  • logmsg – If not None, the message to append to the reflog. Otherwise no reflog entry is written.
Returns:

Newly created symbolic Reference

Raises:

OSError – If a (Symbolic)Reference with the same name but different contents already exists.

Note:

This does not alter the current HEAD, index or Working Tree

classmethod delete(repo, path)

Delete the reference at the given path

Parameters:
  • repo – Repository to delete the reference from
  • path – Short or full path pointing to the reference, i.e. refs/myreference or just “myreference”, hence ‘refs/’ is implied. Alternatively the symbolic reference to be deleted
classmethod dereference_recursive(repo, ref_path)
Returns:hexsha stored in the reference at the given ref_path, recursively dereferencing all intermediate references as required
Parameters:repo – the repository containing the reference at ref_path
classmethod from_path(repo, path)
Parameters:path – full .git-directory-relative path name to the Reference to instantiate
Note:use to_full_path() if you only have a partial path of a known Reference Type
Returns:Instance of type Reference, Head, or Tag depending on the given path
is_detached
Returns:True if we are a detached reference, hence we point to a specific commit instead to another reference
is_remote()
Returns:True if this symbolic reference points to a remote branch
is_valid()
Returns:True if the reference is valid, hence it can be read and points to a valid object or reference.
classmethod iter_items(repo, common_path=None)

Find all refs in the repository

Parameters:
  • repo – is the Repo
  • common_path – Optional keyword argument to the path which is to be shared by all returned Ref objects. Defaults to class specific portion if None assuring that only refs suitable for the actual class are returned.
Returns:

git.SymbolicReference[], each of them is guaranteed to be a symbolic ref which is not detached and pointing to a valid ref

List is lexicographically sorted The returned objects represent actual subclasses, such as Head or TagReference

log()
Returns:RefLog for this reference. Its last entry reflects the latest change applied to this reference

Note

As the log is parsed every time, its recommended to cache it for use instead of calling this method repeatedly. It should be considered read-only.

log_append(oldbinsha, message, newbinsha=None)

Append a logentry to the logfile of this ref

Parameters:
  • oldbinsha – binary sha this ref used to point to
  • message – A message describing the change
  • newbinsha – The sha the ref points to now. If None, our current commit sha will be used
Returns:

added RefLogEntry instance

log_entry(index)
Returns:RefLogEntry at the given index
Parameters:index – python list compatible positive or negative index

Note

This method must read part of the reflog during execution, hence it should be used sparringly, or only if you need just one index. In that case, it will be faster than the log() method

name
Returns:In case of symbolic references, the shortest assumable name is the path itself.
object

Return the object our ref currently refers to

path
ref

Returns the Reference we point to

reference

Returns the Reference we point to

rename(new_path, force=False)

Rename self to a new path

Parameters:
  • new_path – Either a simple name or a full path, i.e. new_name or features/new_name. The prefix refs/ is implied for references and will be set as needed. In case this is a symbolic ref, there is no implied prefix
  • force – If True, the rename will succeed even if a head with the target name already exists. It will be overwritten in that case
Returns:

self

Raises:

OSError – In case a file at path but a different contents already exists

repo
set_commit(commit, logmsg=None)

As set_object, but restricts the type of object to be a Commit

Raises:ValueError – If commit is not a Commit object or doesn’t point to a commit
Returns:self
set_object(object, logmsg=None)

Set the object we point to, possibly dereference our symbolic reference first. If the reference does not exist, it will be created

Parameters:
  • object – a refspec, a SymbolicReference or an Object instance. SymbolicReferences will be dereferenced beforehand to obtain the object they point to
  • logmsg – If not None, the message will be used in the reflog entry to be written. Otherwise the reflog is not altered
Note:

plain SymbolicReferences may not actually point to objects by convention

Returns:

self

set_reference(ref, logmsg=None)

Set ourselves to the given ref. It will stay a symbol if the ref is a Reference. Otherwise an Object, given as Object instance or refspec, is assumed and if valid, will be set which effectively detaches the refererence if it was a purely symbolic one.

Parameters:
  • ref – SymbolicReference instance, Object instance or refspec string Only if the ref is a SymbolicRef instance, we will point to it. Everything else is dereferenced to obtain the actual object.
  • logmsg

    If set to a string, the message will be used in the reflog. Otherwise, a reflog entry is not written for the changed reference. The previous commit of the entry will be the commit we point to now.

    See also: log_append()

Returns:

self

Note:

This symbolic reference will not be dereferenced. For that, see set_object(...)

classmethod to_full_path(path)
Returns:string with a full repository-relative path which can be used to initialize a Reference instance, for instance by using Reference.from_path

Refs.reference

class git.refs.reference.Reference(repo, path, check_path=True)

Represents a named reference to any object. Subclasses may apply restrictions though, i.e. Heads can only point to commits.

__init__(repo, path, check_path=True)

Initialize this instance :param repo: Our parent repository

Parameters:
  • path – Path relative to the .git/ directory pointing to the ref in question, i.e. refs/heads/master
  • check_path – if False, you can provide any path. Otherwise the path must start with the default path prefix of this type.
__module__ = 'git.refs.reference'
__slots__ = ()
__str__()
classmethod iter_items(repo, common_path=None)

Equivalent to SymbolicReference.iter_items, but will return non-detached references as well.

name
Returns:(shortest) Name of this reference - it may contain path components
remote_head
remote_name
set_object(object, logmsg=None)

Special version which checks if the head-log needs an update as well :return: self

Refs.head

class git.refs.head.HEAD(repo, path='HEAD')

Special case of a Symbolic Reference as it represents the repository’s HEAD reference.

__init__(repo, path='HEAD')
__module__ = 'git.refs.head'
__slots__ = ()
orig_head()
Returns:SymbolicReference pointing at the ORIG_HEAD, which is maintained to contain the previous value of HEAD
reset(commit='HEAD', index=True, working_tree=False, paths=None, **kwargs)

Reset our HEAD to the given commit optionally synchronizing the index and working tree. The reference we refer to will be set to commit as well.

Parameters:
  • commit – Commit object, Reference Object or string identifying a revision we should reset HEAD to.
  • index – If True, the index will be set to match the given commit. Otherwise it will not be touched.
  • working_tree – If True, the working tree will be forcefully adjusted to match the given commit, possibly overwriting uncommitted changes without warning. If working_tree is True, index must be true as well
  • paths – Single path or list of paths relative to the git root directory that are to be reset. This allows to partially reset individual files.
  • kwargs – Additional arguments passed to git-reset.
Returns:

self

class git.refs.head.Head(repo, path, check_path=True)

A Head is a named reference to a Commit. Every Head instance contains a name and a Commit object.

Examples:

>>> repo = Repo("/path/to/repo")
>>> head = repo.heads[0]

>>> head.name
'master'

>>> head.commit
<git.Commit "1c09f116cbc2cb4100fb6935bb162daa4723f455">

>>> head.commit.hexsha
'1c09f116cbc2cb4100fb6935bb162daa4723f455'
__dict__ = dict_proxy({'rename': <function rename>, '__module__': 'git.refs.head', '_common_path_default': 'refs/heads', 'config_reader': <function config_reader>, 'k_config_remote': 'remote', 'set_tracking_branch': <function set_tracking_branch>, '_config_parser': <function _config_parser>, 'k_config_remote_ref': 'merge', 'tracking_branch': <function tracking_branch>, '__dict__': <attribute '__dict__' of 'Head' objects>, 'config_writer': <function config_writer>, 'checkout': <function checkout>, '__doc__': 'A Head is a named reference to a Commit. Every Head instance contains a name\n and a Commit object.\n\n Examples::\n\n >>> repo = Repo("/path/to/repo")\n >>> head = repo.heads[0]\n\n >>> head.name\n \'master\'\n\n >>> head.commit\n <git.Commit "1c09f116cbc2cb4100fb6935bb162daa4723f455">\n\n >>> head.commit.hexsha\n \'1c09f116cbc2cb4100fb6935bb162daa4723f455\'', '__weakref__': <attribute '__weakref__' of 'Head' objects>, 'delete': <classmethod object>})
__module__ = 'git.refs.head'
__weakref__

list of weak references to the object (if defined)

checkout(force=False, **kwargs)

Checkout this head by setting the HEAD to this reference, by updating the index to reflect the tree we point to and by updating the working tree to reflect the latest index.

The command will fail if changed working tree files would be overwritten.

Parameters:
  • force – If True, changes to the index and the working tree will be discarded. If False, GitCommandError will be raised in that situation.
  • kwargs – Additional keyword arguments to be passed to git checkout, i.e. b=’new_branch’ to create a new branch at the given spot.
Returns:

The active branch after the checkout operation, usually self unless a new branch has been created. If there is no active branch, as the HEAD is now detached, the HEAD reference will be returned instead.

Note:

By default it is only allowed to checkout heads - everything else will leave the HEAD detached which is allowed and possible, but remains a special state that some tools might not be able to handle.

config_reader()
Returns:A configuration parser instance constrained to only read this instance’s values
config_writer()
Returns:A configuration writer instance with read-and write access to options of this head
classmethod delete(repo, *heads, **kwargs)

Delete the given heads

Parameters:force – If True, the heads will be deleted even if they are not yet merged into the main development stream. Default False
k_config_remote = 'remote'
k_config_remote_ref = 'merge'
rename(new_path, force=False)

Rename self to a new path

Parameters:
  • new_path – Either a simple name or a path, i.e. new_name or features/new_name. The prefix refs/heads is implied
  • force – If True, the rename will succeed even if a head with the target name already exists.
Returns:

self

Note:

respects the ref log as git commands are used

set_tracking_branch(remote_reference)
Configure this branch to track the given remote reference. This will alter
this branch’s configuration accordingly.
Parameters:remote_reference – The remote reference to track or None to untrack any references
Returns:self
tracking_branch()
Returns:The remote_reference we are tracking, or None if we are not a tracking branch

Refs.tag

class git.refs.tag.TagReference(repo, path, check_path=True)

Class representing a lightweight tag reference which either points to a commit ,a tag object or any other object. In the latter case additional information, like the signature or the tag-creator, is available.

This tag object will always point to a commit object, but may carry additional information in a tag object:

tagref = TagReference.list_items(repo)[0]
print(tagref.commit.message)
if tagref.tag is not None:
   print(tagref.tag.message)
__module__ = 'git.refs.tag'
__slots__ = ()
commit
Returns:Commit object the tag ref points to
Raises:ValueError – if the tag points to a tree or blob
classmethod create(repo, path, ref='HEAD', message=None, force=False, **kwargs)

Create a new tag reference.

Parameters:
  • path – The name of the tag, i.e. 1.0 or releases/1.0. The prefix refs/tags is implied
  • ref – A reference to the object you want to tag. It can be a commit, tree or blob.
  • message

    If not None, the message will be used in your tag object. This will also create an additional tag object that allows to obtain that information, i.e.:

    tagref.tag.message
    
  • force – If True, to force creation of a tag even though that tag already exists.
  • kwargs – Additional keyword arguments to be passed to git-tag
Returns:

A new TagReference

classmethod delete(repo, *tags)

Delete the given existing tag or tags

object
Returns:The object our ref currently refers to. Refs can be cached, they will always point to the actual object as it gets re-created on each query
tag
Returns:Tag object this tag ref points to or None in case we are a light weight tag
git.refs.tag.Tag

alias of TagReference

Refs.remote

class git.refs.remote.RemoteReference(repo, path, check_path=True)

Represents a reference pointing to a remote head.

__module__ = 'git.refs.remote'
classmethod create(*args, **kwargs)

Used to disable this method

classmethod delete(repo, *refs, **kwargs)

Delete the given remote references

Note:kwargs are given for comparability with the base class method as we should not narrow the signature.
classmethod iter_items(repo, common_path=None, remote=None)

Iterate remote references, and if given, constrain them to the given remote

Refs.log

class git.refs.log.RefLog(filepath=None)

A reflog contains reflog entries, each of which defines a certain state of the head in question. Custom query methods allow to retrieve log entries by date or by other criteria.

Reflog entries are orded, the first added entry is first in the list, the last entry, i.e. the last change of the head or reference, is last in the list.

__init__(filepath=None)

Initialize this instance with an optional filepath, from which we will initialize our data. The path is also used to write changes back using the write() method

__module__ = 'git.refs.log'
static __new__(filepath=None)
__slots__ = ('_path',)
classmethod append_entry(config_reader, filepath, oldbinsha, newbinsha, message)

Append a new log entry to the revlog at filepath.

Parameters:
  • config_reader – configuration reader of the repository - used to obtain user information. May also be an Actor instance identifying the committer directly. May also be None
  • filepath – full path to the log file
  • oldbinsha – binary sha of the previous commit
  • newbinsha – binary sha of the current commit
  • message – message describing the change to the reference
  • write – If True, the changes will be written right away. Otherwise the change will not be written
Returns:

RefLogEntry objects which was appended to the log

Note:

As we are append-only, concurrent access is not a problem as we do not interfere with readers.

classmethod entry_at(filepath, index)
Returns:

RefLogEntry at the given index

Parameters:
  • filepath – full path to the index file from which to read the entry
  • index – python list compatible index, i.e. it may be negative to specifiy an entry counted from the end of the list
Raises:

IndexError – If the entry didn’t exist

Note

This method is faster as it only parses the entry at index, skipping all other lines. Nonetheless, the whole file has to be read if the index is negative

classmethod from_file(filepath)
Returns:a new RefLog instance containing all entries from the reflog at the given filepath
Parameters:filepath – path to reflog
Raises:ValueError – If the file could not be read or was corrupted in some way
classmethod iter_entries(stream)
Returns:Iterator yielding RefLogEntry instances, one for each line read sfrom the given stream.
Parameters:stream – file-like object containing the revlog in its native format or basestring instance pointing to a file to read
classmethod path(ref)
Returns:string to absolute path at which the reflog of the given ref instance would be found. The path is not guaranteed to point to a valid file though.
Parameters:ref – SymbolicReference instance
to_file(filepath)

Write the contents of the reflog instance to a file at the given filepath. :param filepath: path to file, parent directories are assumed to exist

write()

Write this instance’s data to the file we are originating from :return: self

class git.refs.log.RefLogEntry

Named tuple allowing easy access to the revlog data fields

__module__ = 'git.refs.log'
__repr__()

Representation of ourselves in git reflog format

__slots__ = ()
actor

Actor instance, providing access

format()
Returns:a string suitable to be placed in a reflog file
classmethod from_line(line)
Returns:New RefLogEntry instance from the given revlog line.
Parameters:line – line bytes without trailing newline
Raises:ValueError – If line could not be parsed
message

Message describing the operation that acted on the reference

classmethod new(oldhexsha, newhexsha, actor, time, tz_offset, message)
Returns:New instance of a RefLogEntry
newhexsha

The hexsha to the commit the ref now points to, after the change

oldhexsha

The hexsha to the commit the ref pointed to before the change

time

time as tuple:

  • [0] = int(time)
  • [1] = int(timezone_offset) in time.altzone format

Remote

class git.remote.RemoteProgress

Handler providing an interface to parse progress information emitted by git-push and git-fetch and to dispatch callbacks allowing subclasses to react to the progress.

BEGIN = 1
CHECKING_OUT = 256
COMPRESSING = 8
COUNTING = 4
DONE_TOKEN = 'done.'
END = 2
FINDING_SOURCES = 128
OP_MASK = -4
RECEIVING = 32
RESOLVING = 64
STAGE_MASK = 3
TOKEN_SEPARATOR = ', '
WRITING = 16
__init__()
__module__ = 'git.util'
__slots__ = ('_cur_line', '_seen_ops', 'error_lines', 'other_lines')
error_lines
line_dropped(line)

Called whenever a line could not be understood and was therefore dropped.

new_message_handler()
Returns:a progress handler suitable for handle_process_output(), passing lines on to this Progress handler in a suitable format
other_lines
re_op_absolute = <_sre.SRE_Pattern object>
re_op_relative = <_sre.SRE_Pattern object at 0x2ea4df0>
update(op_code, cur_count, max_count=None, message='')

Called whenever the progress changes

Parameters:
  • op_code

    Integer allowing to be compared against Operation IDs and stage IDs.

    Stage IDs are BEGIN and END. BEGIN will only be set once for each Operation ID as well as END. It may be that BEGIN and END are set at once in case only one progress message was emitted due to the speed of the operation. Between BEGIN and END, none of these flags will be set

    Operation IDs are all held within the OP_MASK. Only one Operation ID will be active per call.

  • cur_count – Current absolute count of items
  • max_count – The maximum count of items we expect. It may be None in case there is no maximum number of items or if it is (yet) unknown.
  • message – In case of the ‘WRITING’ operation, it contains the amount of bytes transferred. It may possibly be used for other purposes as well.

You may read the contents of the current line in self._cur_line

x = 8
class git.remote.PushInfo(flags, local_ref, remote_ref_string, remote, old_commit=None, summary='')

Carries information about the result of a push operation of a single head:

info = remote.push()[0]
info.flags          # bitflags providing more information about the result
info.local_ref      # Reference pointing to the local reference that was pushed
                    # It is None if the ref was deleted.
info.remote_ref_string # path to the remote reference located on the remote side
info.remote_ref # Remote Reference on the local side corresponding to
                # the remote_ref_string. It can be a TagReference as well.
info.old_commit # commit at which the remote_ref was standing before we pushed
                # it to local_ref.commit. Will be None if an error was indicated
info.summary    # summary line providing human readable english text about the push
DELETED = 64
ERROR = 1024
FAST_FORWARD = 256
FORCED_UPDATE = 128
NEW_HEAD = 2
NEW_TAG = 1
NO_MATCH = 4
REJECTED = 8
REMOTE_FAILURE = 32
REMOTE_REJECTED = 16
UP_TO_DATE = 512
__init__(flags, local_ref, remote_ref_string, remote, old_commit=None, summary='')

Initialize a new instance

__module__ = 'git.remote'
__slots__ = ('local_ref', 'remote_ref_string', 'flags', '_old_commit_sha', '_remote', 'summary')
flags
local_ref
old_commit
remote_ref
Returns:Remote Reference or TagReference in the local repository corresponding to the remote_ref_string kept in this instance.
remote_ref_string
summary
x = 10
class git.remote.FetchInfo(ref, flags, note='', old_commit=None, remote_ref_path=None)

Carries information about the results of a fetch operation of a single head:

info = remote.fetch()[0]
info.ref           # Symbolic Reference or RemoteReference to the changed
                   # remote head or FETCH_HEAD
info.flags         # additional flags to be & with enumeration members,
                   # i.e. info.flags & info.REJECTED
                   # is 0 if ref is SymbolicReference
info.note          # additional notes given by git-fetch intended for the user
info.old_commit    # if info.flags & info.FORCED_UPDATE|info.FAST_FORWARD,
                   # field is set to the previous location of ref, otherwise None
info.remote_ref_path # The path from which we fetched on the remote. It's the remote's version of our info.ref
ERROR = 128
FAST_FORWARD = 64
FORCED_UPDATE = 32
HEAD_UPTODATE = 4
NEW_HEAD = 2
NEW_TAG = 1
REJECTED = 16
TAG_UPDATE = 8
__init__(ref, flags, note='', old_commit=None, remote_ref_path=None)

Initialize a new instance

__module__ = 'git.remote'
__slots__ = ('ref', 'old_commit', 'flags', 'note', 'remote_ref_path')
__str__()
commit
Returns:Commit of our remote ref
flags
name
Returns:Name of our remote ref
note
old_commit
re_fetch_result = <_sre.SRE_Pattern object at 0x2ee9600>
ref
remote_ref_path
x = 7
class git.remote.Remote(repo, name)

Provides easy read and write access to a git remote.

Everything not part of this interface is considered an option for the current remote, allowing constructs like remote.pushurl to query the pushurl.

NOTE: When querying configuration, the configuration accessor will be cached to speed up subsequent accesses.

__eq__(other)
__getattr__(attr)

Allows to call this instance like remote.special( *args, **kwargs) to call git-remote special self.name

__hash__()
__init__(repo, name)

Initialize a remote instance

Parameters:
  • repo – The repository we are a remote of
  • name – the name of the remote, i.e. ‘origin’
__module__ = 'git.remote'
__ne__(other)
__repr__()
__slots__ = ('repo', 'name', '_config_reader')
__str__()
classmethod add(repo, name, url, **kwargs)

Create a new remote to the given repository :param repo: Repository instance that is to receive the new remote :param name: Desired name of the remote :param url: URL which corresponds to the remote’s name :param kwargs: Additional arguments to be passed to the git-remote add command :return: New Remote instance :raise GitCommandError: in case an origin with that name already exists

add_url(url, **kwargs)

Adds a new url on current remote (special case of git remote set_url)

This command adds new URLs to a given remote, making it possible to have multiple URLs for a single remote.

Parameters:url – string being the URL to add as an extra remote URL
Returns:self
config_reader
Returns:GitConfigParser compatible object able to read options for only our remote. Hence you may simple type config.get(“pushurl”) to obtain the information
config_writer
Returns:

GitConfigParser compatible object able to write options for this remote.

Note:

You can only own one writer at a time - delete it to release the configuration file and make it usable by others.

To assure consistent results, you should only query options through the writer. Once you are done writing, you are free to use the config reader once again.

classmethod create(repo, name, url, **kwargs)

Create a new remote to the given repository :param repo: Repository instance that is to receive the new remote :param name: Desired name of the remote :param url: URL which corresponds to the remote’s name :param kwargs: Additional arguments to be passed to the git-remote add command :return: New Remote instance :raise GitCommandError: in case an origin with that name already exists

delete_url(url, **kwargs)

Deletes a new url on current remote (special case of git remote set_url)

This command deletes new URLs to a given remote, making it possible to have multiple URLs for a single remote.

Parameters:url – string being the URL to delete from the remote
Returns:self
exists()
Returns:True if this is a valid, existing remote. Valid remotes have an entry in the repository’s configuration
fetch(refspec=None, progress=None, **kwargs)

Fetch the latest changes for this remote

Parameters:
  • refspec

    A “refspec” is used by fetch and push to describe the mapping between remote ref and local ref. They are combined with a colon in the format <src>:<dst>, preceded by an optional plus sign, +. For example: git fetch $URL refs/heads/master:refs/heads/origin means “grab the master branch head from the $URL and store it as my origin branch head”. And git push $URL refs/heads/master:refs/heads/to-upstream means “publish my master branch head as to-upstream branch at $URL”. See also git-push(1).

    Taken from the git manual

    Fetch supports multiple refspecs (as the underlying git-fetch does) - supplying a list rather than a string for ‘refspec’ will make use of this facility.

  • progress – See ‘push’ method
  • kwargs – Additional arguments to be passed to git-fetch
Returns:

IterableList(FetchInfo, ...) list of FetchInfo instances providing detailed information about the fetch results

Note:

As fetch does not provide progress information to non-ttys, we cannot make it available here unfortunately as in the ‘push’ method.

classmethod iter_items(repo)
Returns:Iterator yielding Remote objects of the given repository
name
pull(refspec=None, progress=None, **kwargs)

Pull changes from the given branch, being the same as a fetch followed by a merge of branch with your local branch.

Parameters:
  • refspec – see ‘fetch’ method
  • progress – see ‘push’ method
  • kwargs – Additional arguments to be passed to git-pull
Returns:

Please see ‘fetch’ method

push(refspec=None, progress=None, **kwargs)

Push changes from source branch in refspec to target branch in refspec.

Parameters:
  • refspec – see ‘fetch’ method
  • progress

    Can take one of many value types:

    • None to discard progress information
    • A function (callable) that is called with the progress information.

      Signature: progress(op_code, cur_count, max_count=None, message='').

    Click here for a description of all arguments
    given to the function.
    • An instance of a class derived from git.RemoteProgress that overrides the update() function.
  • kwargs – Additional arguments to be passed to git-push
Note:

No further progress information is returned after push returns.

Returns:

IterableList(PushInfo, ...) iterable list of PushInfo instances, each one informing about an individual head which had been updated on the remote side. If the push contains rejected heads, these will have the PushInfo.ERROR bit set in their flags. If the operation fails completely, the length of the returned IterableList will be null.

refs
Returns:IterableList of RemoteReference objects. It is prefixed, allowing you to omit the remote path portion, i.e.:: remote.refs.master # yields RemoteReference(‘/refs/remotes/origin/master’)
classmethod remove(repo, name)

Remove the remote with the given name :return: the passed remote name to remove

rename(new_name)

Rename self to the given new_name :return: self

repo
classmethod rm(repo, name)

Remove the remote with the given name :return: the passed remote name to remove

set_url(new_url, old_url=None, **kwargs)

Configure URLs on current remote (cf command git remote set_url)

This command manages URLs on the remote.

Parameters:
  • new_url – string being the URL to add as an extra remote URL
  • old_url – when set, replaces this URL with new_url for the remote
Returns:

self

stale_refs
Returns:IterableList RemoteReference objects that do not have a corresponding head in the remote reference anymore as they have been deleted on the remote side, but are still available locally.

The IterableList is prefixed, hence the ‘origin’ must be omitted. See ‘refs’ property for an example.

To make things more complicated, it can be possible for the list to include other kinds of references, for example, tag references, if these are stale as well. This is a fix for the issue described here: https://github.com/gitpython-developers/GitPython/issues/260

update(**kwargs)

Fetch all changes for this remote, including new branches which will be forced in ( in case your local remote branch is not part the new remote branches ancestry anymore ).

Parameters:kwargs – Additional arguments passed to git-remote update
Returns:self
urls
Returns:Iterator yielding all configured URL targets on a remote as strings

Repo.Base

class git.repo.base.Repo(path=None, odbt=<class 'git.db.GitCmdObjectDB'>, search_parent_directories=False)

Represents a git repository and allows you to query references, gather commit information, generate diffs, create and clone repositories query the log.

The following attributes are worth using:

‘working_dir’ is the working directory of the git command, which is the working tree directory if available or the .git directory in case of bare repositories

‘working_tree_dir’ is the working tree directory, but will raise AssertionError if we are a bare repository.

‘git_dir’ is the .git repository directory, which is always set.

DAEMON_EXPORT_FILE = 'git-daemon-export-ok'
GitCommandWrapperType

alias of Git

__del__()
__dict__ = dict_proxy({'create_submodule': <function create_submodule>, '_working_tree_dir': None, 'config_reader': <function config_reader>, 're_author_committer_start': <_sre.SRE_Pattern object>, 're_hexsha_shortened': <_sre.SRE_Pattern object>, 'is_dirty': <function is_dirty>, 'references': <property object>, 'merge_base': <function merge_base>, '_clone': <classmethod object>, '__weakref__': <attribute '__weakref__' of 'Repo' objects>, 'git': None, 'heads': <property object>, 'iter_commits': <function iter_commits>, '__eq__': <function __eq__>, '__enter__': <function __enter__>, 'init': <classmethod object>, 'active_branch': <property object>, '__doc__': "Represents a git repository and allows you to query references,\n gather commit information, generate diffs, create and clone repositories query\n the log.\n\n The following attributes are worth using:\n\n 'working_dir' is the working directory of the git command, which is the working tree\n directory if available or the .git directory in case of bare repositories\n\n 'working_tree_dir' is the working tree directory, but will raise AssertionError\n if we are a bare repository.\n\n 'git_dir' is the .git repository directory, which is always set.", 'head': <property object>, '__exit__': <function __exit__>, 'submodule_update': <function submodule_update>, '__del__': <function __del__>, 'untracked_files': <property object>, 'create_remote': <function create_remote>, 'remotes': <property object>, 'git_dir': None, 'remote': <function remote>, '_get_config_path': <function _get_config_path>, 'GitCommandWrapperType': <class 'git.cmd.Git'>, 'DAEMON_EXPORT_FILE': 'git-daemon-export-ok', 'delete_tag': <function delete_tag>, 'blame_incremental': <function blame_incremental>, 'tree': <function tree>, 'working_tree_dir': <property object>, '__hash__': <function __hash__>, 'rev_parse': <function rev_parse>, '__module__': 'git.repo.base', 're_hexsha_only': <_sre.SRE_Pattern object>, 'submodules': <property object>, 'refs': <property object>, 'iter_submodules': <function iter_submodules>, '__dict__': <attribute '__dict__' of 'Repo' objects>, 'tag': <function tag>, 'bare': <property object>, 'daemon_export': <property object>, 'close': <function close>, 'archive': <function archive>, '__init__': <function __init__>, 'alternates': <property object>, 'index': <property object>, 're_whitespace': <_sre.SRE_Pattern object>, '_set_alternates': <function _set_alternates>, 'delete_head': <function delete_head>, 'working_dir': None, 'has_separate_working_tree': <function has_separate_working_tree>, '_get_untracked_files': <function _get_untracked_files>, '__ne__': <function __ne__>, 're_tab_full_line': <_sre.SRE_Pattern object>, 'description': <property object>, 'create_tag': <function create_tag>, 'tags': <property object>, 'clone': <function clone>, 'create_head': <function create_head>, 'blame': <function blame>, 'submodule': <function submodule>, 'iter_trees': <function iter_trees>, 'config_writer': <function config_writer>, 'branches': <property object>, 'is_ancestor': <function is_ancestor>, 'delete_remote': <function delete_remote>, '__repr__': <function __repr__>, 'commit': <function commit>, '_get_alternates': <function _get_alternates>, 'clone_from': <classmethod object>, 'config_level': ('system', 'user', 'global', 'repository')})
__enter__()
__eq__(rhs)
__exit__(exc_type, exc_value, traceback)
__hash__()
__init__(path=None, odbt=<class 'git.db.GitCmdObjectDB'>, search_parent_directories=False)

Create a new Repo instance

Parameters:
  • path

    the path to either the root git directory or the bare git repo:

    repo = Repo("/Users/mtrier/Development/git-python")
    repo = Repo("/Users/mtrier/Development/git-python.git")
    repo = Repo("~/Development/git-python.git")
    repo = Repo("$REPOSITORIES/Development/git-python.git")
    
    • In Cygwin, path may be a ‘cygdrive/...’ prefixed path.
    • If it evaluates to false, GIT_DIR is used, and if this also evals to false, the current-directory is used.
  • odbt – Object DataBase type - a type which is constructed by providing the directory containing the database objects, i.e. .git/objects. It will be used to access all object data
  • search_parent_directories

    if True, all parent directories will be searched for a valid repo as well.

    Please note that this was the default behaviour in older versions of GitPython, which is considered a bug though.

Raises:
Returns:

git.Repo

__module__ = 'git.repo.base'
__ne__(rhs)
__repr__()
__weakref__

list of weak references to the object (if defined)

active_branch

The name of the currently active branch.

Returns:Head to the active branch
alternates

Retrieve a list of alternates paths or set a list paths to be used as alternates

archive(ostream, treeish=None, prefix=None, **kwargs)

Archive the tree at the given revision.

Parm ostream:

file compatible stream object to which the archive will be written as bytes

Parm treeish:

is the treeish name/id, defaults to active branch

Parm prefix:

is the optional prefix to prepend to each filename in the archive

Parm kwargs:

Additional arguments passed to git-archive

  • Use the ‘format’ argument to define the kind of format. Use specialized ostreams to write any format supported by python.
  • You may specify the special path keyword, which may either be a repository-relative path to a directory or file to place into the archive, or a list or tuple of multipe paths.
Raises:

GitCommandError – in case something went wrong

Returns:

self

bare
Returns:True if the repository is bare
blame(rev, file, incremental=False, **kwargs)

The blame information for the given file at the given revision.

Parm rev:revision specifier, see git-rev-parse for viable options.
Returns:list: [git.Commit, list: [<line>]] A list of tuples associating a Commit object with a list of lines that changed within the given commit. The Commit objects will be given in order of appearance.
blame_incremental(rev, file, **kwargs)

Iterator for blame information for the given file at the given revision.

Unlike .blame(), this does not return the actual file’s contents, only a stream of BlameEntry tuples.

Parm rev:revision specifier, see git-rev-parse for viable options.
Returns:lazy iterator of BlameEntry tuples, where the commit indicates the commit to blame for the line, and range indicates a span of line numbers in the resulting file.

If you combine all line number ranges outputted by this command, you should get a continuous range spanning all line numbers in the file.

branches

A list of Head objects representing the branch heads in this repo

Returns:git.IterableList(Head, ...)
clone(path, progress=None, **kwargs)

Create a clone from this repository.

Parameters:
  • path – is the full path of the new repo (traditionally ends with ./<name>.git).
  • progress – See ‘git.remote.Remote.push’.
  • kwargs
    • odbt = ObjectDatabase Type, allowing to determine the object database implementation used by the returned Repo instance
    • All remaining keyword arguments are given to the git-clone command
Returns:

git.Repo (the newly cloned repo)

classmethod clone_from(url, to_path, progress=None, env=None, **kwargs)

Create a clone from the given URL

Parameters:
Returns:

Repo instance pointing to the cloned directory

close()
commit(rev=None)

The Commit object for the specified revision :param rev: revision specifier, see git-rev-parse for viable options. :return: git.Commit

config_level = ('system', 'user', 'global', 'repository')
config_reader(config_level=None)
Returns:GitConfigParser allowing to read the full git configuration, but not to write it

The configuration will include values from the system, user and repository configuration files.

Parameters:config_level – For possible values, see config_writer method If None, all applicable levels will be used. Specify a level in case you know which exact file you whish to read to prevent reading multiple files for instance
Note:On windows, system configuration cannot currently be read as the path is unknown, instead the global path will be used.
config_writer(config_level='repository')
Returns:GitConfigParser allowing to write values of the specified configuration file level. Config writers should be retrieved, used to change the configuration, and written right away as they will lock the configuration file in question and prevent other’s to write it.
Parameters:config_level – One of the following values system = sytem wide configuration file global = user level configuration file repository = configuration file for this repostory only
create_head(path, commit='HEAD', force=False, logmsg=None)

Create a new head within the repository. For more documentation, please see the Head.create method.

Returns:newly created Head Reference
create_remote(name, url, **kwargs)

Create a new remote.

For more information, please see the documentation of the Remote.create methods

Returns:Remote reference
create_submodule(*args, **kwargs)

Create a new submodule

Note:See the documentation of Submodule.add for a description of the applicable parameters
Returns:created submodules
create_tag(path, ref='HEAD', message=None, force=False, **kwargs)

Create a new tag reference. For more documentation, please see the TagReference.create method.

Returns:TagReference object
daemon_export

If True, git-daemon may export this repository

delete_head(*heads, **kwargs)

Delete the given heads

Parameters:kwargs – Additional keyword arguments to be passed to git-branch
delete_remote(remote)

Delete the given remote.

delete_tag(*tags)

Delete the given tag references

description

the project’s description

git = None
git_dir = None
has_separate_working_tree()
Returns:True if our git_dir is not at the root of our working_tree_dir, but a .git file with a platform agnositic symbolic link. Our git_dir will be whereever the .git file points to
Note:bare repositories will always return False here
head
Returns:HEAD Object pointing to the current head reference
heads

A list of Head objects representing the branch heads in this repo

Returns:git.IterableList(Head, ...)
index
Returns:IndexFile representing this repository’s index.
Note:This property can be expensive, as the returned IndexFile will be reinitialized. It’s recommended to re-use the object.
classmethod init(path=None, mkdir=True, odbt=<class 'git.db.GitCmdObjectDB'>, **kwargs)

Initialize a git repository at the given path if specified

Parameters:
  • path – is the full path to the repo (traditionally ends with /<name>.git) or None in which case the repository will be created in the current working directory
  • odbt – Object DataBase type - a type which is constructed by providing the directory containing the database objects, i.e. .git/objects. It will be used to access all object data
Parm mkdir:

if specified will create the repository directory if it doesn’t already exists. Creates the directory with a mode=0755. Only effective if a path is explicitly given

Parm kwargs:

keyword arguments serving as additional options to the git-init command

Returns:

git.Repo (the newly created repo)

is_ancestor(ancestor_rev, rev)

Check if a commit is an ancestor of another

Parameters:
  • ancestor_rev – Rev which should be an ancestor
  • rev – Rev to test against ancestor_rev
Returns:

True, ancestor_rev is an accestor to rev.

is_dirty(index=True, working_tree=True, untracked_files=False, submodules=True, path=None)
Returns:True, the repository is considered dirty. By default it will react like a git-status without untracked files, hence it is dirty if the index or the working copy have changes.
iter_commits(rev=None, paths='', **kwargs)

A list of Commit objects representing the history of a given ref/commit

Parm rev:revision specifier, see git-rev-parse for viable options. If None, the active branch will be used.
Parm paths:is an optional path or a list of paths to limit the returned commits to Commits that do not contain that path or the paths will not be returned.
Parm kwargs:Arguments to be passed to git-rev-list - common ones are max_count and skip
Note:to receive only commits between two named revisions, use the “revA...revB” revision specifier
Returns:git.Commit[]
iter_submodules(*args, **kwargs)

An iterator yielding Submodule instances, see Traversable interface for a description of args and kwargs :return: Iterator

iter_trees(*args, **kwargs)
Returns:Iterator yielding Tree objects
Note:Takes all arguments known to iter_commits method
merge_base(*rev, **kwargs)

Find the closest common ancestor for the given revision (e.g. Commits, Tags, References, etc)

Parameters:
  • rev – At least two revs to find the common ancestor for.
  • kwargs – Additional arguments to be passed to the repo.git.merge_base() command which does all the work.
Returns:

A list of Commit objects. If –all was not specified as kwarg, the list will have at max one Commit, or is empty if no common merge base exists.

Raises:

ValueError – If not at least two revs are provided

re_author_committer_start = <_sre.SRE_Pattern object>
re_hexsha_only = <_sre.SRE_Pattern object>
re_hexsha_shortened = <_sre.SRE_Pattern object>
re_tab_full_line = <_sre.SRE_Pattern object>
re_whitespace = <_sre.SRE_Pattern object>
references

A list of Reference objects representing tags, heads and remote references.

Returns:IterableList(Reference, ...)
refs

A list of Reference objects representing tags, heads and remote references.

Returns:IterableList(Reference, ...)
remote(name='origin')
Returns:Remote with the specified name
Raises:ValueError – if no remote with such a name exists
remotes

A list of Remote objects allowing to access and manipulate remotes :return: git.IterableList(Remote, ...)

rev_parse(repo, rev)
Returns:

Object at the given revision, either Commit, Tag, Tree or Blob

Parameters:

rev – git-rev-parse compatible revision specification as string, please see http://www.kernel.org/pub/software/scm/git/docs/git-rev-parse.html for details

Raises:
  • BadObject – if the given revision could not be found
  • ValueError – If rev couldn’t be parsed
  • IndexError – If invalid reflog index is specified
submodule(name)
Returns:Submodule with the given name
Raises:ValueError – If no such submodule exists
submodule_update(*args, **kwargs)

Update the submodules, keeping the repository consistent as it will take the previous state into consideration. For more information, please see the documentation of RootModule.update

submodules
Returns:git.IterableList(Submodule, ...) of direct submodules available from the current head
tag(path)
Returns:TagReference Object, reference pointing to a Commit or Tag
Parameters:path – path to the tag reference, i.e. 0.1.5 or tags/0.1.5
tags

A list of Tag objects that are available in this repo :return: git.IterableList(TagReference, ...)

tree(rev=None)

The Tree object for the given treeish revision Examples:

repo.tree(repo.heads[0])
Parameters:rev – is a revision pointing to a Treeish ( being a commit or tree )
Returns:git.Tree
Note:If you need a non-root level tree, find it by iterating the root tree. Otherwise it cannot know about its path relative to the repository root and subsequent operations might have unexpected results.
untracked_files
Returns:list(str,...)

Files currently untracked as they have not been staged yet. Paths are relative to the current working directory of the git command.

Note:ignored files will not appear here, i.e. files mentioned in .gitignore
Note:This property is expensive, as no cache is involved. To process the result, please consider caching it yourself.
working_dir = None
working_tree_dir
Returns:The working tree directory of our git repository. If this is a bare repository, None is returned.

Repo.Functions

Package with general repository related functions

git.repo.fun.rev_parse(repo, rev)
Returns:

Object at the given revision, either Commit, Tag, Tree or Blob

Parameters:

rev – git-rev-parse compatible revision specification as string, please see http://www.kernel.org/pub/software/scm/git/docs/git-rev-parse.html for details

Raises:
  • BadObject – if the given revision could not be found
  • ValueError – If rev couldn’t be parsed
  • IndexError – If invalid reflog index is specified
git.repo.fun.is_git_dir(d)

This is taken from the git setup.c:is_git_directory function.

@throws WorkTreeRepositoryUnsupported if it sees a worktree directory. It’s quite hacky to do that here,
but at least clearly indicates that we don’t support it. There is the unlikely danger to throw if we see directories which just look like a worktree dir, but are none.
git.repo.fun.touch(filename)
git.repo.fun.find_submodule_git_dir(d)

Search for a submodule repo.

git.repo.fun.name_to_object(repo, name, return_ref=False)
Returns:object specified by the given name, hexshas ( short and long ) as well as references are supported
Parameters:return_ref – if name specifies a reference, we will return the reference instead of the object. Otherwise it will raise BadObject or BadName
git.repo.fun.short_to_long(odb, hexsha)
Returns:long hexadecimal sha1 from the given less-than-40 byte hexsha or None if no candidate could be found.
Parameters:hexsha – hexsha with less than 40 byte
git.repo.fun.deref_tag(tag)

Recursively dereference a tag and return the resulting object

git.repo.fun.to_commit(obj)

Convert the given object to a commit if possible and return it

Util

git.util.stream_copy(source, destination, chunk_size=524288)

Copy all data from the source stream into the destination stream in chunks of size chunk_size

Returns:amount of bytes written
git.util.join_path(a, *p)

Join path tokens together similar to osp.join, but always use ‘/’ instead of possibly ‘’ on windows.

git.util.to_native_path_linux(path)
git.util.join_path_native(a, *p)
As join path, but makes sure an OS native path is returned. This is only
needed to play it safe on my dear windows and to assure nice paths that only use ‘’
class git.util.Stats(total, files)

Represents stat information as presented by git at the end of a merge. It is created from the output of a diff operation.

Example:

c = Commit( sha1 )
s = c.stats
s.total         # full-stat-dict
s.files         # dict( filepath : stat-dict )

stat-dict

A dictionary with the following keys and values:

deletions = number of deleted lines as int
insertions = number of inserted lines as int
lines = total number of lines changed as int, or deletions + insertions

full-stat-dict

In addition to the items in the stat-dict, it features additional information:

files = number of changed files as int
__init__(total, files)
__module__ = 'git.util'
__slots__ = ('total', 'files')
files
total
class git.util.IndexFileSHA1Writer(f)

Wrapper around a file-like object that remembers the SHA1 of the data written to it. It will write a sha when the stream is closed or if the asked for explicitly using write_sha.

Only useful to the indexfile

Note:Based on the dulwich project
__init__(f)
__module__ = 'git.util'
__slots__ = ('f', 'sha1')
close()
f
sha1
tell()
write(data)
write_sha()
class git.util.Iterable

Defines an interface for iterable items which is to assure a uniform way to retrieve and iterate items within the git repository

__module__ = 'git.util'
__slots__ = ()
classmethod iter_items(repo, *args, **kwargs)

For more information about the arguments, see list_items :return: iterator yielding Items

classmethod list_items(repo, *args, **kwargs)

Find all items of this type - subclasses can specify args and kwargs differently. If no args are given, subclasses are obliged to return all items if no additional arguments arg given.

Note:Favor the iter_items method as it will

:return:list(Item,...) list of item instances

class git.util.IterableList(id_attr, prefix='')

List of iterable objects allowing to query an object by id or by named index:

heads = repo.heads
heads.master
heads['master']
heads[0]

It requires an id_attribute name to be set which will be queried from its contained items to have a means for comparison.

A prefix can be specified which is to be used in case the id returned by the items always contains a prefix that does not matter to the user, so it can be left out.

__contains__(attr)
__delitem__(index)
__getattr__(attr)
__getitem__(index)
__init__(id_attr, prefix='')
__module__ = 'git.util'
static __new__(id_attr, prefix='')
__slots__ = ('_id_attr', '_prefix')
class git.util.BlockingLockFile(file_path, check_interval_s=0.3, max_block_time_s=9223372036854775807)

The lock file will block until a lock could be obtained, or fail after a specified timeout.

Note:If the directory containing the lock was removed, an exception will be raised during the blocking period, preventing hangs as the lock can never be obtained.
__init__(file_path, check_interval_s=0.3, max_block_time_s=9223372036854775807)

Configure the instance

Parm check_interval_s:
 Period of time to sleep until the lock is checked the next time. By default, it waits a nearly unlimited time
Parm max_block_time_s:
 Maximum amount of seconds we may lock
__module__ = 'git.util'
__slots__ = ('_check_interval', '_max_block_time')
class git.util.LockFile(file_path)

Provides methods to obtain, check for, and release a file based lock which should be used to handle concurrent access to the same file.

As we are a utility class to be derived from, we only use protected methods.

Locks will automatically be released on destruction

__del__()
__init__(file_path)
__module__ = 'git.util'
__slots__ = ('_file_path', '_owns_lock')
class git.util.Actor(name, email)

Actors hold information about a person acting on the repository. They can be committers and authors or anything with a name and an email as mentioned in the git log entries.

__eq__(other)
__hash__()
__init__(name, email)
__module__ = 'git.util'
__ne__(other)
__repr__()
__slots__ = ('name', 'email')
__str__()
classmethod author(config_reader=None)

Same as committer(), but defines the main author. It may be specified in the environment, but defaults to the committer

classmethod committer(config_reader=None)
Returns:Actor instance corresponding to the configured committer. It behaves similar to the git implementation, such that the environment will override configuration values of config_reader. If no value is set at all, it will be generated
Parameters:config_reader – ConfigReader to use to retrieve the values from in case they are not set in the environment
conf_email = 'email'
conf_name = 'name'
email
env_author_email = 'GIT_AUTHOR_EMAIL'
env_author_name = 'GIT_AUTHOR_NAME'
env_committer_email = 'GIT_COMMITTER_EMAIL'
env_committer_name = 'GIT_COMMITTER_NAME'
name
name_email_regex = <_sre.SRE_Pattern object>
name_only_regex = <_sre.SRE_Pattern object>
git.util.get_user_id()
Returns:string identifying the currently active system user as name@node
git.util.assure_directory_exists(path, is_file=False)

Assure that the directory pointed to by path exists.

Parameters:is_file – If True, path is assumed to be a file and handled correctly. Otherwise it must be a directory
Returns:True if the directory was created, False if it already existed
class git.util.RemoteProgress

Handler providing an interface to parse progress information emitted by git-push and git-fetch and to dispatch callbacks allowing subclasses to react to the progress.

BEGIN = 1
CHECKING_OUT = 256
COMPRESSING = 8
COUNTING = 4
DONE_TOKEN = 'done.'
END = 2
FINDING_SOURCES = 128
OP_MASK = -4
RECEIVING = 32
RESOLVING = 64
STAGE_MASK = 3
TOKEN_SEPARATOR = ', '
WRITING = 16
__init__()
__module__ = 'git.util'
__slots__ = ('_cur_line', '_seen_ops', 'error_lines', 'other_lines')
error_lines
line_dropped(line)

Called whenever a line could not be understood and was therefore dropped.

new_message_handler()
Returns:a progress handler suitable for handle_process_output(), passing lines on to this Progress handler in a suitable format
other_lines
re_op_absolute = <_sre.SRE_Pattern object>
re_op_relative = <_sre.SRE_Pattern object at 0x2ea4df0>
update(op_code, cur_count, max_count=None, message='')

Called whenever the progress changes

Parameters:
  • op_code

    Integer allowing to be compared against Operation IDs and stage IDs.

    Stage IDs are BEGIN and END. BEGIN will only be set once for each Operation ID as well as END. It may be that BEGIN and END are set at once in case only one progress message was emitted due to the speed of the operation. Between BEGIN and END, none of these flags will be set

    Operation IDs are all held within the OP_MASK. Only one Operation ID will be active per call.

  • cur_count – Current absolute count of items
  • max_count – The maximum count of items we expect. It may be None in case there is no maximum number of items or if it is (yet) unknown.
  • message – In case of the ‘WRITING’ operation, it contains the amount of bytes transferred. It may possibly be used for other purposes as well.

You may read the contents of the current line in self._cur_line

x = 8
class git.util.CallableRemoteProgress(fn)

An implementation forwarding updates to any callable

__init__(fn)
__module__ = 'git.util'
__slots__ = '_callable'
update(*args, **kwargs)
git.util.rmtree(path)

Remove the given recursively.

Note:we use shutil rmtree but adjust its behaviour to see whether files that couldn’t be deleted are read-only. Windows will not remove them in that case
git.util.unbare_repo(func)

Methods with this decorator raise InvalidGitRepositoryError if they encounter a bare repository

git.util.HIDE_WINDOWS_KNOWN_ERRORS = False

We need an easy way to see if Appveyor TCs start failing, so the errors marked with this var are considered “acknowledged” ones, awaiting remedy, till then, we wish to hide them.