7

I have a .gitignore file like the following:

.*
!.gitignore

And I would like to version my .git/config file such that when I do a git pull my .git/config file updates automatically.

How can I do this?

I tried something like:

.*
!.gitignore
!.git/config

But this didn't work. I know if I create a link (ln -s .git/config configurations/git/config) I can version it, but I would like a better way to update the original one automatically.

I'm thinking of creating a hard link to it (ln .git/config configurations/git/config configuration/git), but it doesn't appear to be the best way. Is there a way to avoid this? Will Git work properly when versioning hard links?

Edit: explaining why to do this

The main motivation to do this is that I would like to version the home folder of a user of mine. I will use this versioning to help me on some kind of deploys. I'd like to keep all clones synchronized. Conflicts will appear just if I don't manage the things well, and I'll be taking care of this. Maybe I will be versioning another protected git files too.

GarouDan
  • 3,743
  • 9
  • 49
  • 75

4 Answers4

4

You can't 'version' your .git/config file. What you can do is create a .gitconfig in your working directory and commit that. Then after a clone you perform.

  cat .gitconfig >> .git/config

Of course, this is fraught with all sorts of potential problems. What if somebody edits .gitconfig? You'd need to undo .git/config and then re-append after git pull. What if .gitconfig changes on a different branch? You'd need to undo .git/config and re-append on git checkout <branch>

So, I'm not recommending changing .git/config; but if you must, you must.

GoZoner
  • 67,920
  • 20
  • 95
  • 145
  • GoZoner, I agree this solution will hold, but I would like avoid this because maybe I will versioning other protected git files. But If I really can't to do this I will need to create shell script to do something similar. – GarouDan Apr 26 '13 at 11:52
4

As stated by @Tuxdude it is bad information to share .git/config itself.

But Git (1.7.10+) now supports config inclusion!

So, for important options you may said place them in file: .git.config/repository.config and in .git/config just place link to it like:

[include]
        path = ../.git.config/repository.config

I just add such recommendation in readme file for repo. It much easy to just copy/paste many lines and versioning important settings like regular file. It also transfer updates for all users of repo.

Hubbitus
  • 5,161
  • 3
  • 41
  • 47
1

It is a bad idea to version your .git/config file.

git doesn't let you version anything under the .git directory because the contents of the .git are local to your repo, and 2 people cloning from the same remote URL, need not have the same set of files under the .git directory after the clone.

For example, you might just have a single remote named origin while the other developer might have named the remote something like foo instead. Another developer might have 2 remotes named bar and baz.

The info about remotes (URLs, refspec, etc.), local branches (like upstream tracking branch, etc.), are stored in your .git/config file.

If you really need to override some configuration, you can edit the .git/config file locally or if it is an option that you could set globally use your ~/.gitconfig or equivalent.

Tuxdude
  • 47,485
  • 15
  • 109
  • 110
  • Tuxdude, maybe is a bad idea, but as I explain (see the edit please) on my question, I would like to version this because I would like keep this file syncronized on all clones of mine. If I will have conflits or miss configuration depends only of my managing. – GarouDan Apr 26 '13 at 11:50
  • I don't understand all these "No" answers... There are a lot of good reasons to version your config file. For example if you set up gitflow for a repository, why would you want to let everyone set this up locally? You'd need to create a documentation, when you could just version it... – Inc33 Dec 23 '20 at 12:17
0

To add to the already excellent answers, and to actually answer the OP's question:

I'm thinking of creating a hard link to it (ln .git/config configurations/git/config configuration/git), but it doesn't appear to be the best way.
Is there a way to avoid this?
Will Git work properly when versioning hard links?

No:

  • hard links aren't represented in Git: see "Git and hard links"
  • Symlinks would simply be stored as text file with the path of the linked file.
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250