简体   繁体   English

从特定提交开始将现有项目添加到GitHub

[英]Adding an existing project to GitHub starting at specific commit

A local project is ready to be pushed to a GitHub repository. 准备将本地项目推送到GitHub存储库。 The GitHub repository was initialized WITHOUT README and license. GitHub存储库已初始化,没有自述文件和许可证。

From commit C1 to Cn - C1 is the first/initial commit, and Cn is the n -th/latest commit -, the local project contains sensitive. 从提交C1Cn - C1是第一个/初始提交,而Cn是第n个/最新提交-本地项目包含敏感内容。 But from the commit Cn+1 onwards the sensitive data is masked. 但是从提交Cn+1开始,敏感数据被屏蔽了。

For example, C1..Cn+3 is my currently local git repository: 例如, C1..Cn+3是我当前的本地git存储库:

C1 < C2 < C3 < .. < Cn < Cn+1 < Cn+2 < Cn+3

I want to push the commit Cn+1 and onwards. 我想将提交Cn+1向前推进。 So, the remote repository should have C1'..C3' 因此,远程存储库应具有C1'..C3'

C1 < C2 < C3 < .. < Cn < Cn+1 < Cn+2 < Cn+3
                         C1' < C2' < C3'

Where C1' in the remote repository is Cn+1 in my local repository. 远程存储库中的C1'在我的本地存储库中为Cn+1

How to add the local project to GitHub starting at the commit Cn+1 ? 如何从提交Cn+1开始将本地项目添加到GitHub? The following push should ignore the commits C1..Cn . 以下推送应忽略提交C1..Cn

I've already tried git push origin <sha>:<branch> but it push the whole project. 我已经尝试过git push origin <sha>:<branch>但是它可以推送整个项目。 Rebase didn't quite fit for me either. Rebase也不太适合我。

There are a few ways to do what you want, but the most important point is this: you won't be able to use the Github repo seamlessly . 有几种方法可以执行您想要的操作,但是最重要的一点是: 您将无法无缝使用Github存储库 So if your intention is to use it as a way to share new commits or fetch contributions from other people and then put them back on your original history, that's not going to be possible in the traditional way. 因此,如果您打算将其用作共享新提交或从其他人那里获取稿件,然后再将其放回您的原始历史记录的方式,那么传统方式将无法实现。

Making a new repository 制作一个新的存储库

The simplest way is to just copy your working tree with all your files in it, in the repository state you want, init that as a new git repo, make a single initial commit, and then push everything to Github. 最简单的方法是,在想要的存储库状态下,复制包含所有文件的工作树,将其init为新的git repo,进行一次初始提交,然后将所有内容推送到Github。

You'll be able to use the new repo as normal, and nothing of the original history will be shared or preserved. 您将能够照常使用新的存储库,并且不会共享或保留任何原始历史记录。

Making a public branch 公开分支

Another option is to make a new branch on the current repo as an orphan ie a branch with a no root, and do the same thing as making a new repo above - copy files, make a new starting commit, and push this branch to Github. 另一种选择是在当前存储库上创建一个新分支作为一个孤儿,即一个没有根的分支,并执行与在上面创建新存储库相同的操作-复制文件,进行新的开始提交,然后将该分支推送到Github 。

You'll then be able to share and fetch changes made on the public repository, but getting them back onto your main branch will be more difficult - you can use one-way merges into the private branch, and make all edits on the public branch only, which will keep your history without sharing it. 然后,您将能够共享和获取在公共存储库上所做的更改,但是将它们返回到主分支将更加困难-您可以使用单向合并到私有分支中,并在公共分支上进行所有编辑仅,这将保留您的历史记录而无需共享。

You'll need to be very careful with merge direction if you want to take this approach, though. 但是,如果要采用这种方法,则在合并方向时必须非常小心 If you push a merge in the wrong direction, the history will become public. 如果您向错误的方向进行合并,则历史记录将公开。

For more info, see this question: Push a branch of a git repo to a new remote (github), hiding its history 有关更多信息,请参见以下问题: 将git repo的分支推送到新的远程(github),隐藏其历史记录

Addendum: why isn't this possible? 附录:为什么这不可能?

Ben Jackson explains it in the answers to the above question: a commit's hash is partly based on every commit preceding it in the repo. 本·杰克逊(Ben Jackson)在上述问题的答案中对此进行了解释:提交的哈希部分基于回购中位于它之前的每个提交。 Since what you want to do would publicly create a new commit (without a parent), git will refuse to treat it as though it was really your old commit, because the hashes won't match. 由于您要执行的操作将公开创建一个新的提交(没有父项),因此git将拒绝将其视为确实是您的旧提交,因为哈希不会匹配。

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

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