Skip to content

Instantly share code, notes, and snippets.

@Aupajo
Last active July 16, 2017 06:55
Show Gist options
  • Save Aupajo/d0f02dd24f2b460922561b5611bf8989 to your computer and use it in GitHub Desktop.
Save Aupajo/d0f02dd24f2b460922561b5611bf8989 to your computer and use it in GitHub Desktop.

A better git branch

I'll often use branches to try out experiments, ideas, and to separate different trains of thought. Not every branch gets merged – in fact, it's quite common for me to make several commits on a branch, look at the end result, and throw the branch away without ever merging it (branching is cheap and ideas are cheap, but bad code is expensive). As a result, I can end up with a lot of branches. This makes the output of git branch difficult to sort through:

$ git branch
* account-org-association
  acdx-blog-posts
  add-static-resource-support
  alter-component-naming
  changeset-details-in-org-migration
  checkable-categories
  client-soap
  client-time-zone
  comments-url-in-org-migration-json
  comparison-cache-env
 …another 70+ more…

When I'm listing my branches, I want to see an indication of freshness. Which branches have I worked on recently, and what did I last do on it?

Introducing freshness:

$ git freshness
* account-org-association                          75 minutes ago          Link orgs directly to accounts
  notification-settings                            2 hours ago             WIP
  make-changes-default                             12 days ago             Make migrations redirect to changes after creation
  staging                                          12 days ago             Display a list of components during the review phase
  master                                           3 weeks ago             Add migration target labels for source and destination
  project-ordering                                 4 weeks ago             Sort project names in nav
  checkable-categories                             4 weeks ago             Allow a user to bulk-select a type of metadata
  search                                           4 weeks ago             Support search in comparisons
  refactor-selectable-component                    4 weeks ago             Refactor SelectableComponent
  fix/intermittent-failing-spec                    8 weeks ago             Explicitly specify Salesforce type in specs
…etc.…

This lists the branches by most recently changed first, including how long ago the branch was changed and what the last commit message was. I often pipe this to head to pull up a list of the most recent branches I've worked on.

Installing

To use it, edit your ~/.gitconfig and add the following to the [alias] section:

freshness = "!f() { git for-each-ref --sort=-committerdate refs/heads/ --format='%(HEAD) %(color:cyan)%(refname:short)%(color:reset) | %(committerdate:relative)%(color:reset) | %(subject)' | column -s '|' -t; }; f"
@danielocallaghan
Copy link

danielocallaghan commented May 8, 2017

👍 looks good! added that today

@aspett
Copy link

aspett commented May 16, 2017

Quite nice to use in conjunction with tac so that the most recent is at the bottom (useful when there are many branches)

git freshness | tac

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment