I need to delete old and unmaintained branches from our remote repo. I'm trying to find a way with which to list the remote branches by their last modified date, and I can't.
Does someone know of an easy way to list remote branches this way?
I need to delete old and unmaintained branches from our remote repo. I'm trying to find a way with which to list the remote branches by their last modified date, and I can't.
Does someone know of an easy way to list remote branches this way?
Here is what I use:
This is the output:
For remote branches, just use "refs/remotes" instead of "refs/heads":
You may want to call "git fetch --prune" before to have the latest information.
Here's a function you can add to your bash_profile to make this easier.
Usage when in a git repository:
branch
prints all local branchesbranch -r
prints all remote branchesFunction:
Here is what I came up with after also reviewing this.
The
PREV_REF
check is to remove duplicates if more than one branch points to the same commit. (As in local branch that exist in the remote as well.)NOTE that per the OP request,
git branch --merged
andgit branch --no-merged
are useful in identifying which branches can be easily deleted. [https://git-scm.com/docs/git-branch]Sorted remote branches and the last commit date for each branch.
I made two variants, based on VonC's answer.
My first variant:
This handles local & remote branches (-a), handles detached-head state (the longer sed command, though the solution is kind of crude -- it just replaces the detached branch info with the keyword HEAD), adds in the commit subject (%s), and puts things into columns via literal pipe characters in the format string and passing the end result to
column -t -s "|"
. (You could use whatever as the separator, as long as it's something you don't expect in the rest of the output.)My second variant is quite hacky, but I really wanted something that still has an indicator of "this is the branch you're currently on" like the branch command does.
This turns the * that marks the current branch into a keyword, and when the loop body sees the keyword it instead sets a flag and outputs nothing. The flag is used to indicate that an alternate formatting should be used for the next line. Like I said, totally hacky, but it works! (Mostly. For some reason my last column is getting outdented on the current branch line. But I really should get back to doing actual work instead of tweaking this more.)
Building off of Olivier Croquette, I like using a relative date and shortening the branch name like this:
Which gives you output:
I recommend making a bash file for adding all your favorite aliases and then sharing the script out to your team. Here's an example to add just this one:
Then you can just do this to get a nicely formatted and sorted local branch list: