How to use Github to manage dotfiles?

前端 未结 7 761
萌比男神i
萌比男神i 2020-12-24 13:08

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 m

7条回答
  •  不知归路
    2020-12-24 13:47

    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 ..." 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.

提交回复
热议问题