1

I would like to create a backup of the .zshrc file in my ~/ directory and avoid versioning all other items. How to exclude all subdirectories and their content?

For example:

cd ~/subdirectory/ && git status

should output:

fatal: Not a git repository (or any parent up to mount point /)

How to achieve this?

  • 3
    I link my dot files (ln -s ~/src/dot-files/zshrc ~/.zshrc). The VCS is used in ~/src/dot-files. – Christopher Feb 1 at 19:17
1

You can't get it to output

fatal: Not a git repository (or any parent up to mount point /)

but you can exclude all other files easily enough by adding a .gitignore file, with the contents:

* 
!.zshrc

which ignores everything (*) except .zshrc.

0

Here is a common solution for this:

  1. Create a subdirectory someplace convenient, preferably on the same volume.
  2. Move your .zshrc and any other .* files you want to track in git into that directory. You may want to rename them to make them visible to normal commands or to avoid name conflicts.
  3. Create symlinks from the files in the new git-dir into your home directory.
  4. Manage the git repository in its specific location.

For example:

# Make a git directory someplace to store your config
git init ~/git/dotfiles

# Move your config to the git directory
mv ~/.zshrc ~/git/dotfiles/zshrc

# Link the config's new location into your $HOME
ln -s ~/git/dotfiles/zshrc ~/.zshrc

# Manage your new repo in its specific location
cd ~/git/dotfiles
git add zshrc
git commit -m"Initial commit"
0

One way to do this, is to use a bare git repository in a folder that you do not call .git, but for example .git-dotfiles, and use git --git-dir=~/.git-dotfiles --work-tree=~ when interacting with the dotfiles you track in your home directory. To make this easier, you'd define an alias for that. Usage could then look like: git-dotfiles add .zshrc for example.

Because git normally (without a --git-dir flag) looks only for a .git directory (or file), it will not find your .git-dotfiles directory unless you point it to it.

It is advisable to set status.showUntrackedFiles to false in that repository, otherwise it will list all files in your home directory as untracked files. With that setting it shouldn't even look at untracked files, keeping things fast.

Atlassian published a nice article explaining this method in more detail.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.