How to use Github to manage dotfiles?

只愿长相守 提交于 2019-11-28 22:30:25

问题


I stored my dotfiles in github, with lots pains, because of no automation. I have to update it myself.

Is there a way that can auto install/update/sync dotfiles? I mean in a fresh server, I download dotfiles and exec a install script to copy dotfiles to local. After some time, I can exec a updateToRemote script to push local changes to remote repo, and on another server, I can exec a updateToLocal script to pull remote changes to local.

Something like that.


回答1:


The main source of information about dotfiles is dotfiles.github.io.

It references blog posts like Using Git and Github to Manage Your Dotfiles, based on a symlink method.

For starters, we’ll be putting all of our dotfiles into a folder called dotfiles, like so: /home/smalleycreative/dotfiles/vimrc.
Then, we’ll simply symlink to them from our home directory.


Jaime mentions the Atlassian tutorial "The best way to store your dotfiles: A bare Git repository"

The technique consists in storing a Git bare repository in a "side" folder (like $HOME/.cfg or $HOME/.myconfig) using a specially crafted alias so that commands are run against that repository and not the usual .git/ local folder, which would interfere with any other Git repositories around.
(and then the dotfiles folder is managed as a git repo)




回答2:


The above answers are excellent and are exactly how I would do it on "real operating systems".

I ran into an issue with this on a well-known commercial OS using cygwin/msys whereby using symlinks can sometimes be problematic with "native" ports of some of my favorite software.

To work around this I experimented with just making the folder $HOME be a git repository directly. After some failures I found the key is all in the .gitignore file.

So the setup I've been using for a while was created by making $HOME a repository, making a .gitignore file, and adding the required "dotfiles" files individually. I also added a repository on a backup up drive (z: in this case) as an upstream just to gain the automatic backups. If you're folder is already backed up, adding an upstream is an unnecessary complication. So, assuming "/z/Backups/Dotfiles.git" is a pre-existing "bare" repository, in msys shell, the steps to set things up are:

$ 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 >>

Next I put the following into $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/

Finally the following commands (apologies I didn't capture the output from these) add the .gitignore itself to the repository:

$ cd $HOME
$ git add .gitignore
$ git commit -m "Added some rules so git status on the dotfiles is useful."
$ git push

Now any "normal" files you add to your home dir get ignored by the dotfiles repo, but any new dotfiles show up in git status e.g.:

$ 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)

So far this has been working well, even with some subdirs being their own (unrelated) git repository. Sometimes there are "quirks" with non-git subdirs, but so far nothing problematic.




回答3:


Well, it's three years later and nobody has come up with anything great so i took a stab at this problem. It's platform agnostic and shell agnostic with no dependancies other then the bourne compatible shell you have available(bash, zsh, ksh, etc). It works on mac, linux, & windows:

dotsys

It will automatically synchronize changes to your dotfiles with github and multiple machines, along with many other features.




回答4:


I'm using git repository (github) and bash script to creating symlinks. But now I found this tool which seems to be more powerful. Check it out: https://github.com/shanx/python-dotfiles.




回答5:


I found an interesting way to use plain git to manage your dotfiles, no symlinks needed. This way, you should be able to do push and pull the usual way:

Setup

git init --bare $HOME/.myconf
alias config='/usr/bin/git --git-dir=$HOME/.myconf/ --work-tree=$HOME'
config config status.showUntrackedFiles no

where my ~/.myconf directory is a git bare repository.

Usage

The usual git commands can then be used with the git alias, e.g. config or whatever you choose.

config status
config add .vimrc
config commit -m "Add vimrc"
config push

Benefits

  • No extra tooling
  • no symlinks

More Infos

  • Source: https://news.ycombinator.com/item?id=11071754
  • Article with further infos: https://developer.atlassian.com/blog/2016/02/best-way-to-store-dotfiles-git-bare-repo/



回答6:


You update/sync your dotfiles with git push/pull commands (GitHub acting as 'central' repository). When it comes to symlinking dotfiles I wrote an article about it. Check it out: manage your dotfiles with ease.




回答7:


The main idea is having a separated directory (usually called .dotfiles) with all the real dotfiles which you want to keep track in git and having symbolic links in the home directory.

There is already a lot of work done for this approach so I will recommend you to check DFM (dotfiles manager):

  • Here is my dotfiles selection using dfm (you can consider it an example): vicente's dotfiles
  • Github official repository DFM site


来源:https://stackoverflow.com/questions/16292409/how-to-use-github-to-manage-dotfiles

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!