Git

Version control operations using the Git CLI.

Operations

Operation Description
Git.Install
Installs git in the build container (unlike the other Git operations, which run on the host). Skips with an info log when git is already on PATH; otherwise runs apt-get update && apt-get install -y --no-install-recommends git ca-certificates (apt) or apk add --no-cache git (apk), fails with an "unsupported image" message when neither exists, and logs git --version. Use it before commands in the container that need git, such as tests that create git repositories.
Git.Tag
Creates a git tag. By default creates annotated tags. Runs on host (not in container). WithMessage takes a string or a Func<string>; the function is evaluated when the Git.Tag step runs, so the message can read StepOutput values produced by earlier steps.
Git.Push
Pushes the current branch to the remote repository; it does not push tags (use Git.PushTags for that). Runs on host (not in container).
Git.PushTags
Pushes tags (not the branch; use Git.Push for the current branch) to the remote repository, skipping tags that already exist on the remote. Tags are pushed individually for safety. Runs on host (not in container).
Git.PushTag
Pushes only refs/tags/<tagName> to the remote. Signature: PushTag(string tagName, string remote = "origin"). Fails when the tag does not exist locally. Other local tags are not pushed (unlike Git.PushTags, which stays unchanged). The step context is the tag name. Runs on host (not in container).
Git.RemoteTags
Lists remote tags with git ls-remote --tags --refs <remote> refs/tags/<pattern>. Signature: RemoteTags(string pattern = "*", string remote = "origin"). A step that runs on the host. Returns a CommandStep whose Value is the newline-separated tag names without refs/tags/, empty when none match. The step context is the pattern. Of the CommandStep fluent methods, WithTimeout, WithEnv, Quiet and AllowFailure are honored.
Git.Commit
Evaluated when the script loads, on the host. It is not a step: it does not appear in --dry-run and runs before the container exists. Full SHA of the commit being built: HEAD, or the commit given with ando run --ref. Property (not a method). For a step that runs in order with the other steps, use Git.RevParse.
Git.ShortCommit
Evaluated when the script loads, on the host. It is not a step: it does not appear in --dry-run and runs before the container exists. Short SHA of the commit being built. Property (not a method). For a step that runs in order with the other steps, use Git.RevParse with shortSha: true.
Git.CommitTime
Evaluated when the script loads, on the host. It is not a step: it does not appear in --dry-run and runs before the container exists. The commit time of the commit being built (HEAD, or the commit given with ando run --ref) as a long of unix seconds. Cached; property (not a method). Use it as a reproducible timestamp, for example Archive.Zip(.., o => o.WithTimestamp(Git.CommitTime)).
Git.RequireClean
Fails the build before any step runs if tracked files are modified, staged or deleted, or non-ignored untracked files exist, under the given paths (the whole repository if none). With --ref the build uses a clean checkout, so it always passes. .ando/runs/ has its own .gitignore and never counts. Runs on host at evaluation time.
Git.ChangedSince
Evaluated when the script loads, on the host. It is not a step: it does not appear in --dry-run and runs before the container exists. Returns true if any file under the paths differs between sinceRef and the commit being built (committed changes only). Checks the whole repository when no paths are given. Step-based counterpart: Git.ChangedFiles.
Git.LastTag
Evaluated when the script loads, on the host. It is not a step: it does not appear in --dry-run and runs before the container exists. Returns the most recent tag reachable from the commit being built that matches the glob pattern (default *), or null when there is none. Step-based counterpart: Git.LatestTag.
Git.RevParse
Resolves a revision with git rev-parse [--short] <rev>. Signature: RevParse(string rev = "HEAD", bool shortSha = false). A step that runs on the host in the source root (the --ref worktree when set), in order with the other steps. Returns a CommandStep whose Value is the trimmed SHA; read it at execution time (for example in Ando.Defer, Ando.When or a Func<string> overload). The eager counterparts are Git.Commit and Git.ShortCommit, which are evaluated when the script loads. Of the CommandStep fluent methods, WithTimeout, WithEnv, Quiet and AllowFailure are honored. Do not use InDirectory: it resolves to a container path, but git runs on the host.
Git.LatestTag
Finds the most recent tag reachable from HEAD that matches a glob with git describe --tags --abbrev=0 --match <pattern> HEAD. Signature: LatestTag(string pattern = "*"). A step that runs on the host in the source root (the --ref worktree when set). Returns a CommandStep whose Value is the tag. If no tag matches, Value is empty and the build continues (the failure is allowed). The eager counterpart is Git.LastTag, evaluated when the script loads. This step ignores all the CommandStep fluent methods (WithTimeout, InDirectory, WithEnv, Quiet, AllowFailure).
Git.ChangedFiles
Lists files changed between a ref and HEAD with git diff --name-only <sinceRef> HEAD -- <paths>. Signatures: ChangedFiles(string sinceRef, params string[] paths) and ChangedFiles(Func<string> sinceRef, params string[] paths); the Func overload resolves the ref when the step runs, so it can use Value of an earlier step (its step has no context in --dry-run, because the ref is not known yet). A step that runs on the host in the source root (the --ref worktree when set). Returns a CommandStep whose Value is a newline-separated list, empty when nothing changed. An unknown ref fails the step. No paths means the whole repository. The eager counterpart is Git.ChangedSince, evaluated when the script loads. Of the CommandStep fluent methods, WithTimeout, WithEnv, Quiet and AllowFailure are honored. Do not use InDirectory: it resolves to a container path, but git runs on the host.

Operation Details

Git.Install source

Installs git in the build container (unlike the other Git operations, which run on the host). Skips with an info log when git is already on PATH; otherwise runs apt-get update && apt-get install -y --no-install-recommends git ca-certificates (apt) or apk add --no-cache git (apk), fails with an "unsupported image" message when neither exists, and logs git --version. Use it before commands in the container that need git, such as tests that create git repositories.

Git.Install();

Git.Tag source

Creates a git tag. By default creates annotated tags. Runs on host (not in container). WithMessage takes a string or a Func<string>; the function is evaluated when the Git.Tag step runs, so the message can read StepOutput values produced by earlier steps.

Git.Tag("v1.0.0");
Git.Tag("v1.0.0", o => o.WithMessage("Release notes here"));
Git.Tag("v1.0.0", o => o.WithMessage(() => $"v1.0.0\n\nsha256: {hash.Value}")); // resolved when the step runs
Git.Tag("v1.0.0", o => o.AsLightweight()); // Lightweight tag

Git.Push source

Pushes the current branch to the remote repository; it does not push tags (use Git.PushTags for that). Runs on host (not in container).

Git.Push();
Git.Push(o => o.ToRemote("upstream"));
Git.Push(o => o.WithUpstream()); // -u flag

Git.PushTags source

Pushes tags (not the branch; use Git.Push for the current branch) to the remote repository, skipping tags that already exist on the remote. Tags are pushed individually for safety. Runs on host (not in container).

Git.PushTags();
Git.PushTags("upstream");

Git.PushTag source

Pushes only refs/tags/<tagName> to the remote. Signature: PushTag(string tagName, string remote = "origin"). Fails when the tag does not exist locally. Other local tags are not pushed (unlike Git.PushTags, which stays unchanged). The step context is the tag name. Runs on host (not in container).

Git.Tag($"v{version}");
Git.PushTag($"v{version}");

Git.RemoteTags source

Lists remote tags with git ls-remote --tags --refs <remote> refs/tags/<pattern>. Signature: RemoteTags(string pattern = "*", string remote = "origin"). A step that runs on the host. Returns a CommandStep whose Value is the newline-separated tag names without refs/tags/, empty when none match. The step context is the pattern. Of the CommandStep fluent methods, WithTimeout, WithEnv, Quiet and AllowFailure are honored.

var remote = Git.RemoteTags("v*");
Ando.Defer(() => Log.Info(remote.Value));

Git.Commit source

Evaluated when the script loads, on the host. It is not a step: it does not appear in --dry-run and runs before the container exists. Full SHA of the commit being built: HEAD, or the commit given with ando run --ref. Property (not a method). For a step that runs in order with the other steps, use Git.RevParse.

var commit = Git.Commit;
Ando.WriteManifest(Root / "artifacts", "1.4.0", Git.Commit);

Git.ShortCommit source

Evaluated when the script loads, on the host. It is not a step: it does not appear in --dry-run and runs before the container exists. Short SHA of the commit being built. Property (not a method). For a step that runs in order with the other steps, use Git.RevParse with shortSha: true.

Log.Info($"Building {Git.ShortCommit}");

Git.CommitTime source

Evaluated when the script loads, on the host. It is not a step: it does not appear in --dry-run and runs before the container exists. The commit time of the commit being built (HEAD, or the commit given with ando run --ref) as a long of unix seconds. Cached; property (not a method). Use it as a reproducible timestamp, for example Archive.Zip(.., o => o.WithTimestamp(Git.CommitTime)).

Archive.Zip("artifacts/web", "artifacts/web.zip", o => o.WithTimestamp(Git.CommitTime));

Git.RequireClean source

Fails the build before any step runs if tracked files are modified, staged or deleted, or non-ignored untracked files exist, under the given paths (the whole repository if none). With --ref the build uses a clean checkout, so it always passes. .ando/runs/ has its own .gitignore and never counts. Runs on host at evaluation time.

Git.RequireClean("src", "web", "infra", "Directory.Build.props");

Git.ChangedSince source

Evaluated when the script loads, on the host. It is not a step: it does not appear in --dry-run and runs before the container exists. Returns true if any file under the paths differs between sinceRef and the commit being built (committed changes only). Checks the whole repository when no paths are given. Step-based counterpart: Git.ChangedFiles.

if (Git.ChangedSince(Git.LastTag("v*") ?? "HEAD~1", "src/Worker", "services/signing"))
{
  Log.Info("Worker changed");
}

Git.LastTag source

Evaluated when the script loads, on the host. It is not a step: it does not appear in --dry-run and runs before the container exists. Returns the most recent tag reachable from the commit being built that matches the glob pattern (default *), or null when there is none. Step-based counterpart: Git.LatestTag.

var last = Git.LastTag("v*");

Git.RevParse source

Resolves a revision with git rev-parse [--short] <rev>. Signature: RevParse(string rev = "HEAD", bool shortSha = false). A step that runs on the host in the source root (the --ref worktree when set), in order with the other steps. Returns a CommandStep whose Value is the trimmed SHA; read it at execution time (for example in Ando.Defer, Ando.When or a Func<string> overload). The eager counterparts are Git.Commit and Git.ShortCommit, which are evaluated when the script loads. Of the CommandStep fluent methods, WithTimeout, WithEnv, Quiet and AllowFailure are honored. Do not use InDirectory: it resolves to a container path, but git runs on the host.

var sha = Git.RevParse(shortSha: true);
Ando.Report("commit", () => sha.Value);

Git.LatestTag source

Finds the most recent tag reachable from HEAD that matches a glob with git describe --tags --abbrev=0 --match <pattern> HEAD. Signature: LatestTag(string pattern = "*"). A step that runs on the host in the source root (the --ref worktree when set). Returns a CommandStep whose Value is the tag. If no tag matches, Value is empty and the build continues (the failure is allowed). The eager counterpart is Git.LastTag, evaluated when the script loads. This step ignores all the CommandStep fluent methods (WithTimeout, InDirectory, WithEnv, Quiet, AllowFailure).

var t = Git.LatestTag("v*");
Ando.When(() => t.Value != "", () => Log.Info("A release tag exists"));

Git.ChangedFiles source

Lists files changed between a ref and HEAD with git diff --name-only <sinceRef> HEAD -- <paths>. Signatures: ChangedFiles(string sinceRef, params string[] paths) and ChangedFiles(Func<string> sinceRef, params string[] paths); the Func overload resolves the ref when the step runs, so it can use Value of an earlier step (its step has no context in --dry-run, because the ref is not known yet). A step that runs on the host in the source root (the --ref worktree when set). Returns a CommandStep whose Value is a newline-separated list, empty when nothing changed. An unknown ref fails the step. No paths means the whole repository. The eager counterpart is Git.ChangedSince, evaluated when the script loads. Of the CommandStep fluent methods, WithTimeout, WithEnv, Quiet and AllowFailure are honored. Do not use InDirectory: it resolves to a container path, but git runs on the host.

var t = Git.LatestTag("v*");
var c = Git.ChangedFiles(() => t.Value, "src/Worker");
Ando.When(() => c.Value != "", () => Log.Info("Worker changed"));

Example

Tag and push a release with version tracking.

// Tag and push a release
Git.Tag("v1.0.0");
Git.Push();
Git.PushTags();

// Custom tag with message
Git.Tag("v1.0.0", o => o.WithMessage("Release notes here"));

// Message resolved when the step runs (can read earlier step values)
Git.Tag("v1.0.0", o => o.WithMessage(() => $"v1.0.0\n\nsha256: {hash.Value}"));

// Lightweight tag (not annotated)
Git.Tag("my-tag", o => o.AsLightweight());

Using with Profiles

Git operations are commonly used with profiles to run only during release workflows.

// Use profiles to run Git operations conditionally
var release = DefineProfile("release");

Dotnet.Build(app);
Dotnet.Test(tests);

if (release) {
    Git.Tag("v1.0.0");
    Git.Push();
    Git.PushTags();
}

// CLI usage:
// ando -p release

Options Reference

Git.Tag Options

OptionDescription
WithMessage(string)Set the tag message for annotated tags. Appears when viewing the tag with git show.
WithMessage(Func<string>)Same, but the function runs when the Git.Tag step runs, so the message can use the Value of an earlier step.
AsLightweight()Create a lightweight tag instead of an annotated tag. Lightweight tags are just pointers to commits without metadata. Use for temporary or local markers.
WithSkipIfExists()Skip tag creation if the tag already exists. Shows a warning instead of failing. Useful in CI where builds may be re-run.

Git.Push Options

OptionDescription
ToRemote(string)Remote to push to (e.g., “origin”, “upstream”). Defaults to “origin”.
ToBranch(string)Branch to push. Defaults to the current branch.
WithUpstream()Set upstream tracking reference (-u flag). The local branch will track the remote branch for future push/pull operations.

Push and PushTags

Both are separate steps and neither does the other’s job:

OperationPushesUse it when
Git.Push()The current branch (ToRemote, ToBranch, WithUpstream options). Tags are not pushed.You committed or bumped a version and want the commit on the remote.
Git.PushTags()Tags only, one at a time, skipping tags that already exist on the remote. The branch is not pushed.You created a tag with Git.Tag() and want it on the remote.

A release usually calls Git.Tag(...), Git.Push() and then Git.PushTags().

PushTag and RemoteTags

OperationWhat it does
Git.PushTag(tagName, remote = "origin")Pushes only refs/tags/<tagName>. Fails when the tag does not exist locally. Other local tags are not pushed. Step context is the tag name.
Git.RemoteTags(pattern = "*", remote = "origin")A step: git ls-remote --tags --refs <remote> refs/tags/<pattern>. Value is the newline-separated tag names without refs/tags/, empty when none match.
Git.Tag($"v{version}");
Git.PushTag($"v{version}");

var remote = Git.RemoteTags("v*");
// Pick the previous tag in your own C#, since ordering tags is release policy:
Ando.Defer(() => Log.Info($"Remote tags: {remote.Value.Replace('\n', ' ')}"));

Evaluated when the script loads (not steps)

Git.Commit, Git.ShortCommit, Git.CommitTime, Git.ChangedSince() and Git.LastTag() are not steps. They run git on the host while the script is evaluated, before any container exists and before any step runs. They do not appear in --dry-run, and they see the repository as it is when the script loads, not as earlier steps leave it (a tag created by Git.Tag() in the same script is not visible to Git.LastTag()). Git.RequireClean() is also checked at evaluation time. Their values can be used directly in C# if statements and string interpolation.

For a step that runs in order with the other steps, see Step variants. These refer to the commit being built: HEAD, or the commit given with ando run --ref.

MemberDescription
Git.CommitFull SHA of the commit being built.
Git.ShortCommitShort SHA of the commit being built.
Git.CommitTimeCommit time of the commit being built, as a long of unix seconds. Cached. Use it as a reproducible timestamp: Archive.Zip(.., o => o.WithTimestamp(Git.CommitTime)).
Git.RequireClean(params string[] paths)Fails the build before any step runs if tracked files are modified, staged or deleted, or untracked files that are not ignored exist, under the paths (the whole repository when none are given). ANDO’s own build.csando.log and .ando/ are ignored. With --ref the build uses a clean checkout, so it always passes.
Git.ChangedSince(sinceRef, params string[] paths)True if any file under the paths differs between sinceRef and the commit being built. Committed changes only. Checks the whole repository when no paths are given.
Git.LastTag(pattern = "*")The most recent tag reachable from the commit being built that matches the glob (such as "v*"), or null.
Git.RequireClean("src", "web", "infra", "Directory.Build.props");

var last = Git.LastTag("v*");
if (last != null && Git.ChangedSince(last, "src/Worker", "services/signing"))
{
    Log.Info($"Worker changed since {last}; building {Git.ShortCommit}");
}

.ando/runs/ has its own .gitignore, so run records never make the tree dirty.

Step variants: RevParse, LatestTag, ChangedFiles

These three are steps. They run on the host in the source root (the --ref worktree when set), in order with the other steps like Git.Tag and Git.Push, so they appear in --dry-run and see what earlier steps did. Each returns a CommandStep whose Value is the trimmed stdout; read it at execution time (Ando.When, Ando.Defer, Ando.Report or a Func<> overload), not at the top level of the script.

StepRunsEager counterpart
Git.RevParse(string rev = "HEAD", bool shortSha = false)git rev-parse [--short] <rev>Git.Commit, Git.ShortCommit
Git.LatestTag(string pattern = "*")git describe --tags --abbrev=0 --match <pattern> HEAD. If no tag matches, Value is empty and the build continues.Git.LastTag()
Git.ChangedFiles(string sinceRef, params string[] paths)git diff --name-only <sinceRef> HEAD -- <paths>. Value is a newline-separated list, empty when nothing changed. An unknown ref fails the step.Git.ChangedSince()
Git.ChangedFiles(Func<string> sinceRef, params string[] paths)Same, but the ref is resolved when the step runs, so it can use Value of an earlier step. The step has no context in --dry-run. An empty resolved ref fails the step with a “sinceRef is empty” error.Git.ChangedSince()

Of the CommandStep fluent methods, Git.RevParse and Git.ChangedFiles honor WithTimeout, WithEnv, Quiet and AllowFailure. Do not use InDirectory on them: it resolves to a container path, but git runs on the host. Git.LatestTag ignores all the fluent methods.

Use the eager members when you need the value while the script is evaluated (to decide which steps to register). Use the step variants when the value must reflect the state at that point of the run, or must show up as a step.

var sha = Git.RevParse(shortSha: true);
var t = Git.LatestTag("v*");
var c = Git.ChangedFiles(() => t.Value, "src/Worker");

Ando.When(() => c.Value != "", () => Log.Info($"Worker changed since {t.Value}, building {sha.Value}"));

Installing git in the build container

Git.Install() is different from every other Git.* operation: it runs in the build container, not on the host. Use it when commands inside the container need git, for example tests that create real git repositories. The default image has no git.

  • If git is already on PATH the step logs that and does nothing.
  • Otherwise it installs with apt (apt-get update && apt-get install -y --no-install-recommends git ca-certificates) or with apk (apk add --no-cache git). An image with neither fails with an “unsupported image” message.
  • It logs git --version when done.
Git.Install();

Notes

  • Git operations run on the host machine, not inside the container, since git credentials are typically configured on the host. The members in “Evaluated when the script loads” additionally run when the script loads, not as steps.
  • By default, Git.Tag() creates annotated tags. Use .AsLightweight() for lightweight tags.
  • Git.PushTags() only pushes tags that don’t already exist on the remote. Tags are pushed individually rather than with --tags, so existing remote tags are never overwritten. If all tags already exist, the step succeeds with a skip message.
  • CI Server: The ANDO CI Server automatically configures a git committer identity (user.name / user.email) in build containers, so annotated tags work out of the box. The default identity is Ando Server <ando-server@localhost>. Override it with GIT_COMMITTER_NAME / GIT_COMMITTER_EMAIL environment variables in your .env.ando file.