git

package
v1.0.12 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 2, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package git provides helpers to execute git operations on local repositories.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AheadBehind

func AheadBehind(path string, fetch bool) (ahead, behind int, err error)

AheadBehind returns how many commits the current branch is ahead/behind origin. Pass fetch=true to run git fetch first for accurate up-to-date numbers (slower).

func BranchExists

func BranchExists(path, branch string) bool

BranchExists reports whether a local branch with the given name exists.

func Checkout

func Checkout(path, branch string) error

Checkout switches to the specified branch.

func Commit

func Commit(path, message string) (string, error)

Commit creates a commit with the given message.

func CommitLog

func CommitLog(path string, n int) ([]string, error)

CommitLog returns the last n commits as one-line entries (hash + subject). Each entry has the format "<short-hash> <subject>".

func CreateBranch

func CreateBranch(path, branch string) error

CreateBranch creates and checks out a new branch from the current HEAD.

func CurrentBranch

func CurrentBranch(path string) (string, error)

CurrentBranch returns the name of the currently checked-out branch.

func DefaultBranch

func DefaultBranch(path string) (string, error)

DefaultBranch detects the default branch (main/master) for a repo. It tries origin/HEAD first, then falls back to probing local branches.

func DeleteRemoteBranch

func DeleteRemoteBranch(path, branch string) error

DeleteRemoteBranch deletes a branch on origin.

func DirtyFiles

func DirtyFiles(path string) ([]string, error)

DirtyFiles returns the list of modified/untracked files.

func DirtyFilesWithStatus

func DirtyFilesWithStatus(path string) ([]string, error)

DirtyFilesWithStatus returns modified/untracked files keeping the full porcelain line (e.g. " M src/foo.php", "?? scratch.txt").

func DiscardChanges

func DiscardChanges(path string) error

DiscardChanges discards all uncommitted changes in the working tree and removes untracked files. This is equivalent to:

git checkout -- .
git clean -fd

This is irreversible — call IsDirty first to confirm there are changes.

func DiscardFiles added in v1.0.10

func DiscardFiles(path string, porcelainFiles []string) error

DiscardFiles selectively discards uncommitted changes for the given files. Each entry in porcelainFiles is a porcelain-format line (e.g. " M foo.go", "?? bar.txt", "A new.go"). The function groups files by status and runs the appropriate git command for each group:

  • Staged new files (A): git reset HEAD -- <files>, then git clean -fd -- <files>
  • Tracked modifications/deletions (M, D, etc.): git reset HEAD -- <files>, then git checkout -- <files> (reset first to unstage any staged changes)
  • Untracked files/directories (??): git clean -fd -- <files>

The -d flag is required because git status --porcelain collapses untracked directories into a single "?? dir/" entry, and git clean without -d refuses to remove directories.

This is irreversible.

func FetchBranch added in v1.0.11

func FetchBranch(path, branch string) error

FetchBranch fetches a single branch from origin so that git checkout can create a local tracking branch from the remote ref. The -- separator ensures the branch name is always treated as a refspec and never misinterpreted as a flag (e.g. if it starts with -).

func ForcePush

func ForcePush(path string) error

ForcePush pushes the current branch to origin using --force-with-lease, which refuses to overwrite if the remote has commits we haven't seen. This is the safest form of force-push for history rewriting.

func HasStash

func HasStash(path string) (bool, error)

HasStash reports whether the repository has any stash entries.

func IsDefaultBranch

func IsDefaultBranch(path, defaultBranch string) (bool, error)

IsDefaultBranch reports whether the current branch equals the repo's default branch.

func IsDirty

func IsDirty(path string) (bool, error)

IsDirty reports whether the working tree has uncommitted changes, including untracked files.

func IsDirtyTrackedOnly

func IsDirtyTrackedOnly(path string) (bool, error)

IsDirtyTrackedOnly reports whether tracked files have modifications or staged changes. Untracked files are ignored (-uno flag). Use this for pull/checkout where untracked files pose no risk of conflict.

func IsGitRepo

func IsGitRepo(path string) bool

IsGitRepo reports whether the directory is the root of a git repository.

func Pull

func Pull(path string) (string, error)

Pull runs git pull on the current branch.

func Push

func Push(path string) error

Push pushes the current branch to origin. If no upstream is set yet, it sets one automatically.

func PushBranch

func PushBranch(path, branch string) error

PushBranch pushes a local branch to origin and sets upstream tracking.

func RemoteBranchExists

func RemoteBranchExists(path, branch string) bool

RemoteBranchExists reports whether a remote tracking branch exists.

func RenameBranch

func RenameBranch(path, oldName, newName string) error

RenameBranch renames a local branch from oldName to newName.

func RepoName

func RepoName(path string) string

RepoName returns the base directory name of a repository path.

func ResetHard

func ResetHard(path, ref string) error

ResetHard moves HEAD back by <ref> and discards all staged and working-tree changes. This is irreversible. Equivalent to: git reset --hard <ref>

func ResetMixed

func ResetMixed(path, ref string) error

ResetMixed moves HEAD back by <ref> and unstages all changes, leaving them as working-tree modifications. Equivalent to: git reset <ref>

func ResetSoft

func ResetSoft(path, ref string) error

ResetSoft moves HEAD back by <ref> (e.g. "HEAD~1") while keeping all changes staged in the index. Equivalent to: git reset --soft <ref>

func StageFiles

func StageFiles(path string, files []string) error

StageFiles stages specific files (by their path relative to the repo root).

func StashApply

func StashApply(path string) error

StashApply applies the most recent stash without removing it.

func StashList

func StashList(path string) ([]string, error)

StashList returns the stash entries for the repository (one line per entry). Returns nil if there are no stash entries.

func StashPop

func StashPop(path string) error

StashPop applies the most recent stash and removes it from the stash list.

func StashPush

func StashPush(path, message string) error

StashPush stashes all uncommitted changes (tracked and untracked) with an auto-generated message. Pass an empty message to use git's default.

func TrackedFiles

func TrackedFiles(path string) ([]string, error)

TrackedFiles returns all tracked files in the repository as porcelain-style lines with a " T " prefix (e.g. " T src/main.go") for display in the file picker.

func UntrackFiles

func UntrackFiles(path string, files []string) error

UntrackFiles removes files from the git index but keeps them on disk. Equivalent to: git rm --cached -- <files>

func UntrackedFiles

func UntrackedFiles(path string) ([]string, error)

UntrackedFiles returns all untracked, non-ignored files as porcelain-style lines (e.g. "?? scratch.txt") for display in the file picker.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL