首页 > git > 将git branch推送到其他存储库

将git branch推送到其他存储库 (Push git branch to other repository)

问题

我有以下用例:

我们有一个私有gitlab存储库,想要推送到公共仓库github来开源。但私有存储库和公共存储库应包含某些文件的不同版本。例如不同的文档等...

我试图将opensource_branch从私人gitlab仓库推送到公共github仓库:

git push origin_github opensource_branch:master 

但结果是之前的所有其他历史提交也被推送(其中包含不公开的文档)。

任何想法如何解决这个问题,而无需手动复制和粘贴?

谢谢。

PS:我当然搜索了类似的用例,但没有找到这个。关于推送到其他遥控器的所有其他问题都没有要求在遥控器上根本看不到特定的历史性提交。

解决方法

Git基于哈希。这意味着完整性与每次提交一起保留。

提交链接到它的父级,这就是为什么你经常看到Git日志的图形,其中箭头指向前一个提交(这似乎不直观)。在您的情况下,您可能有这样的历史记录:

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

您想要在远程存储库上推送的内容仅来自978e43c。不幸的是,这个提交有一个父级4837aab。你不能隐藏它既不告诉Git也不要推翻所有的历史。

解决方案1:在孤立分支上重新登录

一种解决方案是使提交978e43c成为孤儿,换句话说:没有父母。

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

然后你将有一个全新的分支,不包括你的私人文档。

此解决方案有效,但它有几个缺点:

解决方案2:改变历史 git filter-branch

您可以使用以下命令从整个历史记录中删除敏感文档:

export PRIVATE ./documentation/private-api

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

问题

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. But the private repository and the public repository shall contain different versions of some files. E.g. different documentation, etc...

I have tried to push the opensource_branch from the private gitlab repo to the public github repo:

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. 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.

解决方法

Git is based on hashes. 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). 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. Unfortunately this commit has a parent which is 4837aab. You cannot hide it neither tell Git to not push all the history before.

Solution 1: Rebase on an orphan branch

One solution is to make the commit 978e43c an orphan, said differently: with no parent.

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

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
相似信息