开发者

Git: copy all files in a directory from another branch

开发者 https://www.devze.com 2022-12-27 04:40 出处:网络
How do I copy all files in a directory from another branch? I can list all of the files in that directory by doing

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?


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


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


To copy the directory without tracking it:

git restore --source master dirname


In my case this simplest solution to get files from the origin branch directory was:

  • origin - remote repository alias(origin by default) sourceBranchName
  • branch with required files in directory
  • sourceBranchDirPath - relative/absolute path to the required directory with

files

git checkout origin/sourceBranchName -- sourceBranchDirPath

Example:

git checkout origin/validation_fix -- src/test/java/validation/

Result was all files from origin/validation_fix branch by src/test/java/validation/ relative path in the draft mode(uncommited)


git checkout sourceBranchName :

can checkout everything (root directory) from sourceBranch to your local current branch.

I tested by myself. Thx!

-- Shizheng

0

精彩评论

暂无评论...
验证码 换一张
取 消