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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2668886/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 08:16:16  来源:igfitidea点击:

Git: copy all files in a directory from another branch

gitfilecopybranch

提问by alexenko

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 脚本来做到这一点,但必须有一种更简单的方法,对吧?

回答by CB Bailey

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

回答by test30

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')