如何将 ~/.zshrc 文件添加到 git 中?

Luk*_*ski 5 backup zsh git

我想.zshrc在我的~/目录中创建文件的备份,并避免对所有其他项目进行版本控制。如何排除所有子目录及其内容?

例如:

cd ~/subdirectory/ && git status
Run Code Online (Sandbox Code Playgroud)

应该输出:

fatal: Not a git repository (or any parent up to mount point /)
Run Code Online (Sandbox Code Playgroud)

如何实现这一目标?

pho*_*ord 5

这是一个常见的解决方案:

  1. 在方便的地方创建一个子目录,最好在同一卷上。
  2. 将您的 .zshrc 和您想要在 git 中跟踪的任何其他 .* 文件移动到该目录中。您可能需要重命名它们以使它们对普通命令可见或避免名称冲突。
  3. 从新 git-dir 中的文件创建符号链接到您的主目录。
  4. 在其特定位置管理 git 存储库。

例如:

# 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"
Run Code Online (Sandbox Code Playgroud)


Chr*_*ris 1

你无法让它输出

fatal: Not a git repository (or any parent up to mount point /)
Run Code Online (Sandbox Code Playgroud)

但您可以通过添加文件轻松排除所有其他文件.gitignore,其内容为:

* 
!.zshrc
Run Code Online (Sandbox Code Playgroud)

它忽略*除 之外的所有内容 ( ) .zshrc