Listing each branch and its last revision's date in Git

后端 未结 11 716
傲寒
傲寒 2020-11-29 15:07

I need to delete old and unmaintained branches from our remote repository. I\'m trying to find a way with which to list the remote branches by their last modified date, and

相关标签:
11条回答
  • 2020-11-29 15:50

    Sorted remote branches and the last commit date for each branch.

    for branch in `git branch -r | grep -v HEAD`;do echo -e `git show --format="%ci %cr" $branch | head -n 1` \\t$branch; done | sort -r
    
    0 讨论(0)
  • 2020-11-29 15:51

    Just to add to the comment by @VonC, take your preferred solution and add it to your ~/.gitconfig alias list for convenience:

    [alias]  
        branchdate = !git for-each-ref --sort='-authordate' --format='%(refname)%09%(authordate)' refs/heads | sed -e 's-refs/heads/--'
    

    Then a simple "git branchdate" prints the list for you...

    0 讨论(0)
  • 2020-11-29 15:51

    Here is what I came up with after also reviewing this.

    for REF in $(git for-each-ref --sort=-committerdate --format="%(objectname)" \
        refs/remotes refs/heads)
    do
        if [ "$PREV_REF" != "$REF" ]; then
            PREV_REF=$REF
            git log -n1 $REF --date=short \
                --pretty=format:"%C(auto)%ad %h%d %s %C(yellow)[%an]%C(reset)"
        fi
    done
    

    The PREV_REF check is to remove duplicates if more than one branch points to the same commit. (As in a local branch that exist in the remote as well.)

    NOTE that per the OP request, git branch --merged and git branch --no-merged are useful in identifying which branches can be easily deleted.

    [https://git-scm.com/docs/git-branch]

    0 讨论(0)
  • 2020-11-29 15:53

    Here is what I use:

    git for-each-ref --sort='-committerdate:iso8601' --format=' %(committerdate:iso8601)%09%(refname)' refs/heads
    

    This is the output:

    2014-01-22 11:43:18 +0100       refs/heads/master
    2014-01-22 11:43:18 +0100       refs/heads/a
    2014-01-17 12:34:01 +0100       refs/heads/b
    2014-01-14 15:58:33 +0100       refs/heads/maint
    2013-12-11 14:20:06 +0100       refs/heads/d/e
    2013-12-09 12:48:04 +0100       refs/heads/f
    

    For remote branches, just use "refs/remotes" instead of "refs/heads":

    git for-each-ref --sort='-committerdate:iso8601' --format=' %(committerdate:iso8601)%09%(refname)' refs/remotes
    

    Building on n8tr's answer, if you are also interested in the last author on the the branch, and if you have the "column" tool available, you can use:

    git for-each-ref --sort='-committerdate:iso8601' --format='%(committerdate:relative)|%(refname:short)|%(committername)' refs/remotes/ | column -s '|' -t
    

    Which will give you:

    21 minutes ago  refs/remotes/a        John Doe
    6 hours ago     refs/remotes/b        Jane Doe
    6 days ago      refs/remotes/master   John Doe
    

    You may want to call "git fetch --prune" before to have the latest information.

    0 讨论(0)
  • 2020-11-29 15:56

    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 branches
    • branch -r prints all remote branches

    Function:

    branch() {
       local pattern="s/^..//"
       local arg=""
       if [[ $@ == "-r" ]]; then
          pattern="s/^..(.*?)( ->.*)?$/\1/"
          arg=" -r "
          echo '-r provided'
       fi
       for k in $(git branch $arg | perl -pe "$pattern"); do
          echo -e $(git show --pretty=format:"%Cgreen%ci %Cblue%cr%Creset" $k -- | head -n 1)\\t$k
       done | sort -r
    }
    
    0 讨论(0)
提交回复
热议问题