如何配置git以避免意外的git推送

时间:2022-06-21 19:30:09

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

在git clone之后,新repo中的配置如下所示:

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.

编辑:基本上,我想设置默认的push repo与默认的fetch / pull repo不同。

5 个解决方案

#1


Looks like

git config remote.origin.receivepack /bin/false

Makes push to remote origin fail.

使推送到远程原点失败。

#2


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.pushurl config 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的功能。如果push-repository没有跟踪pull-repository,我可以想象出奇怪的行为,但我怀疑Git会尝试从当前/跟踪/匹配分支中快速推送push-repository,而不考虑它是什么当它询问遥控器的同名时会拉。

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

#3


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)来标识pull-from / push-to的默认存储库。

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/3537694 for a recent doc update that clarifies the --repo argument.)

推动你想要的回购。当然,这并不是你想要的。 (您也可以考虑推送--repo参数;请参阅http://kerneltrap.org/mailarchive/git/2008/10/7/3537694以获取最新的文档更新,以澄清--repo参数。)

#4


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.

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

#5


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 [ "$1" != 'push' ]; then
        args=( "${args[@]}" "$1" )
    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.

只需确保在“real”git命令之前在路径中包含wrap命令。

#1


Looks like

git config remote.origin.receivepack /bin/false

Makes push to remote origin fail.

使推送到远程原点失败。

#2


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.pushurl config 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的功能。如果push-repository没有跟踪pull-repository,我可以想象出奇怪的行为,但我怀疑Git会尝试从当前/跟踪/匹配分支中快速推送push-repository,而不考虑它是什么当它询问遥控器的同名时会拉。

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

#3


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)来标识pull-from / push-to的默认存储库。

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/3537694 for a recent doc update that clarifies the --repo argument.)

推动你想要的回购。当然,这并不是你想要的。 (您也可以考虑推送--repo参数;请参阅http://kerneltrap.org/mailarchive/git/2008/10/7/3537694以获取最新的文档更新,以澄清--repo参数。)

#4


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.

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

#5


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 [ "$1" != 'push' ]; then
        args=( "${args[@]}" "$1" )
    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.

只需确保在“real”git命令之前在路径中包含wrap命令。