简体   繁体   English

将git分支推送到其他存储库

[英]Push git branch to other repository

I have the following use case: 我有以下用例:

We have a private gitlab repository and want to push to a public github repo to open source it. 我们有一个私有的gitlab存储库,并希望推送到公共github存储库以对其进行开源。 But the private repository and the public repository shall contain different versions of some files. 但是私有存储库和公共存储库应包含某些文件的不同版本。 Eg different documentation, etc... 例如,不同的文档等。

I have tried to push the opensource_branch from the private gitlab repo to the public github repo: 我试图将opensource_branch从私有gitlab库推送到公共github库:

git push origin_github opensource_branch:master 

But the result is that all other historical commits before are also pushed (which contain documentation which shall not be public). 但是结果是之前的所有其他历史提交也被推送了(其中包含不应公开的文档)。

Any ideas how to solve this without manually copy & paste all the time? 有什么想法可以解决这个问题而无需一直手动复制和粘贴吗?

Thanks. 谢谢。

PS: I have of course searched SO for similar use cases but did not find exactly this one. PS:我当然在SO中搜索了类似的用例,但没有找到确切的用例。 Every other question regarding pushing to other remotes does not have the requirement that particular historic commits shall not be visible on the remote at all. 关于推送到其他遥控器的其他所有问题均不要求特定的历史提交根本不在遥控器上可见。

You can create a new branch from opensource_branch without inheriting its commits. 您可以从opensource_branch创建一个新分支,而无需继承其提交。 git checkout has an option --orphan to create such a branch. git checkout有一个选项--orphan创建这样的分支。 Remove the private files and folders, commit and then push. 删除私有文件和文件夹,提交然后推送。

git checkout --orphan new_branch opensource_branch
# delete the private files
git rm -rf <private_folders_files>
git commit -m 'the first commit of new_branch'
# publish new_branch to github
git push origin_github new_branch:master 

If you will apply commits from opensource_branch to new_branch in future, be careful not to introduce the private files. 如果将来将提交从opensource_branch提交到new_branch,请注意不要引入私有文件。

Git is based on hashes. Git基于哈希值。 It means that the integrity is preserved along with each commit. 这意味着完整性与每次提交一起保留。

A commit is linked to its parent, that's why you often see graphs of Git log with arrows pointing to the previous commit (which does not seem intuitive). 一个提交链接到它的父提交,这就是为什么您经常看到Git日志的图形带有指向上一个提交的箭头(看起来不直观)的原因。 In your case you may have an history like this: 您的情况可能是这样的:

$ git log --graph --oneline
* f329c58 Fix (HEAD -> master)
* 9d13dc2 Another fix 
* 5641ac5 Spelling
* 978e43c Remove private documentation 
* 4837aab Fix code
...
* 1bcd23a Initial commit

What you want to push on the remote repository is only from 978e43c . 您要在远程存储库上推送的内容仅来自978e43c Unfortunately this commit has a parent which is 4837aab . 不幸的是,此提交的父级为4837aab You cannot hide it neither tell Git to not push all the history before. 您无法隐藏它,也不会告诉Git之前不推送所有历史记录。

Solution 1: Rebase on an orphan branch 解决方案1:基于孤立分支

One solution is to make the commit 978e43c an orphan, said differently: with no parent. 一种解决方案是使提交978e43c成为孤儿, 978e43c :没有父母。

git checkout --orphan public
git rm -rf . # Clear the working directory
git commit --allow-empty -m "Initial commit"
git rebase master..978e43c 

Then you will have a whole new branch which does not include your private documentation. 这样您将拥有一个全新的分支,其中不包含您的私人文档。

This solution works but it has several drawback: 此解决方案有效,但有几个缺点:

Solution 2: Alter history with git filter-branch 解决方案2:使用git filter-branch更改历史记录

You can simply remove your sensitive documentation from the whole history with: 您可以使用以下方法从整个历史记录中删除敏感文档:

export PRIVATE ./documentation/private-api

git filter-branch -f \
    --prune-empty \
    --tag-name-filter cat \
    --tree-filter 'rm -rf $PRIVATE' \
    -- --all

不知道这是否可以解决您的确切问题,但是您可以在推送后尝试压缩提交?

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

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