git_cp_file.sh 842 B

12345678910111213141516171819202122232425
  1. #!/bin/bash
  2. # 从命令行参数获取分支名和正则表达式
  3. dev_branch=$1
  4. test_branch=$2
  5. grep_pattern=$3
  6. # 使用git diff命令查找test分支相对于dev分支修改的文件名,然后使用grep过滤文件名
  7. files=$(git diff --name-only ${dev_branch}..${test_branch} | egrep ${grep_pattern})
  8. # 使用for循环遍历文件名列表
  9. for file in $files; do
  10. # 判断文件是否存在于dev分支
  11. if git show ${dev_branch}:${file} &> /dev/null; then
  12. # 如果文件存在于dev分支,使用git checkout命令将文件从test分支复制到dev分支
  13. echo "git checkout ${test_branch} ${file}"
  14. git checkout ${test_branch} ${file}
  15. else
  16. # 如果文件不存在于dev分支
  17. echo "git show ${test_branch}:${file} > ${file}"
  18. git show ${test_branch}:${file} > ${file}
  19. git add ${file}
  20. fi
  21. done
  22. git status