Git:从另一个分支复制目录中的所有文件

时间:2020-12-14 16:02:44

How do I copy all files in a directory from another branch? I can list all of the files in that directory by doing

如何从另一个分支复制目录中的所有文件?我可以列出该目录中的所有文件

git ls-tree master:dirname

I can then copy all of the files individually by doing

然后,我可以通过执行单独复制所有文件

git checkout master -- dirname/filename

However, using wildcards has so far been a total fail. This does nothing:

但是,到目前为止,使用通配符已经完全失败了。这没有任何作用:

git checkout master -- dirname/*.png

Though I guess I can use a bash script to do that, there has to be an easier way, right?

虽然我想我可以使用bash脚本来做到这一点,但必须有一个更简单的方法,对吧?

2 个解决方案

#1


202  

As you are not trying to move the files around in the tree, you should be able to just checkout the directory:

由于您没有尝试在树中移动文件,因此您应该只需检查目录:

git checkout master -- dirname

#2


14  

If there are no spaces in paths, and you are interested, like I was, in files of specific extension only, you can use

如果路径中没有空格,并且您感兴趣,就像我一样,只在特定扩展名的文件中,您可以使用

git checkout otherBranch -- $(git ls-tree --name-only -r otherBranch | egrep '*.java')

#1


202  

As you are not trying to move the files around in the tree, you should be able to just checkout the directory:

由于您没有尝试在树中移动文件,因此您应该只需检查目录:

git checkout master -- dirname

#2


14  

If there are no spaces in paths, and you are interested, like I was, in files of specific extension only, you can use

如果路径中没有空格,并且您感兴趣,就像我一样,只在特定扩展名的文件中,您可以使用

git checkout otherBranch -- $(git ls-tree --name-only -r otherBranch | egrep '*.java')