如何使用Git远程URL的环境变量 [英] How to use environment variable for Git remote URL

查看:109
本文介绍了如何使用Git远程URL的环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我保留了一小部分我自己的git存储库,不时地,我想更改存储主存储库的机器或目录。不幸的是,这会导致存储库的所有工作副本中断。

I maintain a small set of my own git repositories and from time to time, I'd like to change the machine or directory where I'm storing the 'master' repositories. Unfortunately, this causes all the working copies of the repository to break.

有没有办法在本地工作副本的远程路径中使用环境变量?

Is there a way to use an environment variable in a 'remote' path for my local working copy?

如果没有,有没有一个简单的方法我可以欺骗Git(禁止扩展一个变量的强力sh脚本,删除并重新分配到远程位置,等等)在推拉时扩展变量?

If not, is there a simple way I can trick Git (barring a brute force sh script that expands a variable, removes and reassigns it to a remote location before pushing, etc.) into expanding a variable when pushing and pulling?

推荐答案

您可以将配置变量通过环境传递给git,你很棘手,做你想要的。这只有在没有设置GIT_CONFIG的情况下才有效。在您的shell环境的启动脚本中,您可以设置以下内容:

You can pass configuration variables to git through the environment which will let you be tricky and do what you want. This will only work if you don't have GIT_CONFIG set. In your shell environment's startup scripts you could setup something like:

GIT_ORIGIN_REMOTE=git://github.com/gitster/git.git
GIT_CONFIG_PARAMETERS="'remote.origin.url=${GIT_ORIGIN_REMOTE}'"

确保已经删除.git / config中的远程源URL配置行。这将导致使用 origin remote的所有操作与 git://github.com/gitster/git.git 。现在您只需更改启动环境脚本中的GIT_ORIGIN_REMOTE即可。注意GIT_CONFIG_PARAMETERS的格式,重要的是您的配置选项是单引号的。如果要从环境中传递多个配置选项到Git,请执行以下格式:

Make sure you have removed the remote origin url config line in .git/config. This will cause all operations that use the origin remote to operate with git://github.com/gitster/git.git. Now all you have to do is change the GIT_ORIGIN_REMOTE in your startup environment script if your directory changes. Notice the format of GIT_CONFIG_PARAMETERS, it is important that your config options are single quoted. If you want to pass more than one configuration option to Git from the environment follow the form of:

GIT_CONFIG_PARAMETERS="'<config_variable>=<value>' '<config_variable>=<value>'"

应该能够使用这个信息设置环境你如何看待合适。我没有真正看到这个记录,只是通过源代码发现,所以YMMV取决于你使用的Git版本。参考: Git令牌代码& Git解析代码

You should be able to use this info to setup the environment how you see fit. I haven't actually seen this documented, just found it through the source, so YMMV depending on the version of Git you're using. Ref: Git Tokenizing Code & Git Parsing Code

这篇关于如何使用Git远程URL的环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆