0

When I used git init . in the directory whose contents were about to be stored using git, it dutifully created a local repository in a subdirectory named .git.

Under macOS, this is annoying, since by default any .xxxx file is "hidden".

  1. How can I use a different name than .git without it affecting use of subsequent git commands?

  2. If I move the (renamed) .git to another local directory, how do I modify subsequent git commands so they will find the repository?

Jacob Lee
  • 4,405
  • 2
  • 16
  • 37
murray
  • 737
  • 2
  • 10
  • 28
  • 1
    Why? What is your problem? – Marek R May 13 '21 at 14:40
  • 4
    I'd just change settings in your Mac to be able to see any .xx files if you really need to. Renaming will cause issues in some way shape or form. Why do you need to "see" your .git folder anyway? it's not something I've ever needed to look at in my experience – James May 13 '21 at 14:40
  • Here's a good thread concerning `.git` (https://stackoverflow.com/questions/29217859/what-is-the-git-folder) – dejanualex May 13 '21 at 14:41
  • 3
    `ls -a` will show the hidden 'dot-files'. But, you shouldn't be messing with stuff in the `.git` directory anyway except for exceptional cases. – PaulProgrammer May 13 '21 at 14:42
  • I know how to make hidden files visible in Finder, but it causes distractingly long listings in many directories, and in particular clutters up the Desktop with many such! – murray May 13 '21 at 14:42
  • 1
    @murray it's intentional for it to be hidden; this is because it's not an actual file you're tracking, but metadata that allows `git` to do it's job. I'm not sure why you would want it to be shown by default. – Andrew Ferrier May 13 '21 at 14:44
  • 1
    You should not interact directly with contents of this directory. So so not seeing it should not be a problem. – Marek R May 13 '21 at 14:44
  • 1
    @murray if you want, you can quickly toggle the visibility in Finder of hidden files/dirs with `Cmd+Shift+.` – Andrew Ferrier May 13 '21 at 14:45
  • @AndrewFerrier: Yes, I know how to change visitbility. OK, I'm convinced about leaving the `.git` folder hidden, and to keep it named `.git`. So my question 1 is disposed of. that leaves my question 2. – murray May 13 '21 at 14:53

1 Answers1

2

It's possible, but a nag, I guess. You can setup environment variable $GIT_DIR to specify where the git repo is.

https://git-scm.com/book/en/v2/Git-Internals-Environment-Variables

Just in case, it makes sense to have the directory named .git. This is so that it is hidden on purpose so that users don't mess with it unintentedly.

eftshift0
  • 26,375
  • 3
  • 36
  • 60
  • The linked docs don't show examples for `GIT_DIR`. Suppose I want the `.git` directory to be inside `~/backups`. Do I say `export GIT_DIR="~/backups/.git` or just `export GIT_DIR="~/backups` ? – murray May 13 '21 at 14:51
  • `export GIT_DIR="~/backups/.git` if you will place it there. – eftshift0 May 13 '21 at 14:53
  • It sounds a little more like you should mv `~/myprojectA/.git` to `~/backups/projectA` and then set `GIT_DIR=~/backups/projectA` – eftshift0 May 13 '21 at 14:54
  • Hmm... what you say indicates that the setting for `GIT_DIR` does *not* include `.git`, i.e., should *not* be `~/backups/projectA/.git`, whereas your previous comment seems to indicate it should be that, including the `.git` . So I'm confused! – murray May 13 '21 at 14:57
  • Is there normally a single `.git` directory on the local system holding backups of various projects (in which case a single setting of `GIT_DIR` is appropriate) or instead different `.git` directories for different projects, in wihich case a single setting of env var `GIT_DIR` would not handle things and I would have to use additional parameters on command line each time I used git commands for a particular project. (These are the sort of "basics" I don't find explained readily1) – murray May 13 '21 at 15:00
  • I think you are getting yourself down a big rabbit hole. What is normally done is that you have a project at a certain location and it has a git repo inside of it (the .git directory). It's technically possible to have it in a separate location, but don't think it's a lot of people who do it. I am sure it's used by people who have very special needs. – eftshift0 May 13 '21 at 15:06
  • the GIT_DIR will point to whatever directory you choose to put it into. If you put it in `~/backups/.git`, it's fine. That's the location you would put in GIT_DIR.... but it sounds like you will have only a single project in backups and the name suggests that it could hold multiple projects. – eftshift0 May 13 '21 at 15:08
  • Just in case, a little more development of your idea. You could have a single git repo holding multiple projects, actually. Say, create a bare repo, and then start adding remotes to it to different git repos for different projects. Then fetch all of them. You will end up with a multitude of projects all living in the same repo. It's messy but, hey, it's technically possible. – eftshift0 May 13 '21 at 15:12
  • I was trying to keep things a tiny bit safer from accidental deletion by having the `.git` repo elsewhere than in the dir where I'm working. (Yes, keeping `.git` hidden adds safety against stupidity!( But if I move `.git` elsewhere, out of the working dir, then can I put multiple projects there? – murray May 13 '21 at 15:12
  • How use same `.git` folder with multiple projects? (My last question for now that this has engendered, I promise! As I indicated, I'm a rank git newbie.) – murray May 13 '21 at 15:13
  • 4
    @murray: *How to use (one) `.git` folder for multiple projects:* Don't. You will make yourself miserable. Git is designed to store one `.git` folder per clone. Git stores its databases in there. – torek May 13 '21 at 15:16
  • Thanks to all! My intensive reading of `Pro Git` book about to begin! – murray May 13 '21 at 15:17
  • @torek that's great advice (not to use it). – eftshift0 May 13 '21 at 15:22