我如何跟踪“点"?我的主目录中的配置文件与 git? [英] How do I track "dot" configuration files in my home directory with git?

查看:19
本文介绍了我如何跟踪“点"?我的主目录中的配置文件与 git?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的主目录中有许多我想用 git 跟踪的点"文件 - 例如.pryrc.zshrc 等.我想要这些文件的远程存储库,以便 a) 如果我丢失了我的配置设置,有一种简单的方法可以恢复我的配置设置出于任何原因的机器;b) 安全地跟踪对文件所做的任何更改,以防我在配置更改时搞砸了.

I have a number of "dot" files in my home directory that I'd like to track with git - e.g. .pryrc, .zshrc, etc. I want to have a remote repository for these files so that a) there is an easy way to recover my configuration settings should I lose my machine for any reason; and b) to safely track any changes made to the files in the event I screw something up when configuring changes.

我最初在主目录中设置了一个 git 存储库来跟踪它们,并配置了一个 .gitignore 文件以忽略除特定的白名单文件名之外的每个文件.然而,我意识到在我的主目录中有一个 git 存储库并不是我的疯狂,而且在我的终端窗口中看到分支名称master"也增加了分心.我使用带有设置的 ZSH 在提示中显示 git 分支,结果发现我在一个 git 存储库中,同时在我不希望有存储库的目录中导航时,结果令人惊讶和困惑.

I initially set up a git repository in the home directory to track them, with a .gitignore file configured to ignore every file except specific, whitelisted file names. However, I realized I'm not crazy about having a git repository in my home directory, and there is also the added distraction of seeing the branch name "master" in my terminal window. I use ZSH with settings to display the git branch in the prompt, and it has turned out to be surprising and confusing to see that I'm in a git repository while navigating in directories that I don't expect to have a repository.

我尝试在主文件夹下创建一个配置目录,将其初始化为 git 存储库,并生成指向所需文件的符号链接.但是,在添加和提交所有内容后,我注意到远程分支只显示链接,而不是文件的实际内容.

I attempted to create a configuration directory below the home folder, initialize it a git repository, and generate symlinks to the desired files. However, I noted after adding and committing everything that the remote branch only showed the link, not the actual content of the file.

实现这一目标的最佳方法是什么?

What is the best way to achieve this?

推荐答案

我是这样想出来的:

首先,我创建了一个目录 ~/repositories/configurations/ 用于存放 git 存储库.导航到这个目录后,我像往常一样运行 git init.

First, I created a directory ~/repositories/configurations/ which I use to house the git repository. After navigating to this directory, I run git init as usual.

接下来,我创建了一个 .gitignore 文件,配置为忽略除白名单上的文件之外的所有文件.(请注意,您可以在提交之前的过程中稍后执行此操作,但是如果您在设置 .gitignore 之前运行 git status,您将看到很多未跟踪的文件).

Next, I created a .gitignore file configured to ignore all files except those on a whitelist. (Note that you can do this later in the process before the commit, but if you run git status before setting up the .gitignore you'll see a LOT of untracked files).

# ignore everything
*

# except these whitelisted files:
!.gitignore
!.pryrc
!.zshrc
!.bashrc
!.bash_profile
!.bash_login

然后将 git 存储库指向不同的工作目录:

Then point the git repository to a different working directory:

git config core.worktree "/用户/用户名"

运行 git status 现在显示:

Untracked files:
  (use "git add <file>..." to include in what will be committed)

  ../../.bash_login
  ../../.bash_profile
  ../../.bashrc
  ../../.gitignore
  ../../.pryrc
  ../../.zshrc

从这里开始,我们git add ~/.bash_profile ~/.bashrc ~/.pryrc ~/.zshrc 并提交.设置并推送到远程存储库是标准操作.

From here, we git add ~/.bash_profile ~/.bashrc ~/.pryrc ~/.zshrc and commit. Setting up and pushing to a remote repository is standard.

一个注意事项 - 我决定我想要一个本地 README.md 文件来记录目录的用途以及它是如何配置的等等.我不希望这个文件在我的主文件夹中,因为它会脱离上下文.创建自述文件后,我必须使用 -f 标志将其添加到存储库:

One note - I decided that I wanted a local README.md file to document the purpose of the directory and how it was configured, etc. I didn't want this file in my home folder where it would be out of context. After creating the README I had to use -f flag to add it to the repository:

git add -f README.md.

我尝试将其添加到 .gitignore 白名单,但无法说服 git 找到包含路径变化的文件,例如 !~/repositories/configurations/README.md 等等.无论我如何尝试指定路径,git 都没有看到该文件.用力添加后,它工作正常.比我更聪明的人可能会建议如何做到这一点.

I attempted to add it to the .gitignore whitelist, but couldn't convince git to find the file with variations of the path, such as !~/repositories/configurations/README.md and so forth. Regardless of how I tried to specify the path git didn't see the file. After adding with force though, it's working fine. Someone smarter than me may have a suggestion how to do this.

这个过程就像一个魅力.我现在有一个工作存储库来跟踪点文件更改并安全地远程存储它们.

This procedure worked like a charm. I now have a working repository to track dot file changes and safely store them remotely.

这篇关于我如何跟踪“点"?我的主目录中的配置文件与 git?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆