Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git - do not listen to commands in nested dirs

Tags:

git

dotfiles

I've got a git repo in my home directory to keep track of some dotfiles.

An unpleasant side effect of that, no matter where I am now, it is considered a part of the ~ repo, and it's easy to mistakenly add files to it etc.

It is somehow possible, that the repo in ~/.git listens to commands (git add, etc) ONLY if these commands are invoked directly in ~, and otherwise it just pretends to not exist, i.e. not a repository is thrown? (For example, if I invoke git add -A in ~/Documents)

like image 341
Sun Avatar asked Mar 01 '23 14:03

Sun


1 Answers

No this is not possible.

Another way, however, can be to use a non standard .git directory :

  • rename your .git/ directory to some other name (e.g : .git-wont-know/, or .git-dotfiles/)
  • use git --git-dir=~/.git-wont-know ... when you want to interact with it (link to docs)

You can set an alias or a wrapper to use this specific git dir ; you can also set GIT_DIR=~/.git-wont-know if you want to pass this down to existing scripts.


[update] this turns off the "spot where the repo is located by looking for the .git directory" feature.

If you want to invoke git with this setup from a subdirectory (say : from $HOME/.config/), you may want to also specify that the worktree is your home directory :

git --git-dir=~/.git-wont-know --work-tree=~ ...

# or using environment variables :
GIT_DIR=~/.git-wont-know
GIT_WORK_TREE=~
like image 173
LeGEC Avatar answered Mar 05 '23 15:03

LeGEC