Merge pull request #15726 from wxy/20190320-Move-your-dotfiles-to-version-control

TSL:20190320 Move your dotfiles to version control.md
This commit is contained in:
Xingyu.Wang 2019-10-03 09:28:46 +08:00 committed by GitHub
commit c72a3d58f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 125 additions and 130 deletions

View File

@ -1,130 +0,0 @@
[#]: collector: (lujun9972)
[#]: translator: (wxy)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (Move your dotfiles to version control)
[#]: via: (https://opensource.com/article/19/3/move-your-dotfiles-version-control)
[#]: author: (Matthew Broberg https://opensource.com/users/mbbroberg)
Move your dotfiles to version control
======
Back up or sync your custom configurations across your systems by sharing dotfiles on GitLab or GitHub.
![](https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/documents_papers_file_storage_work.png?itok=YlXpAqAJ)
There is something truly exciting about customizing your operating system through the collection of hidden files we call dotfiles. In [What a Shell Dotfile Can Do For You][1], H. "Waldo" Grunenwald goes into excellent detail about the why and how of setting up your dotfiles. Let's dig into the why and how of sharing them.
### What's a dotfile?
"Dotfiles" is a common term for all the configuration files we have floating around our machines. These files usually start with a **.** at the beginning of the filename, like **.gitconfig** , and operating systems often hide them by default. For example, when I use **ls -a** on MacOS, it shows all the lovely dotfiles that would otherwise not be in the output.
```
dotfiles on master
➜ ls
README.md  Rakefile   bin       misc    profiles   zsh-custom
dotfiles on master
➜ ls -a
.               .gitignore      .oh-my-zsh      README.md       zsh-custom
..              .gitmodules     .tmux           Rakefile
.gemrc          .global_ignore .vimrc           bin
.git            .gvimrc         .zlogin         misc
.gitconfig      .maid           .zshrc          profiles
```
If I take a look at one, **.gitconfig** , which I use for Git configuration, I see a ton of customization. I have account information, terminal color preferences, and tons of aliases that make my command-line interface feel like mine. Here's a snippet from the **[alias]** block:
```
87   # Show the diff between the latest commit and the current state
88   d = !"git diff-index --quiet HEAD -- || clear; git --no-pager diff --patch-with-stat"
89
90   # `git di $number` shows the diff between the state `$number` revisions ago and the current state
91   di = !"d() { git diff --patch-with-stat HEAD~$1; }; git diff-index --quiet HEAD -- || clear; d"
92
93   # Pull in remote changes for the current repository and all its submodules
94   p = !"git pull; git submodule foreach git pull origin master"
95
96   # Checkout a pull request from origin (of a github repository)
97   pr = !"pr() { git fetch origin pull/$1/head:pr-$1; git checkout pr-$1; }; pr"
```
Since my **.gitconfig** has over 200 lines of customization, I have no interest in rewriting it on every new computer or system I use, and either does anyone else. This is one reason sharing dotfiles has become more and more popular, especially with the rise of the social coding site GitHub. The canonical article advocating for sharing dotfiles is Zach Holman's [Dotfiles Are Meant to Be Forked][2] from 2008. The premise is true to this day: I want to share them, with myself, with those new to dotfiles, and with those who have taught me so much by sharing their customizations.
### Sharing dotfiles
Many of us have multiple systems or know hard drives are fickle enough that we want to back up our carefully curated customizations. How do we keep these wonderful files in sync across environments?
My favorite answer is distributed version control, preferably a service that will handle the heavy lifting for me. I regularly use GitHub and continue to enjoy GitLab as I get more experienced with it. Either one is a perfect place to share your information. To set yourself up:
1. Sign into your preferred Git-based service.
2. Create a repository called "dotfiles." (Make it public! Sharing is caring.)
3. Clone it to your local environment.*
4. Copy your dotfiles into the folder.
5. Symbolically link (symlink) them back to their target folder (most often **$HOME** ).
6. Push them to the remote repository.
* You may need to set up your Git configuration commands to clone the repository. Both GitHub and GitLab will prompt you with the commands to run.
![](https://opensource.com/sites/default/files/uploads/gitlab-new-project.png)
Step 4 above is the crux of this effort and can be a bit tricky. Whether you use a script or do it by hand, the workflow is to symlink from your dotfiles folder to the dotfiles destination so that any updates to your dotfiles are easily pushed to the remote repository. To do this for my **.gitconfig** file, I would enter:
```
$ cd dotfiles/
$ ln -nfs .gitconfig $HOME/.gitconfig
```
The flags added to the symlinking command offer a few additional benefits:
* **-s** creates a symbolic link instead of a hard link
* **-f** continues with other symlinking when an error occurs (not needed here, but useful in loops)
* **-n** avoids symlinking a symlink (same as **-h** for other versions of **ln** )
You can review the IEEE and Open Group [specification of **ln**][3] and the version on [MacOS 10.14.3][4] if you want to dig deeper into the available parameters. I had to look up these flags since I pulled them from someone else's dotfiles.
You can also make updating simpler with a little additional code, like the [Rakefile][5] I forked from [Brad Parbs][6]. Alternatively, you can keep it incredibly simple, as Jeff Geerling does [in his dotfiles][7]. He symlinks files using [this Ansible playbook][8]. Keeping everything in sync at this point is easy: you can cron job or occasionally **git push** from your dotfiles folder.
### Quick aside: What not to share
Before we move on, it is worth noting what you should not add to a shared dotfile repository—even if it starts with a dot. Anything that is a security risk, like files in your **.ssh/** folder, is not a good choice to share using this method. Be sure to double-check your configuration files before publishing them online and triple-check that no API tokens are in your files.
### Where should I start?
If Git is new to you, my [article about the terminology][9] and [a cheat sheet][10] of my most frequently used commands should help you get going.
There are other incredible resources to help you get started with dotfiles. Years ago, I came across [dotfiles.github.io][11] and continue to go back to it for a broader look at what people are doing. There is a lot of tribal knowledge hidden in other people's dotfiles. Take the time to scroll through some and don't be shy about adding them to your own.
I hope this will get you started on the joy of having consistent dotfiles across your computers.
What's your favorite dotfile trick? Add a comment or tweet me [@mbbroberg][12].
--------------------------------------------------------------------------------
via: https://opensource.com/article/19/3/move-your-dotfiles-version-control
作者:[Matthew Broberg][a]
选题:[lujun9972][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/mbbroberg
[b]: https://github.com/lujun9972
[1]: https://opensource.com/article/18/9/shell-dotfile
[2]: https://zachholman.com/2010/08/dotfiles-are-meant-to-be-forked/
[3]: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ln.html
[4]: https://www.unix.com/man-page/FreeBSD/1/ln/
[5]: https://github.com/mbbroberg/dotfiles/blob/master/Rakefile
[6]: https://github.com/bradp/dotfiles
[7]: https://github.com/geerlingguy/dotfiles
[8]: https://github.com/geerlingguy/mac-dev-playbook
[9]: https://opensource.com/article/19/2/git-terminology
[10]: https://opensource.com/downloads/cheat-sheet-git
[11]: http://dotfiles.github.io/
[12]: https://twitter.com/mbbroberg?lang=en

View File

@ -0,0 +1,125 @@
[#]: collector: (lujun9972)
[#]: translator: (wxy)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (Move your dotfiles to version control)
[#]: via: (https://opensource.com/article/19/3/move-your-dotfiles-version-control)
[#]: author: (Matthew Broberg https://opensource.com/users/mbbroberg)
把“点文件”放到版本控制中
======
> 通过在 GitLab 或 GitHub 上分享你的点文件,可以在整个系统上备份或同步你的自定义配置。
![](https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/documents_papers_file_storage_work.png?itok=YlXpAqAJ)
通过隐藏文件集(称为<ruby>点文件<rt>dotfile</rt></ruby>)来定制操作系统确实令人兴奋。在这篇 [Shell 点文件可以为你做什么][1]中H. "Waldo" Grunenwald 详细介绍了为什么以及如何设置点文件的细节。让我们深入探讨分享它们的原因和方式。
### 什么是点文件?
<ruby>点文件<rt>dotfile</rt></ruby>”是指我们计算机中四处漂泊的配置文件。这些文件通常在文件名的开头以 `.` 开头,例如 `.gitconfig`,并且操作系统通常默认情况下将其隐藏。例如,当我在 MacOS 上使用 `ls -a` 时,它才会显示所有可爱的点文件,否则这些就不会显示这些点文件。
```
dotfiles on master
➜ ls
README.md  Rakefile   bin       misc    profiles   zsh-custom
dotfiles on master
➜ ls -a
.               .gitignore      .oh-my-zsh      README.md       zsh-custom
..              .gitmodules     .tmux           Rakefile
.gemrc          .global_ignore .vimrc           bin
.git            .gvimrc         .zlogin         misc
.gitconfig      .maid           .zshrc          profiles
```
如果我看一下用于 Git 配置的 `.gitconfig`,我能看到大量的自定义配置。我设置了帐户信息、终端颜色首选项和大量别名,这些别名使我的命令行界面可以满足我自己的偏好。这是 `[alias]` 块的摘录:
```
87   # Show the diff between the latest commit and the current state
88   d = !"git diff-index --quiet HEAD -- || clear; git --no-pager diff --patch-with-stat"
89
90   # `git di $number` shows the diff between the state `$number` revisions ago and the current state
91   di = !"d() { git diff --patch-with-stat HEAD~$1; }; git diff-index --quiet HEAD -- || clear; d"
92
93   # Pull in remote changes for the current repository and all its submodules
94   p = !"git pull; git submodule foreach git pull origin master"
95
96   # Checkout a pull request from origin (of a github repository)
97   pr = !"pr() { git fetch origin pull/$1/head:pr-$1; git checkout pr-$1; }; pr"
```
由于我的 `.gitconfig` 有 200 多个自定义行,因此我不想在使用的每台新计算机或系统上重写它,其他人肯定也不想这样。这是分享点文件变得越来越流行的原因之一,尤其是随着社交编码网站 GitHub 的兴起。提倡分享点文件的规范文章是 Zach Holman 在 2008 年发表的 [点文件意味着要复刻][2]。其前提到今天依然如此:我想与我自己、与点文件新手,以及那些分享了他们的自定义配置从而教会了我很多知识的人分享它们。
### 分享点文件
我们中的许多人拥有多个系统,或者知道硬盘不可靠,因此我们希望备份精心策划的自定义设置。那么我们如何在环境之间同步这些精彩的文件?
我最喜欢的答案是分布式版本控制,作为一个用来为我处理繁重任务的服务是极棒的。我经常使用 GitHub随着我对 GitLab 的使用经验越来越丰富,我肯定会一如既往地继续喜欢它。任何一个这样的服务都是共享你的信息的理想场所。要自己设置的话可以这样做:
1. 登录到你首选的基于 Git 的服务。
2. 创建一个名为 `dotfiles` 的存储库。(将其设置为公开!分享即关爱。)
3. 将其克隆到你的本地环境。(你可能需要设置 Git 配置命令来克隆存储库。GitHub 和 GitLab 都会提示你需要运行的命令。)
4. 将你的点文件复制到该文件夹中。
5. 将它们符号链接回到其目标文件夹(最常见的是 `$HOME`)。
6. 将它们推送到远程存储库。
![](https://opensource.com/sites/default/files/uploads/gitlab-new-project.png)
上面的步骤 4 是这项工作的关键,可能有些棘手。无论是使用脚本还是手动执行,工作流程都是从 `dotfiles` 文件夹符号链接到点文件的目标位置,以便对点文件的任何更新都可以轻松地推送到远程存储库。要对我的 `.gitconfig` 文件执行此操作,请输入:
```
$ cd dotfiles/
$ ln -nfs .gitconfig $HOME/.gitconfig
```
添加到符号链接命令的标志还具有其他一些用处:
* `-s` 创建符号链接而不是硬链接。
* `-f` 在发生错误时继续做其他符号链接(此处不需要,但在循环中很有用)
* `-n` 避免符号链接到一个符号链接文件(等同于其他版本的 `ln``-h` 标志)
如果要更深入地研究可用参数,可以查看 IEEE 和开放小组的 [ln 规范][3]以及 [MacOS 10.14.3] [4] 上的版本。自从其他人的点文件中拉取出这些标志以来,我才发现了这些标志。
你还可以使用一些其他代码来简化更新,例如我从 [Brad Parbs][6] 派生的 [Rakefile][5]。另外,你也可以像 Jeff Geerling [在其点文件中][7]那样,使它保持极其简单的状态。他使用[此 Ansible 剧本][8]对文件进行符号链接。这样使所有内容保持同步很容易:你可以从点文件的文件夹中进行 cron 作业或偶尔进行 `git push`
### 快速说明:不要分享的内容
在继续之前,需要注意的是你不应该添加到共享的点文件存储库中的内容 —— 即使它以点开头。任何有安全风险的东西,例如 `.ssh/` 文件夹中的文件,都不是使用此方法分享的好选择。确保在在线发布配置文件之前仔细检查配置文件,并再三检查文件中没有 API 令牌。
### 我应该从哪里开始?
如果你不熟悉 Git那么我最常用的命令[有关术语的文章][9]和[备忘清单][10]将会帮助你继续前进。
还有其他不可思议的资源可帮助你开始使用点文件。多年前,我就遇到了 [dotfiles.github.io][11],并继续使用它来更广泛地了解人们在做什么。在其他人的点文件中隐藏了许多部落知识。花时间浏览一些内容,大胆地将它们添加到自己的内容中。
我希望这是让你在计算机上拥有一致的点文件的快乐开端。
您最喜欢的点文件技巧是什么? 添加评论或在 Twitter 上找我 [@mbbroberg][12]。
--------------------------------------------------------------------------------
via: https://opensource.com/article/19/3/move-your-dotfiles-version-control
作者:[Matthew Broberg][a]
选题:[lujun9972][b]
译者:[wxy](https://github.com/wxy)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/mbbroberg
[b]: https://github.com/lujun9972
[1]: https://opensource.com/article/18/9/shell-dotfile
[2]: https://zachholman.com/2010/08/dotfiles-are-meant-to-be-forked/
[3]: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ln.html
[4]: https://www.unix.com/man-page/FreeBSD/1/ln/
[5]: https://github.com/mbbroberg/dotfiles/blob/master/Rakefile
[6]: https://github.com/bradp/dotfiles
[7]: https://github.com/geerlingguy/dotfiles
[8]: https://github.com/geerlingguy/mac-dev-playbook
[9]: https://opensource.com/article/19/2/git-terminology
[10]: https://opensource.com/downloads/cheat-sheet-git
[11]: http://dotfiles.github.io/
[12]: https://twitter.com/mbbroberg?lang=en