简体   繁体   English

Git将母版合并到无提交历史的孤儿中

[英]Git merge master into orphan without commit history

Given an orphan branch without history, how would I incorporate changes done on master branch, since the moment of creation of the orphan, without copying all commits history when pushing the orphan remotely. 给定一个无历史记录的orphan分支,自创建孤儿以来,我将如何合并在master分支上所做的更改,而在远程推动该孤儿时不复制所有提交历史记录。

A <- B <- C <- D  

        orphan(created from state B) <-X <- Y

I would like to bring c and d to the orphan branch and then x and y to master. 我想将cd带入孤立分支,然后将xy掌握。

If I do checkout orphan, merge master or rebase master orphan gets the commits, but also all history tree. 如果我checkout orphan, merge master或重新设置rebase master孤儿将获取提交,而且还会获取所有历史记录树。 such as when I push orphan to a remote server, everybody will be able to see all master's history as well. 例如当我将“孤儿”推送到远程服务器时,每个人也都可以看到所有master's历史记录。

Also later I would like to merge orphan back into master, bringing commits x and y there. 同样,稍后我想将orphan合并回master,将提交xy引入那里。

Edit: 编辑:

Now merging orphan into master works ok with git merge 现在使用git merge将孤儿合并到master作品中

A <- B <- C <- D <------X <- Y
                       /  
        orphan <-X <- Y 

But, merging master back into orphan either puts the entire master history into orphan (such as becomes X preceded by B ) or with cherry picking, but than I need to skip the merge-commits and also get a lot more conflicts 但是,master合并回orphan或者将整个母版历史放入孤儿中(例如,在X之前加B ),或者在进行樱桃采摘时,但是我需要跳过合并提交并获得更多冲突

Grafts are built for exactly this. 嫁接正是为此而构建的。

root=$(git rev-list my-orphan-branch --max-parents=0)  # get the orphaned-branch root
echo $(git rev-parse $root B) >.git/info/grafts        # locally remember its real parent

and now all the local commands will know about the ancestry but it will remain repo-local, push and fetch won't export it. 现在所有本地命令都将知道祖先,但是它将保持回购本地,push和fetch将不会导出。

Based on the comments and what you are intending, I'm going to suggest an alternate workflow/design to avoid the need to do super-ordinary Git things. 根据评论和您的打算,我将建议一种替代的工作流程/设计,以避免进行超常规Git事情。

It sounds like there are some special config files or settings with passwords or similar sensitive information that you don't want to pass back to the remote, that are baked into your compiled files in the repo. 听起来好像有一些特殊的配置文件或带有密码或类似敏感信息的设置,您不想传递回远程,这些文件已存储在仓库中的已编译文件中。 Why not separate those out from the baked in code? 为什么不将它们与代码中的代码分开呢? If these files are already separate, then you can just .gitignore them. 如果这些文件已经分开,则可以.gitignore它们。 Regardless, it's better to have both a standard default that is compiled in or a default config file that gets distributed with the repo (and/or the application itself), and then have the code check for the presence of a custom config file, which is saved outside the repo or ignored with .gitignore . 无论如何,最好是既有编译的标准默认值,也有随存储库(和/或应用程序本身)一起分发的默认配置文件,然后让代码检查自定义配置文件的存在,保存在.gitignore外或用.gitignore

Without knowing more about your setup (linux/PC/Mac, programming language, frameworks, etc.) it's difficult to give further guidance - for example, with Visual Studio development for C#, there is a Properties.Settings file that can be setup. 如果不了解有关您的设置的更多信息(Linux / PC / Mac,编程语言,框架等),很难给出进一步的指导-例如,对于C#的Visual Studio开发,存在可以设置的Properties.Settings文件。 The defaults become a part of the compiled code, but a class gets auto-generated that will save out any changes to a user-specific file and reload it when the application launches, but this is in the user home directory, and not in the development folders. 默认值成为编译代码的一部分,但是会自动生成一个类,该类将保存对用户特定文件的所有更改,并在应用程序启动时重新加载它,但这位于用户主目录中,而不是在开发文件夹。 Another method would be having the file locally, and using .gitignore to have Git not transfer it between repos. 另一种方法是在本地存储文件,并使用.gitignore来使Git不在存储库之间传输文件。

I think this method will be better for you long-term, though might incur a short-term cost to update your design for better config management. 我认为这种方法从长远来看会更好,尽管可能会花费短期成本来更新设计以实现更好的配置管理。 My concern is that if you need some non-standard Git wizardry every time you need to push changes will cause times where someone forgets to take the extra steps and pushes the sensitive info up to the remote. 我担心的是,如果每次需要推送更改时都需要一些非标准的Git向导,则会导致某些时候人们忘记采取额外的步骤,并将敏感信息推送到远程。

Try this: 尝试这个:

git checkout orphan
git merge --no-commit --squash master
git commit

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

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