git 如何配置git避免意外的git push

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/527833/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 03:34:45  来源:igfitidea点击:

How to configure git to avoid accidental git push

git

提问by Daniel Fanjul

After git clone, the config in the new repo looks like:

git clone 后,新仓库中的配置如下所示:

remote.origin.url=<some url>
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master

Then, I can execute "git pull" and "git push". But I'm interested in only do "git pull", because I want to push into another repo.

然后,我可以执行“git pull”和“git push”。但我只对“git pull”感兴趣,因为我想推入另一个回购。

One thing I can do is:

我能做的一件事是:

git add remote repo-for-push <some other url>
git push repo-for-push master

But I would like to configure git to use default and distinct repositories for pull and push, i.e:

但我想将 git 配置为使用默认和不同的存储库进行拉取和推送,即:

git pull # pulls from origin
git push # pushes into repo-for-push, avoiding accidental push into the origin

How can this be configured? Thanks in advance.

如何配置?提前致谢。

EDIT:
Basically, I want to setup the default push repo to be different from the default fetch/pull repo.

编辑:
基本上,我想将默认推送存储库设置为与默认获取/拉取存储库不同。

回答by iny

Looks like

好像

git config remote.origin.receivepack /bin/false

Makes push to remote origin fail.

使推送到远程源失败。

回答by Phil Miller

In version 1.6.4, Git gained the ability to have a remote pull from one URL and push to another, using the remote.name.pushurlconfig setting. I can imagine weird behavior if the push-repository doesn't track the pull-repository, but I suspect Git will just try to fast-forward the push-repository from the current/tracking/matching branch(es) without regard for what it will pull when it asks the remote of the same name.

1.6.4版本中,Git 获得了使用remote.name.pushurl配置设置从一个 URL 远程拉取并推送到另一个 URL 的能力。如果推送存储库不跟踪拉存储库,我可以想象奇怪的行为,但我怀疑 Git 只会尝试从当前/跟踪/匹配分支快进推送存储库而不考虑它是什么当它询问同名遥控器时会拉。

For instance, if you wanted to pull via anonymous git protocol, but push via SSH (maybe you need a value off a SecurID token or something to authenticate):

例如,如果你想通过匿名 git 协议拉取,但通过 SSH 推送(也许你需要一个 SecurID 令牌或其他东西的值来进行身份验证):

[remote "myremote"]
    url = git://server/path
    pushurl = user@server:/path

回答by Emil Sit

I'm not sure you can actually do this in git today. The implementation of git-fetch (in builtin-fetch.c) and git-push (in builtin-push.c) both call the internal function remote_get(NULL) to identify the default repository to pull-from/push-to.

我不确定你今天真的可以在 git 中做到这一点。git-fetch(在 builtin-fetch.c 中)和 git-push(在 builtin-push.c 中)的实现都调用内部函数 remote_get(NULL) 来标识要拉取/推送到的默认存储库。

One option would be to create an alias that specifies your desired repo. For example:

一种选择是创建一个别名来指定您想要的存储库。例如:

git config --add alias.mypush "push repo-for-push"

Then you could:

那么你可以:

git mypush

to push to your desired repo. Not precisely what you want, of course. (You may also consider the --repo argument to push; see http://kerneltrap.org/mailarchive/git/2008/10/7/3537694for a recent doc update that clarifies the --repo argument.)

推送到您想要的回购。当然,不完全是你想要的。(您也可以考虑使用 --repo 参数进行推送;请参阅http://kerneltrap.org/mailarchive/git/2008/10/7/3537694了解最近的文档更新,该更新澄清了 --repo 参数。)

回答by skiphoppy

If you could do all of your pushing from another branch, I think you could configure that branch to have its own separate repository to push to:

如果您可以从另一个分支进行所有推送,我认为您可以将该分支配置为拥有自己的单独存储库以推送到:

git checkout master
git branch outbound
git remote add destination <some url>
git config branch.outbound.remote destination

I haven't tried this, and you may need to do some more work to create a complete solution. It also might not work for you, if you have to push from master.

我还没有尝试过这个,你可能需要做更多的工作来创建一个完整的解决方案。如果您必须从主人那里推动,它也可能不适合您。

回答by skiphoppy

Wrap the "git" command in something that eats the push argument. Off the top of my head I wrote this:

将“git”命令包装在吃 push 参数的东西中。在我的头顶上,我写道:

~$ cat /usr/local/bin/git
#!/bin/bash

# git wrapper
# prevents pushing to repository

declare -a args
declare msg=''
while [ $# -gt 0 ]
do
    if [ "" != 'push' ]; then
        args=( "${args[@]}" "" )
    else
        msg="No pushing"
    fi
   shift
done

if [ ${#msg} -gt 0 ]; then
    echo "$msg"
fi
/usr/bin/git "${args[@]}"  

Just be sure to have the wrapped command in your path before the "real" git command.

只要确保在“真正的”git 命令之前在您的路径中包含包装的命令。