Git:设置仅获取远程?
当我在配置了远程的 Git 存储库之一中运行 git remote -v 时,我看到每个远程都具有获取和推送规范:
$ git remote -v
<remote-name> ssh://host/path/to/repo (fetch)
<remote-name> ssh://host/path/to/repo (push)
对于指向同行开发人员的远程,没有需要推送,Git 无论如何都会拒绝推送到非裸存储库。有没有办法将这些遥控器配置为“仅获取”,没有推送地址或功能?
When I run git remote -v
in one of my Git repositories that has a remote(s) configured, I see that each remote has both fetch and push specs:
$ git remote -v
<remote-name> ssh://host/path/to/repo (fetch)
<remote-name> ssh://host/path/to/repo (push)
For remotes that point to peer developers there's no need to push, and Git will refuse to push to a non-bare repository anyway. Is there any way to configure these remotes as "fetch-only" with no push address or capabilities?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我认为您无法删除推送 URL,您只能覆盖它,使其成为除拉取 URL 之外的其他内容。所以我认为您会得到的最接近的结果是这样的:
您将推送 URL 设置为
no-pushing
,只要您的文件夹中没有同名的文件夹即可。工作目录,git将无法找到。您实际上是在强制 git 使用不存在的位置。I don't think you can remove the push URL, you can only override it to be something other than the pull URL. So I think the closest you'll get is something like this:
You are setting the push URL to
no-pushing
, which, as long as you don't have a folder of the same name in your working directory, git will not be able to locate. You are essentially forcing git to use a location that does not exist.除了将推送 URL 更改为无效的内容(例如,
git remote set-url --push origin DISABLED
)之外,还可以使用pre-push
挂钩。停止 git Push 的一种快速方法是将符号链接到 /usr/bin/false 来作为钩子:
如果需要,使用钩子可以对推送进行更细粒度的控制。有关如何防止推送正在进行的提交的示例,请参阅 .git/hooks/pre-push.sample 。
为了防止推送到特定分支或限制推送到单个分支,在示例钩子中:
具有多个远程的测试存储库:
允许推送到
origin
:不允许推送到任何其他远程:
请注意,可以修改
pre-push
挂钩脚本,以向 stderr 打印一条消息,说明推送已被禁用。Apart from changing the push URL to something invalid (e.g.,
git remote set-url --push origin DISABLED
), one can also use thepre-push
hook.One quick way to stop
git push
is to symlink/usr/bin/false
to be the hook:Using a hook allows for more fine-grained control of pushes if desirable. See
.git/hooks/pre-push.sample
for an example of how to prevent pushing work-in-progress commits.To prevent pushing to a specific branch or to limit pushing to a single branch, this in an example hook:
A test repo with multiple remotes:
Pushing to
origin
is allowed:Pushing to any other remote is not allowed:
Note that the
pre-push
hook script can be modified to, among other things, print a message to stderr saying the push has been disabled.一般的说法“Git 将拒绝推送到非裸存储库”是不正确的。仅当您尝试推送与远程存储库的签出工作目录位于同一分支上的更改时,Git 才会拒绝推送到非裸远程存储库。
这个答案给出了一个简单的解释: https://stackoverflow.com/a/2933656/1866402
(我将其添加为一个答案,因为我还没有足够的声誉来添加评论)
The general statement "Git will refuse to push to a non-bare repository" is not true. Git will only refuse to push to a non-bare remote repository if you are attempting to push changes that are on the same branch as the remote repository's checked-out working directory.
This answer gives a simple explanation: https://stackoverflow.com/a/2933656/1866402
(I am adding this as an answer because I don't have enough reputation to add comments yet)
如果您已经进行了远程设置,并且只是想防止自己执行某些操作,例如意外直接推送到
master
或release/product
,则可以使用来防止这种情况发生git 配置
。根据记录,
no_push
不是一个特殊的名称。这只是任何不存在的分支的名称。所以你可以使用 $ git configbranch.master.pushRemote create_a_pr_and_do_not_push_directly_to_master ,它会工作得很好。更多信息: git-config PushRemote
If you already have a remote set up and just want to prevent yourself from doing something like accidentally pushing directly to
master
orrelease/production
, you can prevent this usinggit config
.For the record,
no_push
is not a special name. It's just the name of any nonexistent branch. So you could use$ git config branch.master.pushRemote create_a_pr_and_do_not_push_directly_to_master
and it would work just fine.More info: git-config pushRemote
如果您可以控制存储库,则可以通过使用权限来实现此目的。获取存储库的用户不应具有主存储库的写入权限。
If you have control over the repository, you can achieve this by making use of permissions. The user who is fetching repository shouldn't have write permissions on master repository.