libvcs.cmd.git

For git(1).

Compare to: fabtools.git, salt.modules.git, ansible.builtin.git

Managers and Commands

libvcs provides Managers and Commands for git subcommands:

  • Managers (git.branches, git.tags, etc.) let you traverse repository entities intuitively with ORM-like filtering via QueryList

  • Commands are contextual ways to run git commands against a specific target entity

Git instance
├── branches: GitBranchManager
│   ├── ls() -> QueryList[GitBranchCmd]
│   ├── get() -> GitBranchCmd
│   └── create()
├── tags: GitTagManager
├── remotes: GitRemoteManager
├── stashes: GitStashManager
├── worktrees: GitWorktreeManager
├── notes: GitNotesManager
├── submodules: GitSubmoduleManager
└── reflog: GitReflogManager

Quick Example

from libvcs.cmd.git import Git

git = Git(path='/path/to/repo')

# List all branches
branches = git.branches.ls()

# Filter to remote branches only
remote_branches = git.branches.ls(remotes=True)

# Get a specific branch and rename it
branch = git.branches.get(branch_name='old-name')
branch.rename('new-name')

# Create and manage tags
git.tags.create(name='v1.0.0', message='Release 1.0')
tag = git.tags.get(tag_name='v1.0.0')
tag.delete()

Run git commands directly against a local git repo.

class libvcs.cmd.git.Git[source]

Bases: object

Run commands directly on a git repository.

Lite, typed, pythonic wrapper for git(1).

Parameters:

path (str | PathLike[str]) – Operates as PATH in the corresponding git subcommand.

Examples

>>> git = Git(path=example_git_repo.path)
>>> git
<Git path=...>

Subcommands:

>>> git.remotes.show()
'origin'
>>> git.remotes.add(
...     name='my_remote', url=f'file:///dev/null'
... )
''
>>> git.remotes.show()
'my_remote\norigin'
>>> git.stash.save(message="Message")
'No local changes to save'
>>> git.submodule.init()
''

# Additional tests

>>> git.remotes.get(remote_name='my_remote').remove()
''
>>> git.remotes.show()
'origin'
>>> git.stash.ls()
''
>>> git.stashes.ls()
[]
>>> git.tags.create(name='v1.0.0', message='Version 1.0.0')
''
>>> any(t.tag_name == 'v1.0.0' for t in git.tags.ls())
True
__init__(*, path, progress_callback=None)[source]

Lite, typed, pythonic wrapper for git(1).

Parameters:
Return type:

None

Examples

>>> git = Git(path=example_git_repo.path)
>>> git
<Git path=...>

Subcommands:

>>> git.remotes.show()
'origin'
>>> git.remotes.add(
...     name='my_remote', url=f'file:///dev/null'
... )
''
>>> git.remotes.show()
'my_remote\norigin'
>>> git.stash.save(message="Message")
'No local changes to save'
>>> git.submodule.init()
''

# Additional tests

>>> git.remotes.get(remote_name='my_remote').remove()
''
>>> git.remotes.show()
'origin'
>>> git.stash.ls()
''
>>> git.stashes.ls()
[]
>>> git.tags.create(name='v1.0.0', message='Version 1.0.0')
''
>>> any(t.tag_name == 'v1.0.0' for t in git.tags.ls())
True
path: Path

Directory to check out

progress_callback: ProgressCallbackProtocol | None = None
submodule: GitSubmoduleCmd
submodules: GitSubmoduleManager
remotes: GitRemoteManager
stash: GitStashCmd
stashes: GitStashManager
branches: GitBranchManager
tags: GitTagManager
worktrees: GitWorktreeManager
notes: GitNotesManager
reflog: GitReflogManager
run(args, *, version=None, _help=None, html_path=None, man_path=None, info_path=None, C=None, cwd=None, git_dir=None, work_tree=None, namespace=None, super_prefix=None, exec_path=None, bare=None, no_replace_objects=None, literal_pathspecs=None, global_pathspecs=None, noglob_pathspecs=None, icase_pathspecs=None, no_optional_locks=None, config=None, config_env=None, log_in_real_time=False, **kwargs)[source]

Run a command for this git repository.

Passing None to a subcommand option, the flag won’t be passed unless otherwise stated.

git help and git help [cmd]

Wraps git’s Options.

Parameters:
Return type:

str

Examples

>>> git = Git(path=tmp_path)
>>> git.run(['help'])
"usage: git [...--version] [...--help] [-C <path>]..."
clone(*, url, separate_git_dir=None, template=None, depth=None, branch=None, origin=None, upload_pack=None, shallow_since=None, shallow_exclude=None, reference=None, reference_if_able=None, server_option=None, jobs=None, force=None, local=None, _all=None, no_hardlinks=None, hardlinks=None, shared=None, progress=None, no_checkout=None, no_reject_shallow=None, reject_shallow=None, sparse=None, shallow_submodules=None, no_shallow_submodules=None, remote_submodules=None, no_remote_submodules=None, verbose=None, quiet=None, config=None, log_in_real_time=False, check_returncode=None, make_parents=True, **kwargs)[source]

Clone a working copy from an git repo.

Wraps git clone.

Parameters:
  • url (str)

  • directory (str)

  • separate_git_dir (StrOrBytesPath) – Separate repository (.git/ ) from working tree

  • force (bool, optional) – force operation to run

  • make_parents (bool, default: True) – Creates checkout directory (:attr:`self.path) if it doesn’t already exist.

  • template (str | None)

  • depth (int | None)

  • branch (str | None)

  • origin (str | None)

  • upload_pack (str | None)

  • shallow_since (str | None)

  • shallow_exclude (str | None)

  • reference (str | None)

  • reference_if_able (str | None)

  • server_option (str | None)

  • jobs (str | None)

  • local (bool | None)

  • _all (bool | None)

  • no_hardlinks (bool | None)

  • hardlinks (bool | None)

  • shared (bool | None)

  • progress (bool | None)

  • no_checkout (bool | None)

  • no_reject_shallow (bool | None)

  • reject_shallow (bool | None)

  • sparse (bool | None)

  • shallow_submodules (bool | None)

  • no_shallow_submodules (bool | None)

  • remote_submodules (bool | None)

  • no_remote_submodules (bool | None)

  • verbose (bool | None)

  • quiet (bool | None)

  • config (dict[str, Any] | None)

  • log_in_real_time (bool)

  • check_returncode (bool | None)

  • kwargs (Any)

Return type:

str

Examples

>>> git = Git(path=tmp_path)
>>> git_remote_repo = create_git_remote_repo()
>>> git.clone(url=f'file://{git_remote_repo}')
''
>>> git.path.exists()
True
fetch(*, reftag=None, deepen=None, depth=None, upload_pack=None, shallow_since=None, shallow_exclude=None, negotiation_tip=None, jobs=None, server_option=None, recurse_submodules=None, recurse_submodules_default=None, submodule_prefix=None, _all=None, force=None, keep=None, multiple=None, dry_run=None, append=None, atomic=None, ipv4=None, ipv6=None, progress=None, quiet=None, verbose=None, unshallow=None, update_shallow=None, negotiate_tip=None, no_write_fetch_head=None, write_fetch_head=None, no_auto_maintenance=None, auto_maintenance=None, no_write_commit_graph=None, write_commit_graph=None, prefetch=None, prune=None, prune_tags=None, no_tags=None, tags=None, no_recurse_submodules=None, set_upstream=None, update_head_ok=None, show_forced_updates=None, no_show_forced_updates=None, negotiate_only=None, check_returncode=None, **kwargs)[source]

Download from repo. Wraps git fetch.

Return type:

str

Parameters:
  • reftag (Any | None)

  • deepen (str | None)

  • depth (str | None)

  • upload_pack (str | None)

  • shallow_since (str | None)

  • shallow_exclude (str | None)

  • negotiation_tip (str | None)

  • jobs (str | None)

  • server_option (str | None)

  • recurse_submodules (bool | Literal['yes', 'on-demand', 'no'] | None)

  • recurse_submodules_default (bool | Literal['yes', 'on-demand'] | None)

  • submodule_prefix (str | bytes | PathLike[str] | PathLike[bytes] | None)

  • _all (bool | None)

  • force (bool | None)

  • keep (bool | None)

  • multiple (bool | None)

  • dry_run (bool | None)

  • append (bool | None)

  • atomic (bool | None)

  • ipv4 (bool | None)

  • ipv6 (bool | None)

  • progress (bool | None)

  • quiet (bool | None)

  • verbose (bool | None)

  • unshallow (bool | None)

  • update_shallow (bool | None)

  • negotiate_tip (bool | None)

  • no_write_fetch_head (bool | None)

  • write_fetch_head (bool | None)

  • no_auto_maintenance (bool | None)

  • auto_maintenance (bool | None)

  • no_write_commit_graph (bool | None)

  • write_commit_graph (bool | None)

  • prefetch (bool | None)

  • prune (bool | None)

  • prune_tags (bool | None)

  • no_tags (bool | None)

  • tags (bool | None)

  • no_recurse_submodules (bool | None)

  • set_upstream (bool | None)

  • update_head_ok (bool | None)

  • show_forced_updates (bool | None)

  • no_show_forced_updates (bool | None)

  • negotiate_only (bool | None)

  • check_returncode (bool | None)

  • kwargs (Any)

Examples

>>> git = Git(path=example_git_repo.path)
>>> git_remote_repo = create_git_remote_repo()
>>> git.fetch()
''
>>> git = Git(path=example_git_repo.path)
>>> git_remote_repo = create_git_remote_repo()
>>> git.fetch(reftag=f'file://{git_remote_repo}')
''
>>> git.path.exists()
True
rebase(*, upstream=None, onto=None, branch=None, apply=None, merge=None, quiet=None, verbose=None, stat=None, no_stat=None, verify=None, no_verify=None, fork_point=None, no_fork_point=None, whitespace=None, ignore_whitespace=None, commit_date_is_author_date=None, ignore_date=None, root=None, autostash=None, no_autostash=None, autosquash=None, no_autosquash=None, reschedule_failed_exec=None, no_reschedule_failed_exec=None, context=None, rerere_autoupdate=None, no_rerere_autoupdate=None, keep_empty=None, no_keep_empty=None, reapply_cherry_picks=None, no_reapply_cherry_picks=None, allow_empty_message=None, signoff=None, keep_base=None, strategy=None, strategy_option=None, _exec=None, gpg_sign=None, no_gpg_sign=None, empty=None, rebase_merges=None, interactive=None, edit_todo=None, skip=None, show_current_patch=None, abort=None, _quit=None, check_returncode=None, **kwargs)[source]

Reapply commit on top of another tip.

Wraps git rebase.

Parameters:
  • continue (bool) – Accepted via kwargs

  • upstream (str | None)

  • onto (str | None)

  • branch (str | None)

  • apply (bool | None)

  • merge (bool | None)

  • quiet (bool | None)

  • verbose (bool | None)

  • stat (bool | None)

  • no_stat (bool | None)

  • verify (bool | None)

  • no_verify (bool | None)

  • fork_point (bool | None)

  • no_fork_point (bool | None)

  • whitespace (str | None)

  • ignore_whitespace (bool | None)

  • commit_date_is_author_date (bool | None)

  • ignore_date (bool | None)

  • root (bool | None)

  • autostash (bool | None)

  • no_autostash (bool | None)

  • autosquash (bool | None)

  • no_autosquash (bool | None)

  • reschedule_failed_exec (bool | None)

  • no_reschedule_failed_exec (bool | None)

  • context (int | None)

  • rerere_autoupdate (bool | None)

  • no_rerere_autoupdate (bool | None)

  • keep_empty (bool | None)

  • no_keep_empty (bool | None)

  • reapply_cherry_picks (bool | None)

  • no_reapply_cherry_picks (bool | None)

  • allow_empty_message (bool | None)

  • signoff (bool | None)

  • keep_base (bool | None)

  • strategy (str | bool | None)

  • strategy_option (str | None)

  • _exec (str | None)

  • gpg_sign (str | bool | None)

  • no_gpg_sign (bool | None)

  • empty (str | Literal['drop', 'keep', 'ask'] | None)

  • rebase_merges (str | Literal['rebase-cousins', 'no-rebase-cousins'] | None)

  • interactive (bool | None)

  • edit_todo (bool | None)

  • skip (bool | None)

  • show_current_patch (bool | None)

  • abort (bool | None)

  • _quit (bool | None)

  • check_returncode (bool | None)

  • kwargs (Any)

Return type:

str

Examples

>>> git = Git(path=example_git_repo.path)
>>> git_remote_repo = create_git_remote_repo()
>>> git.rebase()
'Current branch master is up to date.'

Declare upstream:

>>> git = Git(path=example_git_repo.path)
>>> git_remote_repo = create_git_remote_repo()
>>> git.rebase(upstream='origin')
'Current branch master is up to date.'
>>> git.path.exists()
True
pull(*, reftag=None, repository=None, deepen=None, depth=None, upload_pack=None, shallow_since=None, shallow_exclude=None, negotiation_tip=None, jobs=None, server_option=None, recurse_submodules=None, recurse_submodules_default=None, submodule_prefix=None, cleanup=None, rebase=None, no_rebase=None, strategy=None, strategy_option=None, gpg_sign=None, no_gpg_sign=None, commit=None, no_commit=None, edit=None, no_edit=None, fast_forward_only=None, fast_forward=None, no_fast_forward=None, sign_off=None, no_sign_off=None, stat=None, no_stat=None, squash=None, no_squash=None, verify=None, no_verify=None, verify_signatures=None, no_verify_signatures=None, summary=None, no_summary=None, autostash=None, no_autostash=None, allow_unrelated_histories=None, fetch=None, no_fetch=None, _all=None, force=None, keep=None, multiple=None, dry_run=None, append=None, atomic=None, ipv4=None, ipv6=None, progress=None, quiet=None, verbose=None, unshallow=None, update_shallow=None, negotiate_tip=None, no_write_fetch_head=None, write_fetch_head=None, no_auto_maintenance=None, auto_maintenance=None, no_write_commit_graph=None, write_commit_graph=None, prefetch=None, prune=None, prune_tags=None, no_tags=None, tags=None, no_recurse_submodules=None, set_upstream=None, update_head_ok=None, show_forced_updates=None, no_show_forced_updates=None, negotiate_only=None, log_in_real_time=False, check_returncode=None, **kwargs)[source]

Download from repo. Wraps git pull.

Return type:

str

Parameters:
  • reftag (Any | None)

  • repository (str | None)

  • deepen (str | None)

  • depth (str | None)

  • upload_pack (str | None)

  • shallow_since (str | None)

  • shallow_exclude (str | None)

  • negotiation_tip (str | None)

  • jobs (str | None)

  • server_option (str | None)

  • recurse_submodules (bool | Literal['yes', 'on-demand', 'no'] | None)

  • recurse_submodules_default (bool | Literal['yes', 'on-demand'] | None)

  • submodule_prefix (str | bytes | PathLike[str] | PathLike[bytes] | None)

  • cleanup (str | None)

  • rebase (str | bool | None)

  • no_rebase (bool | None)

  • strategy (str | bool | None)

  • strategy_option (str | None)

  • gpg_sign (str | bool | None)

  • no_gpg_sign (bool | None)

  • commit (bool | None)

  • no_commit (bool | None)

  • edit (bool | None)

  • no_edit (bool | None)

  • fast_forward_only (bool | None)

  • fast_forward (bool | None)

  • no_fast_forward (bool | None)

  • sign_off (bool | None)

  • no_sign_off (bool | None)

  • stat (bool | None)

  • no_stat (bool | None)

  • squash (bool | None)

  • no_squash (bool | None)

  • verify (bool | None)

  • no_verify (bool | None)

  • verify_signatures (bool | None)

  • no_verify_signatures (bool | None)

  • summary (bool | None)

  • no_summary (bool | None)

  • autostash (bool | None)

  • no_autostash (bool | None)

  • allow_unrelated_histories (bool | None)

  • fetch (bool | None)

  • no_fetch (bool | None)

  • _all (bool | None)

  • force (bool | None)

  • keep (bool | None)

  • multiple (bool | None)

  • dry_run (bool | None)

  • append (bool | None)

  • atomic (bool | None)

  • ipv4 (bool | None)

  • ipv6 (bool | None)

  • progress (bool | None)

  • quiet (bool | None)

  • verbose (bool | None)

  • unshallow (bool | None)

  • update_shallow (bool | None)

  • negotiate_tip (bool | None)

  • no_write_fetch_head (bool | None)

  • write_fetch_head (bool | None)

  • no_auto_maintenance (bool | None)

  • auto_maintenance (bool | None)

  • no_write_commit_graph (bool | None)

  • write_commit_graph (bool | None)

  • prefetch (bool | None)

  • prune (bool | None)

  • prune_tags (bool | None)

  • no_tags (bool | None)

  • tags (bool | None)

  • no_recurse_submodules (bool | None)

  • set_upstream (bool | None)

  • update_head_ok (bool | None)

  • show_forced_updates (bool | None)

  • no_show_forced_updates (bool | None)

  • negotiate_only (bool | None)

  • log_in_real_time (bool)

  • check_returncode (bool | None)

  • kwargs (Any)

Examples

>>> git = Git(path=example_git_repo.path)
>>> git_remote_repo = create_git_remote_repo()
>>> git.pull()
'Already up to date.'

Fetch via ref:

>>> git = Git(path=tmp_path)
>>> git.run(['init'])
'Initialized ...'
>>> git_remote_repo = create_git_remote_repo()
>>> git.pull(reftag=f'file://{git_remote_repo}')
''
>>> git.path.exists()
True
init(*, template=None, separate_git_dir=None, object_format=None, branch=None, initial_branch=None, shared=None, quiet=None, bare=None, ref_format=None, check_returncode=None, make_parents=True, **kwargs)[source]

Create empty repo. Wraps git init.

Parameters:
  • template (str | pathlib.Path, optional) – Directory from which templates will be used. The template directory contains files and directories that will be copied to the $GIT_DIR after it is created. The template directory will be one of the following (in order): - The argument given with the –template option - The contents of the $GIT_TEMPLATE_DIR environment variable - The init.templateDir configuration variable - The default template directory: /usr/share/git-core/templates

  • separate_git_dir (libvcs._internal.types.StrOrBytesPath, optional) – Instead of placing the git repository in <directory>/.git/, place it in the specified path. The .git file at <directory>/.git will contain a gitfile that points to the separate git dir. This is useful when you want to store the git directory on a different disk or filesystem.

  • object_format ("sha1" | "sha256", optional) – Specify the hash algorithm to use. The default is sha1. Note that sha256 is still experimental in git and requires git version >= 2.29.0. Once the repository is created with a specific hash algorithm, it cannot be changed.

  • branch (str, optional) – Use the specified name for the initial branch. If not specified, fall back to the default name (currently “master”, but may change based on init.defaultBranch configuration).

  • initial_branch (str, optional) – Alias for branch parameter. Specify the name for the initial branch. This is provided for compatibility with newer git versions.

  • shared (bool | str, optional) – Specify that the git repository is to be shared amongst several users. Valid values are: - false: Turn off sharing (default) - true: Same as group - umask: Use permissions specified by umask - group: Make the repository group-writable - all, world, everybody: Same as world, make repo readable by all users - An octal number string: Explicit mode specification (e.g., “0660”)

  • quiet (bool, optional) – Only print error and warning messages; all other output will be suppressed. Useful for scripting.

  • bare (bool, optional) – Create a bare repository. If GIT_DIR environment is not set, it is set to the current working directory. Bare repositories have no working tree and are typically used as central repositories.

  • ref_format ("files" | "reftable", optional) – Specify the reference storage format. Requires git version >= 2.37.0. - files: Classic format with packed-refs and loose refs (default) - reftable: New format that is more efficient for large repositories

  • check_returncode (bool, optional) – If True, check the return code of the git command and raise a CalledProcessError if it is non-zero.

  • make_parents (bool, default: True) – If True, create the target directory if it doesn’t exist. If False, raise an error if the directory doesn’t exist.

  • kwargs (Any)

Returns:

The output of the git init command.

Return type:

str

Raises:
  • CalledProcessError – If the git command fails and check_returncode is True.

  • ValueError – If invalid parameters are provided.

  • FileNotFoundError – If make_parents is False and the target directory doesn’t exist.

Examples

>>> git = Git(path=tmp_path)
>>> git.init()
'Initialized empty Git repository in ...'

Create with a specific initial branch name:

>>> new_repo = tmp_path / 'branch_example'
>>> new_repo.mkdir()
>>> git = Git(path=new_repo)
>>> git.init(branch='main')
'Initialized empty Git repository in ...'

Create a bare repository:

>>> bare_repo = tmp_path / 'bare_example'
>>> bare_repo.mkdir()
>>> git = Git(path=bare_repo)
>>> git.init(bare=True)
'Initialized empty Git repository in ...'

Create with a separate git directory:

>>> repo_path = tmp_path / 'repo'
>>> git_dir = tmp_path / 'git_dir'
>>> repo_path.mkdir()
>>> git_dir.mkdir()
>>> git = Git(path=repo_path)
>>> git.init(separate_git_dir=str(git_dir.absolute()))
'Initialized empty Git repository in ...'

Create with shared permissions:

>>> shared_repo = tmp_path / 'shared_example'
>>> shared_repo.mkdir()
>>> git = Git(path=shared_repo)
>>> git.init(shared='group')
'Initialized empty shared Git repository in ...'

Create with octal permissions:

>>> shared_repo = tmp_path / 'shared_octal_example'
>>> shared_repo.mkdir()
>>> git = Git(path=shared_repo)
>>> git.init(shared='0660')
'Initialized empty shared Git repository in ...'

Create with a template directory:

>>> template_repo = tmp_path / 'template_example'
>>> template_repo.mkdir()
>>> git = Git(path=template_repo)
>>> git.init(template=str(tmp_path))
'Initialized empty Git repository in ...'

Create with SHA-256 object format (requires git >= 2.29.0):

>>> sha256_repo = tmp_path / 'sha256_example'
>>> sha256_repo.mkdir()
>>> git = Git(path=sha256_repo)
>>> git.init(object_format='sha256')
'Initialized empty Git repository in ...'
help(*, _all=None, verbose=None, no_external_commands=None, no_aliases=None, config=None, guides=None, info=None, man=None, web=None, check_returncode=None, **kwargs)[source]

Help info. Wraps git help.

Parameters:
  • _all (bool) – Prints everything.

  • no_external_commands (bool) – For use with all, excludes external commands.

  • no_aliases (bool) – For use with all, excludes aliases.

  • verbose (bool) – For us with all, on by default.

  • config (bool) – List all config vars.

  • guides (bool) – List concept guides.

  • info (bool) – Display man page in info format.

  • man (bool) – Man page.

  • web (bool) – Man page in HTML.

  • check_returncode (bool | None)

  • kwargs (Any)

Return type:

str

Examples

>>> git = Git(path=tmp_path)
>>> git.help()
"usage: git [...--version] [...--help] [-C <path>]..."
>>> git.help(_all=True)
"See 'git help <command>' to read about a specific subcommand..."
>>> git.help(info=True)
"usage: git [...--version] [...--help] [-C <path>] [-c <name>=<value>]..."
>>> git.help(man=True)
"usage: git [...--version] [...--help] [-C <path>] [-c <name>=<value>]..."
reset(*, quiet=None, refresh=None, no_refresh=None, pathspec_from_file=None, pathspec=None, soft=None, mixed=None, hard=None, merge=None, keep=None, commit=None, recurse_submodules=None, no_recurse_submodules=None, check_returncode=None, **kwargs)[source]

Reset HEAD. Wraps git help.

Parameters:
Return type:

str

Examples

>>> git = Git(path=example_git_repo.path)
>>> git.reset()
''
>>> git.reset(soft=True, commit='HEAD~0')
''
checkout(*, quiet=None, progress=None, no_progress=None, pathspec_from_file=None, pathspec=None, force=None, ours=None, theirs=None, no_track=None, guess=None, no_guess=None, _list=None, detach=None, merge=None, ignore_skip_worktree_bits=None, patch=None, orphan=None, conflict=None, overwrite_ignore=None, no_overwrite_ignore=None, recurse_submodules=None, no_recurse_submodules=None, overlay=None, no_overlay=None, commit=None, branch=None, new_branch=None, start_point=None, treeish=None, check_returncode=None, **kwargs)[source]

Switch branches or checks out files.

Wraps git checkout (git co).

Parameters:
Return type:

str

Examples

>>> git = Git(path=example_git_repo.path)
>>> git.checkout()
"Your branch is up to date with 'origin/master'."
>>> git.checkout(branch='origin/master', pathspec='.')
''
status(*, verbose=None, long=None, short=None, branch=None, z=None, column=None, no_column=None, ahead_behind=None, no_ahead_behind=None, renames=None, no_renames=None, find_renames=None, porcelain=None, untracked_files=None, ignored=None, ignored_submodules=None, pathspec=None, check_returncode=None, **kwargs)[source]

Return status of working tree.

Wraps git status.

git ls-files has similar params (e.g. z)

Parameters:
Return type:

str

Examples

>>> git = Git(path=example_git_repo.path)
>>> git.status()
"On branch master..."
>>> pathlib.Path(example_git_repo.path / 'new_file.txt').touch()
>>> git.status(porcelain=True)
'?? new_file.txt'
>>> git.status(porcelain='1')
'?? new_file.txt'
>>> git.status(porcelain='2')
'? new_file.txt'
>>> git.status(C=example_git_repo.path / '.git', porcelain='2')
'? new_file.txt'
>>> git.status(porcelain=True, untracked_files="no")
''
config(*, replace_all=None, get=None, get_all=None, get_regexp=None, get_urlmatch=None, system=None, local=None, worktree=None, file=None, blob=None, remove_section=None, rename_section=None, unset=None, unset_all=None, _list=None, fixed_value=None, no_type=None, null=None, name_only=None, show_origin=None, show_scope=None, get_color=None, get_colorbool=None, default=None, _type=None, edit=None, no_includes=None, includes=None, add=None, check_returncode=None, **kwargs)[source]

Get and set repo configuration.

git config.

Parameters:
  • replace_all (Optional[bool])

  • get (Optional[bool])

  • get_all (Optional[bool])

  • get_regexp (Optional[bool])

  • get_urlmatch (Optional[tuple[str, str]])

  • system (Optional[bool])

  • local (Optional[bool])

  • worktree (Optional[bool])

  • file (Optional[StrOrBytesPath])

  • blob (Optional[str])

  • remove_section (Optional[bool])

  • rename_section (Optional[bool])

  • unset (Optional[bool])

  • unset_all (Optional[bool])

  • _list (Optional[bool])

  • fixed_value (Optional[bool])

  • no_type (Optional[bool])

  • null (Optional[bool])

  • name_only (Optional[bool])

  • show_origin (Optional[bool])

  • show_scope (Optional[bool])

  • get_color (Optional[t.Union[str, bool]])

  • get_colorbool (Optional[t.Union[str, bool]])

  • default (Optional[str])

  • _type ("bool", "int", "bool-or-int", "path", "expiry-date", "color")

  • edit (Optional[bool])

  • no_includes (Optional[bool])

  • includes (Optional[bool])

  • add (Optional[bool])

  • check_returncode (bool | None)

  • kwargs (Any)

Return type:

str

Examples

>>> git = Git(path=example_git_repo.path)
>>> git.config()
'...: ...'
>>> git.config(_list=True)
'...user.email=...'
>>> git.config(get='color.diff')
'auto'
version(*, build_options=None, check_returncode=None, **kwargs)[source]

Version. Wraps git version.

Return type:

str

Parameters:
  • build_options (bool | None)

  • check_returncode (bool | None)

  • kwargs (Any)

Examples

>>> git = Git(path=example_git_repo.path)
>>> git.version()
'git version ...'
>>> git.version(build_options=True)
'git version ...'
rev_parse(*, parseopt=None, sq_quote=None, keep_dashdash=None, stop_at_non_option=None, stuck_long=None, verify=None, args=None, check_returncode=None, **kwargs)[source]

rev-parse. Wraps git rev-parse.

Return type:

str

Parameters:
  • parseopt (bool | None)

  • sq_quote (bool | None)

  • keep_dashdash (bool | None)

  • stop_at_non_option (bool | None)

  • stuck_long (bool | None)

  • verify (bool | None)

  • args (str | None)

  • check_returncode (bool | None)

  • kwargs (Any)

Examples

>>> git = Git(path=example_git_repo.path)
>>> git.rev_parse()
''
>>> git.rev_parse(parseopt=True)
'usage: git rev-parse --parseopt...'
>>> git.rev_parse(verify=True, args='HEAD')
'...'
rev_list(*, commit, path=None, max_count=None, skip=None, since=None, after=None, until=None, before=None, max_age=None, min_age=None, author=None, committer=None, grep=None, all_match=None, invert_grep=None, regexp_ignore_case=None, basic_regexp=None, extended_regexp=None, fixed_strings=None, perl_regexp=None, remove_empty=None, merges=None, no_merges=None, no_min_parents=None, min_parents=None, no_max_parents=None, max_parents=None, first_parent=None, exclude_first_parent_only=None, _not=None, _all=None, branches=None, tags=None, remotes=None, exclude=None, reflog=None, alternative_refs=None, single_worktree=None, ignore_missing=None, stdin=None, disk_usage=None, cherry_mark=None, cherry_pick=None, left_only=None, right_only=None, cherry=None, walk_reflogs=None, merge=None, boundary=None, use_bitmap_index=None, progress=None, header=None, check_returncode=True, log_in_real_time=False, **kwargs)[source]

rev-list. Wraps git rev-list.

Return type:

str

Parameters:
  • commit (list[str] | str | None)

  • path (list[str | PathLike[str]] | str | PathLike[str] | None)

  • max_count (int | None)

  • skip (int | None)

  • since (str | None)

  • after (str | None)

  • until (str | None)

  • before (str | None)

  • max_age (str | None)

  • min_age (str | None)

  • author (str | None)

  • committer (str | None)

  • grep (str | None)

  • all_match (bool | None)

  • invert_grep (bool | None)

  • regexp_ignore_case (bool | None)

  • basic_regexp (bool | None)

  • extended_regexp (bool | None)

  • fixed_strings (bool | None)

  • perl_regexp (bool | None)

  • remove_empty (bool | None)

  • merges (bool | None)

  • no_merges (bool | None)

  • no_min_parents (bool | None)

  • min_parents (int | None)

  • no_max_parents (bool | None)

  • max_parents (int | None)

  • first_parent (bool | None)

  • exclude_first_parent_only (bool | None)

  • _not (bool | None)

  • _all (bool | None)

  • branches (str | bool | None)

  • tags (str | bool | None)

  • remotes (str | bool | None)

  • exclude (bool | None)

  • reflog (bool | None)

  • alternative_refs (bool | None)

  • single_worktree (bool | None)

  • ignore_missing (bool | None)

  • stdin (bool | None)

  • disk_usage (bool | str | None)

  • cherry_mark (bool | None)

  • cherry_pick (bool | None)

  • left_only (bool | None)

  • right_only (bool | None)

  • cherry (bool | None)

  • walk_reflogs (bool | None)

  • merge (bool | None)

  • boundary (bool | None)

  • use_bitmap_index (bool | None)

  • progress (str | bool | None)

  • header (bool | None)

  • check_returncode (bool | None)

  • log_in_real_time (bool)

  • kwargs (Any)

Examples

>>> git = Git(path=example_git_repo.path)
>>> git.rev_list(commit="HEAD")
'...'
>>> git.run(['commit', '--allow-empty', '--message=Moo'])
'[master ...] Moo'
>>> git.rev_list(commit="HEAD", max_count=1)
'...'
>>> git.rev_list(commit="HEAD", path=".", max_count=1, header=True)
'...'
>>> git.rev_list(commit="origin..HEAD", max_count=1, _all=True, header=True)
'...'
>>> git.rev_list(commit="origin..HEAD", max_count=1, header=True)
'...'
symbolic_ref(*, name, ref=None, message=None, short=None, delete=None, quiet=None, check_returncode=None, **kwargs)[source]

Return symbolic-ref.

Wraps git symbolic-ref.

Return type:

str

Parameters:
  • name (str)

  • ref (str | None)

  • message (str | None)

  • short (bool | None)

  • delete (bool | None)

  • quiet (bool | None)

  • check_returncode (bool | None)

  • kwargs (Any)

Examples

>>> git = Git(path=example_git_repo.path)
>>> git.symbolic_ref(name="test")
'fatal: ref test is not a symbolic ref'
>>> git.symbolic_ref(name="test")
'fatal: ref test is not a symbolic ref'
show_ref(*, pattern=None, quiet=None, verify=None, head=None, dereference=None, tags=None, _hash=None, abbrev=None, check_returncode=None, **kwargs)[source]

show-ref. Wraps git show-ref.

Return type:

str

Parameters:

Examples

>>> git = Git(path=example_git_repo.path)
>>> git.show_ref()
'...'
>>> git.show_ref(pattern='master')
'...'
>>> git.show_ref(pattern='master', head=True)
'...'
>>> git.show_ref(pattern='HEAD', verify=True)
'... HEAD'
>>> git.show_ref(pattern='master', dereference=True)
'... refs/heads/master\n... refs/remotes/origin/master'
>>> git.show_ref(pattern='HEAD', tags=True)
''