Git:设置仅获取远程?

发布于 2024-12-06 16:29:07 字数 290 浏览 3 评论 0原文

当我在配置了远程的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

心不设防 2024-12-13 16:29:07

我认为您无法删除推送 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

您将推送 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:

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

御弟哥哥 2024-12-13 16:29:07

除了将推送 URL 更改为无效的内容(例如,git remote set-url --push origin DISABLED)之外,还可以使用 pre-push 挂钩。

停止 git Push 的一种快速方法是将符号链接到 /usr/bin/false 来作为钩子:

$ ln -s /usr/bin/false .git/hooks/pre-push
$ git push
error: failed to push some refs to '...'

如果需要,使用钩子可以对推送进行更细粒度的控制。有关如何防止推送正在进行的提交的示例,请参阅 .git/hooks/pre-push.sample 。

为了防止推送到特定分支或限制推送到单个分支,在示例钩子中:

$ 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:
#
# $1 -- Name of the remote to which the push is being done
# $2 -- URL to which the push is being done
#
# If this script exits with a non-zero status nothing will be pushed.

remote="$1"
url="$2"

[[ "$remote" == "origin" ]]

具有多个远程的测试存储库:

$ git remote -v
origin  ../gitorigin (fetch)
origin  ../gitorigin (push)
upstream        ../gitupstream (fetch)
upstream        ../gitupstream (push)

允许推送到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

不允许推送到任何其他远程:

$ git push upstream
error: failed to push some refs to '../gitupstream'

请注意,可以修改 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 the pre-push hook.

One quick way to stop git push is to symlink /usr/bin/false to be the hook:

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

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:
#
# $1 -- Name of the remote to which the push is being done
# $2 -- URL to which the push is being done
#
# If this script exits with a non-zero status nothing will be pushed.

remote="$1"
url="$2"

[[ "$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:

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

別甾虛僞 2024-12-13 16:29:07

一般的说法“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)

倾`听者〃 2024-12-13 16:29:07

如果您已经进行了远程设置,并且只是想防止自己执行某些操作,例如意外直接推送到 masterrelease/product,则可以使用 来防止这种情况发生git 配置

# 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

根据记录,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 or release/production, you can prevent this using 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.

More info: git-config pushRemote

云朵有点甜 2024-12-13 16:29:07

如果您可以控制存储库,则可以通过使用权限来实现此目的。获取存储库的用户不应具有主存储库的写入权限。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文