• Stars
    star
    366
  • Rank 116,547 (Top 3 %)
  • Language
    Shell
  • License
    MIT License
  • Created almost 3 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

git-trim header


git trim

A command to quickly remove merged, pruned, untracked, or stale branches within a repository.

Usage

git trim
git trim --pruned
git trim -p
    # Removes local branches where its remote branch no longer exists

git trim --merged
git trim -m
   # Removes local branches already merged into the current branch

git trim --stale
git trim -s
   # Removes local branches without commits in the last 3 months

git trim --untracked
git trim -u
    # Removes local branches not tracking a remote branch

git trim --reset[=remote]
    # Removes all local branches except those existing on remote which defaults to "origin" (requires confirmation)

git trim --all
    # Removes all local branches except the current branch (requires confirmation)

Of course, you may combine these options where appropriate. For example, git trim --merged --stale, would remove both local branches which have been merged with the current branch or are stale.

There are two additional options which may also be combined with the options above: --tracked and --remote.

When the --tracked (or -t) option is used, the remote tracking branch is also removed. For example, git trim --merged --tracked will remove local branches merged into the current branch, and remove the remote branch it's tracking (if any).

When the --remote (or -r) option is used, git trim will remove remote branches instead of local branches. For example, git trim --stale --remote will remove remote branches without commits in the last 3 months (stale local branches will not be removed).

Finally, you may also append the --dry-run option to any git-trim command to simply list the branches which would be deleted instead of actually removing them.

Installation

Basic install

The preferred installation method is to simply save the git-trim script somewhere included in your path. For example, copy git-trim into an existing included path like /usr/local/bin, or add the parent directory to your PATH environment.

Install via NPM

npm install --global git-trim

Install via ZSH

Oh-My-Zsh

  1. Run: git clone https://github.com/jasonmccreary/git-trim.git $ZSH_CUSTOM/plugins/git-trim
  2. Add git-trim to your plugin list: edit ~/.zshrc and change plugins=(...) to plugins=(... git-trim)

Updating

Once installed, the easiest way to update is to overwrite your local copy with the latest contents of the git-trim script.

Updating via NPM

npm update --global git-trim

Updating via ZSH

Oh-My-Zsh

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

Configuration

By default, git trim will never remove the current branch. However, depending on your branching strategy, you may have additional branches you never want to remove. For example, a dev or staging branch.

If so, you may set a gt.exclude option to your Git configuration, either locally or globally, with a space delimited string of any additional branches you want to exclude from removal.

git config gt.exclude "dev staging"
    # Always exclude the "dev" and "staging" branches from removal

Disclaimer

Some of the options in this command remove branches without warning. Once a branch is removed, it might not be recoverable. You are solely responsible when running this command.

Credits

There are thousands of commands and aliases for cleaning up branches sprinkled across the internet. I'll specifically link the commands and posts shared in this Twitter thread, another project for inspiring the name, and git-open which I used as a code reference.

And a special thanks to Caneco for the logo.

Contributing

Feel free to open an issue or pull request to help contribute to the project. Currently the only code style requirement is an indentation of 2 spaces.

License

Copyright Jason McCreary. Licensed under MIT.

More Repositories

1

laravel-test-assertions

A set of helpful assertions when testing Laravel applications.
PHP
319
star
2

gh

gh - A shell script to open the GitHub page for a repository.
62
star
3

local-docker-stack

This repository contains the Docker stack for my local LAMP development
Shell
56
star
4

start-testing-laravel

An app to demo testing Laravel.
PHP
26
star
5

what-the-fake

Example app for my Laracon 2023 talk.
PHP
25
star
6

dotfiles

Local system files to quickly set up my command line, shell, and Git preferences.
Shell
18
star
7

test-double

A simple helper method to make using Mockery easier.
PHP
12
star
8

start-testing-php

A sample project for my "Start Testing PHP" workshop.
12
star
9

orfail

PHP OrFail Trait.
PHP
9
star
10

ludicrous-speed

A demo of PHP + Redis + nginx
PHP
8
star
11

furry-umbrella

A Laravel application used in my conference workshops
PHP
7
star
12

og-image-generator

PHP
6
star
13

you-dont-know-git

Sample repo for Git talk.
PHP
6
star
14

expect

BDD-style assertions for PHP.
PHP
5
star
15

processcon-ci

Demo project for ProcessCon '23
PHP
5
star
16

tasty-brisket

Examples using GitHub Actions for PHP projects.
PHP
5
star
17

jmacs-hot-chicken

A modern PHP app for ordering Hot Chicken
Vue
4
star
18

bluecasts

PHP
3
star
19

BaseLaravel-Streamlines

A sample application to demo the practices from BaseLaravel
PHP
3
star
20

laravel-stubs

2
star
21

oxc

Type `oxc` to open Xcode with a workspace, project, or file from the command line.
Shell
2
star
22

looselycoupledpodcast.com

The Loosely Coupled Podcast Website
Blade
2
star
23

javascript-cookie-object

A JavaScript class for encapsulating common HTTP Cookie methods.
JavaScript
1
star
24

sample.com

PHP
1
star
25

testing-strategies

Code from my talk at LonghornPHP 2021.
PHP
1
star
26

blog-posts

Posts from my blog.
1
star
27

pspec

PSpec is a modern, BDD-style testing framework for PHP.
PHP
1
star
28

turbo-zonda

A sample Laravel application to demo the Laravel Fixer.
PHP
1
star
29

legendary-goggles

CodePalousa Workshop
1
star
30

laravel-fixer-demo

A demo repo for the Laravel Fixer
PHP
1
star
31

json-sample-data

Sample data for testing the JSON grammar. Currently simple. Eventually more complete.
1
star