按扩展名跨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:
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 originSwitch to your target branch
If you're copying to an existing branch:git checkout your-target-branchIf you need to create a new target branch first:
git checkout -b new-target-branchCheck 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.javafiles fromorigin/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.
Commit the changes
Verify the files were copied correctly withgit 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:
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 masterSwitch back to your feature branch
git checkout featureOverwrite all HTML files with the version from master
Use the same recursive wildcard trick to grab every.htmlfile from master and replace the ones in your feature branch:git checkout master -- '**/*.html'Verify your changes are preserved
Rungit status—you should see that your.javaand.xmlfiles are still marked as modified (or staged, if you had already committed them), while the.htmlfiles are now updated to match master.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

