git-trim

repository·main·Indexed 18 days ago

https://github.com/jasonmccreary/git-trim

A command-line utility for cleaning up Git repositories by removing merged, squash-merged, pruned, untracked, or stale branches. Version 0.2.1 supports local and remote branch operations, dry runs, and custom branch exclusions via the gt.exclude Git configuration option.

Tokens
2K
Snippets
17
Records
17
Agent score
14%

What's inside git-trim

  1. Use git-trim to remove branches

    main

    The git trim command allows you to clean up your Git repository by removing various types of branches. You can combine multiple options to perform complex cleanups.

    Branch Removal Options

    • --pruned or -p: Removes local branches where the remote branch no longer exists.
    • --merged or -m: Removes local branches already merged into the current branch.
    • --stale or -s: Removes local branches without commits in the last 3 months.
    • --squashed or -q: Removes local branches already squash-merged into the current branch.
    • --untracked or -u: Removes local branches not tracking a remote branch.
    • --reset[=remote]: Removes all local branches except those existing on the specified remote (defaults to origin). Requires confirmation.
    • --all: Removes all local branches except the current branch. Requires confirmation.

    Modifier Options

    • --tracked or -t: When used with a removal option, also removes the remote tracking branch.
    • --remote or -r: Operates on remote branches instead of local branches.
    • --dry-run: Lists the branches that would be deleted without actually removing them.
    git trim --merged --stale
    git trim --stale --remote
    git trim --merged --tracked
    git trim --dry-run
  2. Install git-trim

    main

    You can install git-trim using several methods depending on your environment:

    Basic Install

    Save the git-trim script into a directory included in your PATH (e.g., /usr/local/bin).

    NPM

    Install globally via npm:

    npm install --global git-trim

    Oh-My-Zsh

    1. Clone the repository into your custom plugins directory: git clone https://github.com/jasonmccreary/git-trim.git $ZSH_CUSTOM/plugins/git-trim
    2. Add git-trim to your plugins list in ~/.zshrc.
    npm install --global git-trim
  3. Update git-trim

    main

    To update git-trim to the latest version:

    Via NPM

    npm update --global git-trim

    Via Oh-My-Zsh

    cd $ZSH_CUSTOM/plugins/git-trim
    git fetch
    git pull

    Basic Install

    Overwrite your local copy of the git-trim script with the latest version.

    npm update --global git-trim
  4. Use git-trim CLI to manage git branches

    main

    The git-trim CLI tool allows you to clean up your git repository by removing various types of local or remote branches. It supports several modes of operation, such as removing merged branches, pruned branches (where the remote no longer exists), stale branches (no commits in 3 months), and more.

    By default, if no options are provided, git-trim performs a prune operation (removing local branches whose remote tracking branch is gone).

    git trim [options]
  5. Remove remote branches instead of local with --remote

    main

    By default, git-trim operates on local branches. If you pass the --remote flag, the tool will target remote branches instead. When using --remote, the tool will also perform a git fetch --all -p to ensure the remote state is up to date.

    git trim --remote --merged
  6. Remove tracking information with --tracked

    main

    When using --tracked in combination with other flags (like --merged or --squashed), git-trim will also attempt to remove the upstream tracking relationship from the local branch before deleting the branch itself.

    git trim --merged --tracked
  7. Configure excluded branches with gt.exclude

    main

    By default, git trim never removes the current branch. To prevent other specific branches (like dev or staging) from being removed, set the gt.exclude Git configuration option. You can use space-delimited strings or glob patterns (wildcards).

    This can be set locally or globally using git config.

    git config gt.exclude "dev staging"
    git config gt.exclude "dev staging save/*"
  8. Exclude branches from git-trim using git config

    main

    You can prevent git-trim from deleting specific branches by setting the gt.exclude configuration in your git config. git-trim will check this list before performing any deletions.

    For local branches, it performs an exact match. For remote branches, it checks if the branch name ends with the excluded string.

    git config gt.exclude branch-to-keep
    git config --add gt.exclude another-branch-to-keep
  9. Reset local branches to match a remote with --reset

    main

    Use the --reset option to remove all local branches that do not exist on a specified remote.

    • By default, it uses origin.
    • You can specify a different remote using --reset=[remote_name].

    This operation requires manual confirmation in the terminal.

    git trim --reset=upstream
  10. Remove pruned branches with --pruned

    main

    Use the --pruned flag to remove local branches that are tracking a remote branch that no longer exists (i.e., the remote branch was deleted). This is the default behavior if no other options are specified.

    git trim --pruned
  11. Remove squash-merged branches with --squashed

    main

    Use the --squashed flag to identify and remove branches that have been squash-merged into the current branch. It uses git cherry to detect if the changes from the branch are already present in the current branch's history.

    git trim --squashed