简体   繁体   English

Git-分支以引入新文件而不删除旧文件

[英]Git - branch to introduce new files without deleting old files

I have an unusual git question, hoping someone else has done this before and can provide insight... 我有一个不寻常的git问题,希望其他人以前已经做过,可以提供见解...

I'm working on workflow to add files to an existing git project. 我正在努力将文件添加到现有git项目中。 Through a series of automated steps, I was hoping to create a branch that is completely empty, introduce new files, commit those files to the remote branch, then have the user manually merge the new files so that the new changes don't automatically overwrite/damage anything in the existing project. 通过一系列自动化步骤,我希望创建一个完全为空的分支,引入新文件,将这些文件提交到远程分支,然后让用户手动合并新文件,以使新更改不会自动覆盖/损坏现有项目中的任何内容。

I've tried git checkout --orphan , which doesn't do what I want b/c it just creates a "new" history that, when merged, replaces the existing branch's contents. 我尝试了git checkout --orphan ,它并没有实现我想要的功能,它只是创建了一个“新”历史记录,合并git checkout --orphan替换现有分支的内容。 You can't merge these the way you would normally merge changes. 您无法像通常合并更改那样合并它们。

I've also tried doing a normal git checkout -b emptybranch , then manually deleting all of the files and introducing the new files. 我还尝试了正常的git checkout -b emptybranch ,然后手动删除了所有文件并引入了新文件。 The problem with this case is that I don't want existing files to be deleted on merge - I only want new files to be included into the new project, or conflicts to be made visible for the project owner to merge themselves. 这种情况下的问题是,我不希望在合并时删除现有文件-我只希望将新文件包含到新项目中,或者使冲突对于项目所有者可见以使其自身合并。

What I want is the addition/merge of new files without deleting anything existing from the existing branch (and the new branch only contains the new files, nothing else from the existing branch). 我想要的是添加/合并新文件,而不删除现有分支中现有的任何内容(新分支仅包含新文件,现有分支中没有其他内容)。 Any assistance would be greatly appreciated. 任何帮助将不胜感激。

TL;DR executive summary TL; DR执行摘要

I think you do want git checkout --orphan , but you want it followed by git rm -r . 我认为您确实需要git checkout --orphan ,但是您想要它紧随其后是git rm -r . (and perhaps also rm -rf of any untracked files this leaves behind). (也许还有rm -rf留下的所有未跟踪文件)。

Description 描述

I've tried git checkout --orphan , which doesn't do what I want b/c it just creates a "new" history that, when merged, replaces the existing branch's contents. 我尝试了git checkout --orphan ,它并没有实现我想要的功能,它只是创建了一个“新”历史记录,合并git checkout --orphan替换现有分支的内容。

That's not quite right. 那不是很正确。 What --orphan does is set things up so that the next commit on the now-current branch is itself a root commit . --orphan所做的是进行设置,以便当前分支上的下一个提交本身就是root提交 Using --orphan is otherwise the same as using -b , ie, the index remains full of whatever it has at the moment. 否则,使用--orphan-b相同,即, 索引将保持当前已满的状态。 The work-tree is similarly undisturbed. 工作树同样不受干扰。

If you then also empty out the index ( git rm -r . at the top level), and optionally clean out the work-tree as well (if there are any remaining untracked files), then from this point on, only new files you git add from the work-tree will be in the next commit. 如果然后清空索引(在顶层使用git rm -r . ),还可以选择清理工作树(如果还有任何剩余的未跟踪文件),那么从现在开始, 只有新文件来自工作树的git add将在下一次提交中。 (You might want to commit just a README-ish file first, as is often done in the initial root commit of a new, totally-empty repository, for the same reasons that people make such a commit in a new repository.) (由于人们在新存储库中进行此类提交的相同原因,您可能希望首先仅提交自述文件,就像在新的完全空的存储库的初始根提交中所做的那样。)

Later, if and when you go to git merge this branch, Git now (as of 2.9) rejects the attempt unless you add --allow-unrelated-histories . 以后,如果并且当您使用git merge此分支时,Git现在(从2.9版开始)会拒绝该尝试,除非您添加--allow-unrelated-histories This is because there is no merge base between the orphan branch and the other branches in your repository. 这是因为孤立分支与存储库中的其他分支之间没有合并基础 Adding --allow-unrelated-histories tells Git to use a synthetic empty commit (actually, the empty tree ) as the merge base. 添加--allow-unrelated-histories告诉Git使用合成的空提交(实际上是空树 )作为合并基础。 Hence during the merge, all files in both branch tips are viewed as newly-added, and if any two files are identified (ie, have the same path name in both tip commits), you will get an add/add conflict on them and be forced to resolve this manually. 因此,在合并期间,两个分支提示中的所有文件都被视为新添加的,并且如果标识了两个文件(即,两个提示提交中具有相同的路径名),您将在它们上发生添加/添加冲突,并且被迫手动解决此问题。 Given your problem description, it sounds like this is what you wnat. 给定您的问题描述,听起来这就是您所拥有的。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM