1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
---
title: "Dotfiles under revision control"
description: "How I manage my configuration files with Git."
published: 2019-09-02
---
<!-- TODO: Test commands -->
I regularly spend some time to fit my preferred tools to my personal need and taste.
Luckily, most command-line tools and a growing number of graphical tools, accept configuration files -- commonly called _dotfiles_ because of the typical dot at the beginnig of the file name, e.g., `.tmux.conf`.
This post describes my not-so-special way to put these dotfiles under revision control using Git and Bash.
In fact, you'll find a myriad of public dotfiles repositories on the web, for example on [GitHub](https://github.com/search?q=dotfiles).
Add the following snippet to your `~/.bashrc`.
The first line defines a `dotfiles` alias for `git` to distinguish your dotfiles repository from any other Git repository in and below your home directory.
The remaining lines reuse -- you might say hack -- Git's Bash completion for the alias.
```sh
alias dotfiles="git --git-dir=\${HOME}/.dotfiles/ --work-tree=\${HOME}"
if [ -f /usr/share/git/completion/git-completion.bash ]; then
source /usr/share/git/completion/git-completion.bash
__git_complete dotfiles __git_main
fi
```
Initialize a bare Git repository for your dotfiles, and tell Git to ignore untracked files.
```sh
mkdir ~/.dotfiles
git -C ~/.dotfiles init --bare
dotfiles config status.showUntrackedFiles no
```
Now you can `add`, `commit`, and `push` your dotfiles as usual.
You can even add other repositories as submodules.
The following snippet, for example, adds Vim and Tmux plug-ins for the acclaimed [Solarized](https://ethanschoonover.com/solarized/) color scheme.
```sh
mkdir -p ~/.tmux/plugins
cd ~/.tmux/plugins
dotfiles submodule add https://github.com/seebi/tmux-colors-solarized.git
mkdir -p ~/.vim/pack/stefan/{start,opt}
cd ~/.vim/pack/stefan/start
dotfiles submodule add https://github.com/altercation/vim-colors-solarized.git
dotfiles add ~/gitmodules
```
Update the submodules as always.
```sh
dotfiles submodule update --remote --merge
```
Generate help tags.
```sh
for d in ~/.vim/pack/stefan/*/*/doc; do
vim -u NONE -c "helptags $d" -c q
done
```
Finally, clone your dotfiles to another machine.
Be careful to clone into a temporary directory, though.
Otherwise you might screw up your home directory.
```sh
git clone \
--recurse-submodules \
--separate-git-dir=$HOME/.dotfiles \
example.com:~/git/dotfiles ~/dotfiles-tmp
rm ~/dotfiles-tmp/.git
cp -ai ~/dotfiles-tmp/.* ~
rm -r ~/dotfiles-tmp
dotfiles config status.showUntrackedFiles no
```
That's it. Happy tracking!
P.S. Did you know that Unix' hidden files were a mistake?
See [this archived post](https://web.archive.org/web/20190318012059/https://plus.google.com/101960720994009339267/posts/R58WgWwN9jp) by Rob Pike.
|