Git:设置一个只能获取的遥控器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7556155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Git: Set up a fetch-only remote?
提问by mtbkrdave
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
在配置了远程的 Git 存储库之一中运行时,我看到每个远程都具有 fetch 和 push 规范:
$ 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?
对于指向对等开发人员的远程,没有必要推送,而且 Git 无论如何都会拒绝推送到非裸存储库。有没有办法将这些遥控器配置为“仅获取”而没有推送地址或功能?
回答by Daniel Brockman
I don't think you can removethe push URL, you can only overrideit to be something other than the pull URL. So I think the closest you'll get is something like this:
我认为您不能删除推送 URL,您只能将其覆盖为拉取 URL 以外的内容。所以我认为你最接近的是这样的:
$ git remote set-url --push origin no-pushing
$ git push
fatal: 'no-pushing' does not appear to be a git repository
fatal: The remote end hung up unexpectedly
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 设置为no-pushing
,只要您的工作目录中没有同名文件夹,git 将无法定位。您实际上是在强迫 git 使用一个不存在的位置。
回答by Rodolfo Carvalho
Apart from changing the push URL to something invalid (e.g., git remote set-url --push origin DISABLED
), one can also use the pre-push
hook.
除了将推送 URL 更改为无效的内容(例如,git remote set-url --push origin DISABLED
)之外,还可以使用pre-push
钩子。
One quick way to stop git push
is to symlink /usr/bin/false
to be the hook:
停止的一种快速方法git push
是符号链接/usr/bin/false
成为钩子:
$ ln -s /usr/bin/false .git/hooks/pre-push
$ git push
error: failed to push some refs to '...'
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.
如果需要,使用钩子可以更细粒度地控制推送。有关.git/hooks/pre-push.sample
如何防止推送正在进行的工作提交的示例,请参见。
To prevent pushing to a specific branch or to limit pushing to a single branch, this in an example hook:
为了防止推送到特定分支或限制推送到单个分支,在示例钩子中:
$ cat .git/hooks/pre-push
#!/usr/bin/sh
# An example hook script to limit pushing to a single remote.
#
# This hook is called with the following parameters:
#
# -- Name of the remote to which the push is being done
# -- URL to which the push is being done
#
# If this script exits with a non-zero status nothing will be pushed.
remote=""
url=""
[[ "$remote" == "origin" ]]
A test repo with multiple remotes:
具有多个遥控器的测试仓库:
$ git remote -v
origin ../gitorigin (fetch)
origin ../gitorigin (push)
upstream ../gitupstream (fetch)
upstream ../gitupstream (push)
Pushing to origin
is allowed:
origin
允许推送到:
$ git push origin
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 222 bytes | 222.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To ../gitorigin
* [new branch] master -> master
Pushing to any other remote is not allowed:
不允许推送到任何其他遥控器:
$ git push upstream
error: failed to push some refs to '../gitupstream'
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.
请注意,pre-push
钩子脚本可以修改为,除其他外,向 stderr 打印一条消息,说明推送已被禁用。
回答by pkeller
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.
“Git 将拒绝推送到非裸存储库”的一般说法是不正确的。如果您尝试推送与远程存储库的检出工作目录位于同一分支上的更改,Git 只会拒绝推送到非裸远程存储库。
This answer gives a simple explanation: https://stackoverflow.com/a/2933656/1866402
这个答案给出了一个简单的解释:https: //stackoverflow.com/a/2933656/1866402
(I am adding this as an answer because I don't have enough reputation to add comments yet)
(我将此添加为答案,因为我还没有足够的声誉来添加评论)
回答by PaulMest
If you already have a remote set up and just want to prevent yourself from doing something like accidentally pushing directly to master
or release/production
, you can prevent this using git config
.
如果您已经有一个远程设置并且只想防止自己不小心直接推送到master
或 之类的事情release/production
,您可以使用git config
.
# prevent pushing to branch: master
$ git config branch.master.pushRemote no_push
# prevent pushing to branch: release/production
$ git config branch.release/production.pushRemote no_push
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.
为了记录,no_push
不是一个特殊的名字。它只是任何不存在的分支的名称。所以你可以使用$ git config branch.master.pushRemote create_a_pr_and_do_not_push_directly_to_master
它,它会工作得很好。
More info: git-config pushRemote
回答by Abhilash
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.
如果您可以控制存储库,则可以通过使用权限来实现这一点。获取存储库的用户不应该对主存储库具有写入权限。