简体   繁体   English

如何将许多扩展名定义的文件从一个远程存储库分支复制到另一个文件夹中的许多文件夹中?

[英]How can I copy many files, defined by extension, in many folders, from one remote repository branch to another?

It seems, I have changed not only my backend files on the remote branch, but also many frontend files. 看来,我不仅更改了远程分支上的后端文件,而且还更改了许多前端文件。 Now they are changed in my branch and I can't merge it to the master branch. 现在它们在我的分支中已更改,我无法将其合并到master分支。

How can I copy files chosen by extension from one branch into another? 如何将扩展名选择的文件从一个分支复制到另一个分支?

  1. I am on the feature branch. 我在功能分支上。
  2. The numerous *.html files in the project and on the feature branch are bad. 项目和功能分支中的大量* .html文件是错误的。
  3. There are many useful current changes in .java and .xml on the feature branch. 功能分支上的.java和.xml中有许多有用的最新更改。
  4. I want to get all *.html files from the current master branch and put them into the current project. 我想从当前master分支中获取所有* .html文件,并将它们放入当前项目中。 Or to the feature branch. 或到功能分支。

Git does not allow the partial merging of branches. Git不允许分支的部分合并。 That is, when you merge you need to merge all the information - you can't ignore some files and include others... (well, you can, but it's very bad practice and involves just pretending you did a merge). 也就是说,合并时,您需要合并所有信息-您不能忽略某些文件而包括其他文件...(可以,但是,这是非常糟糕的做法,只是假装您进行了合并)。

That said, there are several ways you can proceed. 也就是说,有几种方法可以进行。

You can ignore the fact that your branch is on a remote - in git branches are branches, irrespective of where they are. 您可以忽略分支位于远程的事实-在git分支中,分支是分支,无论它们在哪里。 A git pull / fetch will download your remote changes so you can work on them. git pull / fetch将下载您的远程更改,以便您可以进行更改。

1) Simple - Ignore history 1)简单-忽略历史记录

This involves taking all your changes in your feature branch, adding only those with the extension, and making a large commit with those changes on master : 这涉及在功能分支中进行所有更改,仅添加具有扩展名的更改,然后对master更改进行大量提交:

  • git checkout feature
  • git reset master..feature - reset to the intersection of master and feature git reset master..feature重置为masterfeature的交集
  • git add -- *.txt - add all the .txt files git add -- *.txt添加所有.txt文件
  • git commit
  • ( git reset --hard to eliminate the files you don't want) git reset --hard很难清除不需要的文件)
  • git merge

2) Complex - Preserving history 2)复杂-保存历史

  • Use git rebase -i to interactively break each commit into 2 - those that edit the .txt files (for example), and those that don't. 使用git rebase -i交互方式将每个提交分为2个 -例如编辑.txt文件的提交和不编辑.txt文件的提交
  • After, use git cherry-pick to take only the commits that you want into a new branch, called feature-txt-files-only for example. 之后,使用git cherry-pick仅将想要的提交带入新分支,例如, feature-txt-files-only称为feature-txt-files-only
  • Merge this with your master branch. 将其与您的master分支合并。

I will give an example for .py files. 我将举一个.py文件的例子。 Try to do the merge, then 尝试进行合并,然后

find -name '*.py' -print0|xargs -0 git checkout --ours --

or 要么

find -name '*.py' -print0|xargs -0 git checkout --theirs --

"Ours" means the local branch, and "theirs" means the remote branch (except when rebasing, when their meanings are swapped). “我们的”是指本地分支,“他们的”是指远程分支(重新定级时,交换其含义时除外)。

You can do this as many times as necessary for the file types you want to use to clobber the other changes. 对于要用来掩盖其他更改的文件类型,您可以根据需要多次执行此操作。

Then finish the merge in the normal way. 然后以正常方式完成合并。

The only problem is this might not work reliably with files that have been deleted on one of the branches but changed on the other branch. 唯一的问题是,这对于在一个分支上已删除但在另一个分支上已更改的文件可能无法可靠地工作。 So watch out for such cases. 因此,请注意此类情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 git 将文件从一个分支复制到另一个分支? - How can I copy files from a branch to another using git? 如何将提交从一个分支复制到另一个分支? - How can I copy commits from one branch to another? 如何将分支从一个GitHub存储库复制到另一个? - How to copy a branch from one GitHub repository to another? 如何将文件从一个分支复制到另一个分支? - How to copy files from one branch to another branch? 如何将一个远程分支覆盖而不是合并到另一个分支? - How can I overwrite, not merge, one remote branch into another branch? 我如何从多个存储库执行git扩展拉 - How can I execute a git extension pull form many repository 如何将一个远程分支更改为另一远程分支的精确副本? - How to change one remote branch into an exact copy of another remote branch? 如何选择不将某些文件/文件夹从Git存储库中拉到本地副本 - How can I choose not to pull certain files/folders FROM a Git repository TO my local copy 我可以使用Git子模块将一个分支的副本保留在同一存储库中另一个分支的目录中吗? - Can I use Git submodules to keep a copy of one branch inside a directory of another branch in the same repository? 如何将所有文件和文件夹从一个分支替换/复制到GIT中的另一个现有分支 - How Can I replace/copy all file and folder from one branch to another existing branch in GIT
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM