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

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

问题描述

我在主目录中有许多点文件,我想用git跟踪。 .pryrc .zshrc 等。我想有一个远程存储库这些文件,以便a)一个简单的方法来恢复我的配置设置,我应该失去我的机器为任何原因;



我最初在主目录中设置了一个git存储库,以便在配置更改时安全地跟踪对文件所做的任何更改。跟踪它们,将.gitignore文件配置为忽略除特定的白名单文件名以外的每个文件。然而,我意识到我对于在我的主目录中有一个git存储库不感到疯狂,并且在我的终端窗口中看到分支名称master也增加了分心。我使用ZSH与设置来显示提示中的git分支,它已经变得令人惊讶和混乱,看到我在一个git存储库,而导航在目录,我不希望有一个存储库。 / p>

我试图在home文件夹下创建一个配置目录,初始化一个git仓库,并生成到所需文件的符号链接。但是,我注意到添加和提交一切后,远程分支只显示链接,而不是文件的实际内容。



这是最好的方法是什么?

解决方案

是我怎么想的:



首先,我创建了一个目录〜/ repositories / configurations / 以容纳git存储库。导航到此目录后,我照常运行 git init

$



接下来,我创建了一个 .gitignore 文件配置为忽略除白名单上的所有文件。 (注意,你可以在提交之前的过程中这样做,但是如果你在设置 .gitignore git status $ c>您将看到大量未跟踪的文件)。

 #忽略所有内容
*

#除了这些白名单文件:
!.gitignore
!.pryrc
!.zshrc
!.bashrc
!.bash_profile
! .bash_login

然后将git存储库指向其他工作目录:



git config core.worktree/ User / username



$ c> git status 现在显示:

 未跟踪的文件:
添加<文件> ...以包括在将被提交的内容中)

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

从这里,我们 git add〜/ .bash_profile〜/ .bashrc〜/ .pryrc〜/ .zshrc 并提交。设置和推送到远程存储库是标准的。



一个注意 - 我决定我想要一个本地README.md文件来记录目录的用途,配置等,我不想在我的主文件夹中这将是不在上下文的这个文件。创建README后,我不得不使用 -f 标志将其添加到存储库:



git add -f README.md



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



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


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.

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.

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?

解决方案

This is how I figured it out:

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.

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

Then point the git repository to a different working directory:

git config core.worktree "/User/username"

Running git status now shows:

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

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

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

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.

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天全站免登陆