5 votes

Folder-Dependent Git Configurations Using Conditional Includes

Tags: git

5 comments

  1. [3]
    jdsalaro
    Link
    Hey there folks! I finally decided to finish a short tutorial I've been wanting to put out there about how to best structure and handle git repositories which are spread throughout different Git...

    Hey there folks!
    I finally decided to finish a short tutorial I've been wanting to put out there about how to best structure and handle git repositories which are spread throughout different Git backends as well as how to selectively configure them using conditionally included git configurations.
    Let me know what you think (Y)!

    1 vote
    1. [2]
      teaearlgraycold
      Link Parent
      Hey, looks like a pretty well refined piece on information. But I think I’m glad it’s not something I’ll ever need to know! My git life is a simple one compared to yours.

      Hey, looks like a pretty well refined piece on information. But I think I’m glad it’s not something I’ll ever need to know! My git life is a simple one compared to yours.

      3 votes
      1. jdsalaro
        Link Parent
        A backhanded compliment if I ever saw one! Thank you for the kind feedback, though, I appreciate it!

        But I think I’m glad it’s not something I’ll ever need to know!

        A backhanded compliment if I ever saw one!

        Thank you for the kind feedback, though, I appreciate it!

        2 votes
  2. [2]
    zoroa
    Link
    I also have to juggle git identities, and have worked through a couple approaches for managing them. I started with just manually setting my user.name, user.email, and sshCommand. Then moved to...

    I also have to juggle git identities, and have worked through a couple approaches for managing them.

    I started with just manually setting my user.name, user.email, and sshCommand.


    Then moved to manually setting user.name and user.email but relying on my ~/.ssh/config to infer the correct ssh key for an identity.:

    Host work-git
    	Hostname git-service.com
    	IdentityFile ...
    

    And then I could just clone with git clone git@work-git:equalsraf/filemark.git. I ended up moving off of this because it would break some tooling I used that needed the actual hostname.


    I evaluated trying to use includeIf to automatically manage the right identity, but ended up not using it.

    Git currently only supports using includeIf to check paths ( [includeIf "gitdir:/path/to/repo/"]) and the url of a remote ([includeIf "hasconfig:remote.*.url:https://example.com/**"]). But:

    • I deploy my git config to several machines where silo-ing into directories per identity didn't make a ton of sense.
    • I need multiple identities for the same remote hostname (i.e. github.com), and pattern matching based on organization/user is more trouble than it's worth.

    I ended up settingly on keeping a collection of small .gitconfig files that each configure an identity (name, email, and sshCommand if needed).

    And then I clone with an alias that effectively does git clone -c include.path=/path/to/identity.gitconfig ...

    1. jdsalaro
      Link Parent
      I might be missing something, but in this regard you could apply the same approach if you structure you minimally order your repositories: . ├── github │ ├── gitconfig │ ├── ID01 │ │ ├── gitconfig...

      I need multiple identities for the same remote hostname

      I might be missing something, but in this regard you could apply the same approach if you structure you minimally order your repositories:

      .
      ├── github
      │   ├── gitconfig
      │   ├── ID01
      │   │   ├── gitconfig
      │   │   └── repo
      │   ├── ID02
      │   │   ├── gitconfig
      │   │   └── repo
      │   ├── ID03
      │   │   ├── gitconfig
      │   │   └── repo
      │   └── repo
      ├── gitlab
      │   ├── gitconfig
      │   └── repo
      ├── gitlab-university
          └── gitconfig
      
      

      Any repo you clone into ID01 will use the identity in ID01/gitconfig. You can, of course, clone the same repo to ID01, ID02 and ID03 and you will use the corresponding identity seemlesly without needing to do anything.

      Which is basically just a slightly more sophisticated way of doing:

      I ended up settingly on keeping a collection of small .gitconfig files that each configure an identity (name, email, and sshCommand if needed).
      git clone -c include.path=/path/to/identity.gitconfig

      Unless your number of identities is unknown and quite large, and without knowing the specifics of your usecase, the approach I propose seems to work here and actually, fundamentally, reflects what you've already achieved.

      1 vote