wizzfizz94
wizzfizz94

Reputation: 1556

How to git checkout all files from another branch matching a specific pattern?

I'd like to copy all files from another git branch matching a certain pattern.

Upvotes: 1

Views: 1757

Answers (2)

wizzfizz94
wizzfizz94

Reputation: 1556

I was able to solve using a bash command i created. First git checkout the branch you wish to copy to then run the following command:

git ls-tree -r --name-only <branch> | grep <pattern> | while read line; do git checkout <branch> -- $line; done

First this lists all files in the source branches directory using git-ls-tree then uses grep to filter only for files that match the Pattern. Then using a while loop reads each line and copys the file across using git checkout.

Upvotes: 1

eftshift0
eftshift0

Reputation: 30156

Say you need all js files in all directories. That could be done like this:

git checkout the-other-branch -- '*/*.js'

That will put all those files on the working tree and also on the index ready to be committed, with no relation to the branch that you are checking out from... your branch pointer doesn't move.

Upvotes: 2

Related Questions