Skip to main content
 首页 » 编程设计

github之如何使用 Github 管理 dotfiles

2024年12月31日12zhujiabin

由于没有自动化,我将我的点文件存储在 github 中,非常痛苦。我必须自己更新它。

有没有办法可以自动安装/更新/同步点文件?我的意思是在新服务器中,我下载点文件并执行 install将点文件复制到本地的脚本。一段时间后,我可以执行 updateToRemote将本地更改推送到远程仓库的脚本,在另一台服务器上,我可以执行 updateToLocal将远程更改拉到本地的脚本。

类似的东西。

请您参考如下方法:

上面的答案非常好,正是我在“真实操作系统”上的做法。

我在使用 cygwin/msys 的著名商业操作系统上遇到了这个问题,因此使用符号链接(symbolic link)有时可能会对我最喜欢的一些软件的“ native ”端口产生问题。

为了解决这个问题,我尝试直接将文件夹 $HOME 设为 git 存储库。经过一些失败后,我发现关键都在 .gitignore 文件中。

因此,我已经使用了一段时间的设置是通过将 $HOME 设为存储库、创建 .gitignore 文件并单独添加所需的“dotfiles”文件来创建的。我还在备份驱动器(z:在这种情况下)上添加了一个存储库作为上游,只是为了获得自动备份。如果您的文件夹已经备份,添加上游是不必要的复杂性。因此,假设“/z/Backups/Dotfiles.git”是一个预先存在的“裸”存储库,在 msys shell 中,设置的步骤是:

$ cd $HOME 
$ git init 
Initialised empty Git repository in $HOME 
$ git add .bashrc .emacs .gitconfig # for example 
$ git commit -m "Initial import of dotfiles" 
[master (root-commit) xxxxxxxx] Initial import for dotfiles 
 3 files changed, xxx instertions(+) 
 create mode 100644 .bashrc 
 create mode 100644 .emacs 
 create mode 100644 .gitconfig 
 
# The following two lines just add an "upstream" purely for backup purposes. 
$ git remote add origin /z/Backups/Dotfiles.git 
$ git push -u origin master 
<< snip >> 

接下来,我将以下内容放入 $HOME/.gitignore:
# First exclude everything. 
* 
# then re-include all "dotfiles" 
!/.* 
# then a bunch of specific excludes as they are more-or-less "cache" data rather than configuration. 
/.bash_history 
/.dbus-keyrings/ 
/.emacs.d/ 
/.gimp-2.8/ 
/.git/ 
/.gitk 
/.idlerc/ 
/.lesshst 
/.saves/ 
/.thumbnails/ 

最后,以下命令(抱歉我没有从这些命令中捕获输出)将 .gitignore 本身添加到存储库中:
$ cd $HOME 
$ git add .gitignore 
$ git commit -m "Added some rules so git status on the dotfiles is useful." 
$ git push 

现在,您添加到主目录的任何“普通”文件都会被 dotfiles repo 忽略,但任何新的 dotfiles 都会显示在 git status 中,例如:
$ cd $HOME 
$ touch NewFile.txt 
$ git status 
On branch master 
Your branch is up-to-date with 'origin/master'. 
 
nothing to commit, working directory clean 
 
$ touch .funkychicken 
$ git status 
On branch master 
Your branch is up-to-date with 'origin/master'. 
 
Untracked files: 
  (use "git add <file>..." to include in what will be committed) 
 
        .funkychicken 
 
nothing added to commit but untracked files present (use "git add" to track) 

到目前为止,这一直运行良好,即使某些子目录是他们自己的(不相关的)git 存储库。有时非 git 子目录存在“怪癖”,但到目前为止没有任何问题。