为所有子文件夹设置git配置值 [英] Set git config values for all child folders

查看:135
本文介绍了为所有子文件夹设置git配置值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道可以设置覆盖用户级配置的per-repo配置(即 /path/to/my/repo/.gitconfig 覆盖〜/的.gitconfig )。是否可以设置覆盖给定文件夹的所有子文件夹的用户级别设置的git配置? I.e.,我有

  | --topLevelFolder1 
| --\
| ---。gitconfig_override
| --\
| --- childFolder1
| \ --- [...]
| --\
| --- childFolder2
| \ --- [...]

我想要在 .gitconfig_override 应用于 childFolder1 childFolder2



我对此的动机如下:我有一台工作笔记本电脑,我也在空余时间用于个人项目。我所有的工作代码嵌套在一个文件夹中。当我推动git repos工作时,我需要使用我的工作角色 - 工作登录而不是姓名,并使用工作电子邮件。当我推送自己的个人(github)回购协议时,我想用我的真实姓名和个人电子邮件来完成。



我想过的其他可能解决方案(和问题):


  • 为工作和播放创建单独的用户,并适当设置其用户级别设置,然后以(我很容易忘记切换)
  • 创建一个脚本,用于在workFolder中搜索git repos,并将它们的.gitconfig文件添加/更新为保持适当的细节(如果我创建回购协议并忘记在推送前运行脚本,我会推错为错误的人)

  • 破解git,这样每次创建回购,它会检查文件路径,并在适当的情况下更新.gitconfig文件(复杂,凌乱,几乎可以肯定是错误的做法 - 另外,我不会有第一个线索如何去做!)
  • >


我检查了这个问题,它似乎只包含单个回购的解决方案,而不是多个。希望有人会看到这个问题,谁错过了那一个!

解决方案

编辑:Git 2.13介绍条件包含,旨在解决这个问题。



我的原始答案保留在下面,为了历史的缘故(用户坚持使用旧版本的git)。



============= =======================



您所需的确切行为不受支持,基于阅读gitconfig manpage。



但是,从 git 1.7.12 ,Git从四个不同的来源,其中两个是用户特定的:

$ XDG_CONFIG_HOME / git / config 〜/ .gitconfig 〜/ .gitconfig 中的条目覆盖 $ XDG_CONFIG_HOME / git / config 中的条目。



这意味着您可以将您的个人gitconfig存储在 $ XDG_CONFIG_HOME / git / config 中,并将特定于机器的覆盖放在〜/的.gitconfig 。类似于

  [user] 
email = username@example.com

〜/ .gitconfig 应该包含您的电子邮件大小写。



请注意,如果未设置$ XDG_CONFIG_HOME,git会查找〜/ .config / git / config



这对我来说很好,因为我在工作机器上只有两个个人回购站(我的emacs配置和我的点文件)。如果您经常向工作机器添加个人回购,这对您而言可能不够好。

在这种情况下, git init git clone

$ PATH上的任何二进制文件名称与'git- *'相匹配的二进制文件都可以作为git命令调用,所以你可以只需要一对调用原始命令并使用所有传递参数的shell脚本,然后将正确的配置文件复制到 .git / config 中。


I know that it's possible to set per-repo configs which override the user-level config (i.e. /path/to/my/repo/.gitconfig overrides ~/.gitconfig). Is it possible to set git configs which override the user-level settings for all child folders of a given folder? I.e., I have

|--topLevelFolder1
|--\
|   ---.gitconfig_override
|--\
|   ---childFolder1
|       \---[...]
|--\
|   ---childFolder2
|       \---[...]

And I want the settings defined in .gitconfig_override to apply in childFolder1 and childFolder2.

My motivation for this is as follows: I have a work laptop which I also use in my spare time for personal projects. All my work code is nested within a single folder. When I push to work git repos, I need to do so with my work persona - work login instead of name, and work email. When I push to my own personal (github) repos, I want to do so with my real name and personal email.

Other possible solutions I've thought of (and problems):

  • Create separate users for "work" and "play", set their user-level settings appropriately, and log in as the appropriate user when I switch context (hassle, plus I could easily forget to switch)
  • Create a script that searches for git repos inside "workFolder", and adds/updates their .gitconfig files to hold the appropriate details (if I create a repo and forget to run the script before pushing, I will push as the wrong person)
  • "hack" git such that every time it creates a repo, it checks the filepath and, if appropriate, updates the .gitconfig file (complicated, messy, and almost certainly The Wrong Way To Do It - plus, I wouldn't have the first clue how to go about it!)

I checked this question, which only seems to contain solutions for single repos, not multiple. Hopefully someone will see this question who missed that one!

解决方案

EDIT: Git 2.13 introduced conditional includes, which are designed to solve this exact problem.

My original answer is preserved below, for history's sake (and users stuck on older versions of git).

====================================

The exact behavior you desire is not supported, based on reading the gitconfig manpage.

However, as of git 1.7.12, Git reads config data from four different sources, two of which are user-specific:

$XDG_CONFIG_HOME/git/config and ~/.gitconfig. Entries in ~/.gitconfig override entries in $XDG_CONFIG_HOME/git/config.

That means you can store your personal gitconfig at $XDG_CONFIG_HOME/git/config and put machine-specific overrides in ~/.gitconfig. Something like

[user]
    email = username@example.com

in ~/.gitconfig should cover your email case.

Note that if $XDG_CONFIG_HOME isn't set git will look for ~/.config/git/config.

This works well for me, since I only ever have two personal repos on work machines (my emacs config and my dotfiles). If you add personal repos to your work machines frequently, this may not be good enough for you.

In that case, custom wrappers around git init and git clone would be your best bet.

Any binary on your $PATH whose name matches 'git-*' can be called as a git command, so you'd just need a pair of shell scripts that call the original command with all passed args, then copy the correct config file into .git/config.

这篇关于为所有子文件夹设置git配置值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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