Documentation
¶
Overview ¶
Package git provides helpers to execute git operations on local repositories.
Index ¶
- func AheadBehind(path string, fetch bool) (ahead, behind int, err error)
- func BranchExists(path, branch string) bool
- func Checkout(path, branch string) error
- func Commit(path, message string) (string, error)
- func CommitLog(path string, n int) ([]string, error)
- func CreateBranch(path, branch string) error
- func CurrentBranch(path string) (string, error)
- func DefaultBranch(path string) (string, error)
- func DeleteRemoteBranch(path, branch string) error
- func DirtyFiles(path string) ([]string, error)
- func DirtyFilesWithStatus(path string) ([]string, error)
- func DiscardChanges(path string) error
- func DiscardFiles(path string, porcelainFiles []string) error
- func FetchBranch(path, branch string) error
- func ForcePush(path string) error
- func HasStash(path string) (bool, error)
- func IsDefaultBranch(path, defaultBranch string) (bool, error)
- func IsDirty(path string) (bool, error)
- func IsDirtyTrackedOnly(path string) (bool, error)
- func IsGitRepo(path string) bool
- func Pull(path string) (string, error)
- func Push(path string) error
- func PushBranch(path, branch string) error
- func RemoteBranchExists(path, branch string) bool
- func RenameBranch(path, oldName, newName string) error
- func RepoName(path string) string
- func ResetHard(path, ref string) error
- func ResetMixed(path, ref string) error
- func ResetSoft(path, ref string) error
- func StageFiles(path string, files []string) error
- func StashApply(path string) error
- func StashList(path string) ([]string, error)
- func StashPop(path string) error
- func StashPush(path, message string) error
- func TrackedFiles(path string) ([]string, error)
- func UntrackFiles(path string, files []string) error
- func UntrackedFiles(path string) ([]string, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AheadBehind ¶
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 ¶
BranchExists reports whether a local branch with the given name exists.
func CommitLog ¶
CommitLog returns the last n commits as one-line entries (hash + subject). Each entry has the format "<short-hash> <subject>".
func CreateBranch ¶
CreateBranch creates and checks out a new branch from the current HEAD.
func CurrentBranch ¶
CurrentBranch returns the name of the currently checked-out branch.
func DefaultBranch ¶
DefaultBranch detects the default branch (main/master) for a repo. It tries origin/HEAD first, then falls back to probing local branches.
func DeleteRemoteBranch ¶
DeleteRemoteBranch deletes a branch on origin.
func DirtyFiles ¶
DirtyFiles returns the list of modified/untracked files.
func DirtyFilesWithStatus ¶
DirtyFilesWithStatus returns modified/untracked files keeping the full porcelain line (e.g. " M src/foo.php", "?? scratch.txt").
func DiscardChanges ¶
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
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
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 ¶
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 IsDefaultBranch ¶
IsDefaultBranch reports whether the current branch equals the repo's default branch.
func IsDirty ¶
IsDirty reports whether the working tree has uncommitted changes, including untracked files.
func IsDirtyTrackedOnly ¶
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 Push ¶
Push pushes the current branch to origin. If no upstream is set yet, it sets one automatically.
func PushBranch ¶
PushBranch pushes a local branch to origin and sets upstream tracking.
func RemoteBranchExists ¶
RemoteBranchExists reports whether a remote tracking branch exists.
func RenameBranch ¶
RenameBranch renames a local branch from oldName to newName.
func ResetHard ¶
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 ¶
ResetMixed moves HEAD back by <ref> and unstages all changes, leaving them as working-tree modifications. Equivalent to: git reset <ref>
func ResetSoft ¶
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 ¶
StageFiles stages specific files (by their path relative to the repo root).
func StashApply ¶
StashApply applies the most recent stash without removing it.
func StashList ¶
StashList returns the stash entries for the repository (one line per entry). Returns nil if there are no stash entries.
func StashPush ¶
StashPush stashes all uncommitted changes (tracked and untracked) with an auto-generated message. Pass an empty message to use git's default.
func TrackedFiles ¶
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 ¶
UntrackFiles removes files from the git index but keeps them on disk. Equivalent to: git rm --cached -- <files>
func UntrackedFiles ¶
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.