阻止git分支被推送 [英] Block a git branch from being pushed

查看:404
本文介绍了阻止git分支被推送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是情况:

我在github.com上为我的开源应用程序提供公共存储库。但是,现在我想写一些不公开的特定代码(我可能会在我的应用程序的商业版本中使用它)。



我想我可以使用相同的存储库,我会在我的git仓库中创建一个私人分支,我不会推送。



但是,错误会发生。有没有办法阻止git将分支推送到远程服务器?



如果有更好的方法来处理这种情况,我当然会欢迎任何建议。 / p>

解决方案

一个稍微黑客的解决方案:在GitHub上创建一个与真实分支名称相同的虚拟分支,并确保它不会是一个快速前进的合并。这样,推送操作将失败。



下面是一个例子。

  $ git clone git@github.com:user / repo.git 
$ cd repo
$ git checkout -b secret
$ echo这只是一个阻止快进合并> dummy.txt
$ git add。
$ git commit -mDummy
$ git push origin secret

现在假分支已经建立好了,我们可以在本地重新创建它,以便与GitHub上的分支重新分开。

  $ git checkout master 
$ git branch -D secret
$ git checkout -b secret
$ echo这与GitHub分支不同> new-stuff.txt
$ git add。
$ git commit -m新东西

现在如果我们不小心尝试推送,它会失败并出现非快速转发合并错误:

  $ git push原始密码
转至git @ github。 com:user / repo.git
! [被拒绝]秘密 - >秘密(非快速转发)
错误:未能将一些文件推送到'git@github.com:user / repo.git'


Here's the situation:

I have a public repository for my open-source app on github.com. However, now I'd like to write some specific code that will not be public (I might use it in a commercial version of my application).

I figured I could use the same repository, and I'd create a "private" branch in my git repository that I wouldn't push.

But, mistakes happen. Is there some way to forbid git from ever pushing a branch to remote servers?

If there's a better way to handle this situation, I would of course welcome any suggestions.

解决方案

A slightly hackish solution: Make a dummy branch on GitHub with the same name as your real branch, and make sure it would not be a fast forward merge. That way, the push operation will fail.

Here's an example.

$ git clone git@github.com:user/repo.git
$ cd repo
$ git checkout -b secret
$ echo "This is just a dummy to prevent fast-forward merges" > dummy.txt
$ git add .
$ git commit -m "Dummy"
$ git push origin secret

Now that the dummy branch is set up, we can recreate it locally to diverge from the one on GitHub.

$ git checkout master
$ git branch -D secret
$ git checkout -b secret
$ echo "This diverges from the GitHub branch" > new-stuff.txt
$ git add .
$ git commit -m "New stuff"

Now if we accidentally try to push, it will fail with a non-fast forward merge error:

$ git push origin secret
To git@github.com:user/repo.git
! [rejected]        secret -> secret (non-fast forward)
error: failed to push some refs to ‘git@github.com:user/repo.git’

这篇关于阻止git分支被推送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆