1

In our team we had two repositories till now. One was Development and other was Production.

So we configured the gitconfig global file as default fetch from Production and default push to Development.

Now we have introduced a third repository named Common. Whoever clones from 'Common' should have default push configured as 'Common'. Since we have configured 'Development' as default push repository in global, the default push is pointing to 'Development' even inside 'Common' repository when it is cloned.

How can i make something like below in gitconfig global?

if (cloned repository from Production)
then 
    default push is Development

if  (cloned repository from Common)
then 
    default push is Common
SS Hegde
  • 729
  • 2
  • 14
  • 33
  • 1
    One way would be to set the `remote.pushDefault = Common` variable in the *local* config for the `Common` repo, since that will [override](https://git-scm.com/book/en/v2/Customizing-Git-Git-Configuration#_git_config) the value set in the global config. – Enrico Campidoglio Aug 02 '16 at 09:59
  • @EnricoCampidoglio: Now i have two default push paths after the change you recommended. – SS Hegde Aug 02 '16 at 10:05
  • @SSHedge Could you please elaborate? What command did you run? How does your `[remote]` config section look like? – Enrico Campidoglio Aug 02 '16 at 10:08
  • Executed below command inside the repository cloned from Common: `git config remote.origin.pushurl Common` But the following command gives me two push paths: `git remote -v origin Development (push) origin Common (push)` – SS Hegde Aug 02 '16 at 10:13
  • I see what you mean. The global `pushurl` variable can't be overridden locally; in fact, Git is going to show you both of them but only consider the global one. In order to remove the _local_ `pushurl` variable you can run `git config --unset remote.origin.pushurl`. Please, see my answer below for a different approach. – Enrico Campidoglio Aug 02 '16 at 11:37

1 Answers1

1

Assuming your global configuration file contains something like this:

[remote "origin"]
pushurl = http://url/to/development

here's one way to achieve what you want:

  1. Define a new remote in the Common repository that points to the same URL as origin:

    git remote add common http://url/to/common
    
  2. Set the remote.pushDefault variable in the local configuration file to common:

    git config remote.pushDefault common 
    

The remote.pushDefault setting tells Git to push using the URL defined in the specified remote instead of origin.

Here's from the documentation:

remote.pushDefault
The remote to push to by default. Overrides branch.<name>.remote for all branches, and is overridden by branch.<name>.pushRemote for specific branches.

In this case, the push URL for origin is defined in the global configuration file but the Common repository is going to use the push URL defined in its own remote, named common.

Enrico Campidoglio
  • 56,676
  • 12
  • 126
  • 154
  • If i do this change then this setting remains till i remove the cloned repo from Common. If i clone a new repo from Common then again i need to perform these step. Isn't it? – SS Hegde Aug 02 '16 at 11:47
  • Correct – the local config file of a repo [isn't shared remotely](http://stackoverflow.com/a/6548056/26396). But I guess you already have a way to share the global `remote.origin.pushurl` setting. These local settings could be shared in a similar fashion. – Enrico Campidoglio Aug 02 '16 at 11:59
  • Thanks Enrico. That does the job. – SS Hegde Aug 03 '16 at 09:16