You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

按扩展名跨Git分支复制文件方案及分支合并受阻解决方法

Alright, let's break down these two Git problems with practical, step-by-step solutions that get straight to the point.

问题1:从远程仓库的一个分支复制指定扩展名、分布在多文件夹下的文件到另一个分支

Here's how to safely pull only the files you need across branches, even when they're scattered in subfolders:

  1. First, sync your local repo with all remote branches
    Make sure you have the latest version of the source branch from the remote:

    git fetch origin
    
  2. Switch to your target branch
    If you're copying to an existing branch:

    git checkout your-target-branch
    

    If you need to create a new target branch first:

    git checkout -b new-target-branch
    
  3. Check out the specific files from the remote source branch
    Use Git's recursive wildcard (**) to match all subfolders, followed by your target file extension. For example, if you want all .java files from origin/source-branch:

    git checkout origin/source-branch -- '**/*.java'
    

    The quotes around the path are crucial—they prevent your shell from expanding the wildcard before Git processes it.

  4. Commit the changes
    Verify the files were copied correctly with git status, then commit:

    git add .
    git commit -m "Copy all .java files from source-branch to target-branch"
    

    If you need to push to the remote target branch:

    git push origin your-target-branch
    

问题2:修复feature分支的误修改,保留有效更改并同步master的HTML文件

This scenario is all about selectively restoring files while keeping your valuable work intact. Here's what to do:

  1. Update your local master branch first
    Make sure you have the latest version of master to pull the correct HTML files from:

    git checkout master
    git pull origin master
    
  2. Switch back to your feature branch

    git checkout feature
    
  3. Overwrite all HTML files with the version from master
    Use the same recursive wildcard trick to grab every .html file from master and replace the ones in your feature branch:

    git checkout master -- '**/*.html'
    
  4. Verify your changes are preserved
    Run git status—you should see that your .java and .xml files are still marked as modified (or staged, if you had already committed them), while the .html files are now updated to match master.

  5. Commit the fix
    Stage and commit the HTML file updates:

    git add '**/*.html'
    git commit -m "Sync all .html files with master; preserve .java/.xml changes"
    

    Now your feature branch is ready to merge into master without conflicts from the accidental HTML edits.

内容的提问来源于stack exchange,提问作者Gangnus

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 11:05:19