简体   繁体   中英

How do I list git branches with commits only to a specific directory?

Our team commits to a big git repository.

Recently we've decided to export one of subdirectories (named framework) to a separate repo and remove all branches that contain commits only to that subdirectory.

How can I list such branches?

I've modified this advice to get:

for branch in `git for-each-ref --format="%(refname)" refs/remotes/origin `; do 
    git ls-tree -r --name-only $branch | grep -q "framework/" && echo $branch 
done

However this command returns branches with commits to other subdirectories too.

I've tried expanding this snippet:

for branch in `git for-each-ref --format="%(refname)" refs/remotes/origin `; do 
    if git ls-tree -r --name-only $branch | grep -q "framework/" ; then
        if ! git ls-tree -r --name-only $branch | grep -vq "framework/" ; then
            echo $branch 
        fi
    fi
done

However, this command prints nothing.

Since framework/ is part of your repo, all branches contain a directory named framework/ , so your git ls-tree command will always list something.


If you have a reference branch (say master or develop ), you can check if a branch has modified this directory since it forked from that branch:

# will give view an understandable view of what commits are part of this branch :
git log --graph --oneline master..$branch -- framework/

# will give you an int as output (easier to use in a script) :
git rev-list --count master..$branch -- framework/

You can use an alternative syntax to a..b if you want to exclude several "reference branches":

# count commits from $branch that modified 'framework/' since it forked
# either from master or from develop :
git rev-list --count ^master ^develop $branch -- framework/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM