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?

2
  • What do you mean 'remote' path for my local working copy? Commented Jan 4, 2012 at 17:48
  • I agree that using git remote set-url <name> <newurl> 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. Commented Jan 4, 2012 at 21:23

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks James. That's pretty ingenious.
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, these are shell environment variables this should address how to set them unix.stackexchange.com/a/21600
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.

3 Comments

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.
Oh okay. That will break my "solution" :)
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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.