4

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?

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?

Luther Baker
  • 7,236
  • 13
  • 46
  • 64
  • What do you mean `'remote' path for my local working copy`? – manojlds Jan 04 '12 at 17:48
  • I agree that using `git remote set-url ` is the proper way to reconfigure a remote uri easily, but I went ahead and gave an answer on how to change these based on the environment. – James Jan 04 '12 at 21:23

2 Answers2

9

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}'"

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>'"

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

Lily Ballard
  • 182,031
  • 33
  • 381
  • 347
James
  • 1,754
  • 14
  • 22
  • Thanks James. That's pretty ingenious. – Luther Baker Jan 04 '12 at 21:48
  • This answer doesn't really explain what to do with these environment variables. Are they supposed to go into a config file? What would such a config file look like? – Ray Salemi Apr 12 '19 at 16:12
  • Ray, these are shell environment variables this should address how to set them https://unix.stackexchange.com/a/21600 – James Apr 14 '19 at 16:33
1

Interesting problem. Do all these repositories have the same "changing" remote? If so, I can think of a cheap trick. You can add a local hostname entry in /etc/hosts for the actual IP of the remote machine and make all your repositories point to the local name. Changing the entry in /etc/hosts will work. I don't like it but it might work.

Noufal Ibrahim
  • 71,383
  • 13
  • 135
  • 169
  • Most of my repos are housed in dropbox and the machine with the working directory likely has a connection to the dropbox account. If I decide to reorganize one or two parent directories in my dropbox account, I'd like to be able to reset something like GIT_LOCAL_REPOSITORY_ROOT - which would be associated with a remote address in my local repo(s). In this case, the hostname doesn't actually change. – Luther Baker Jan 04 '12 at 17:51
  • Oh okay. That will break my "solution" :) – Noufal Ibrahim Jan 04 '12 at 17:52
  • I was looking for a central way to handle the changing url of my home-server. In stead of messing with variables in the git-config, this is the more appropriate solution for me. Thanks! – Leon S. Apr 27 '16 at 20:25