Using bash to automate dotfiles

后端 未结 6 1958
执笔经年
执笔经年 2021-02-06 09:21

I want to create my own automated dotfiles folder. (I\'ll be using git to use version control on my dotfiles, but that\'s irrelevant for the question)

What i simply want

6条回答
  •  醉酒成梦
    2021-02-06 10:13

    I was also looking for some way to set up a new machine in a minimal number of steps, after spending some time I found that almost all the developers use git to store and share these files and symlinks to sync them.

    Well, symlinks works, but it isn’t the best way to sync your local files to the git repository. There is a much better solution to this, written by people at Atlassian – https://www.atlassian.com/git/tutorials/dotfiles.

    So, to git bare repository is the best and most elegant way to sync your files with your remote copy create a bash script to automate installation and set up.

    Managing dotfiles

    The trick to managing these dotfiles is by creating a bare git repository. If you are starting from scratch and have not tracked your dotfiles before, make a bare repository in the $HOME directory.

    git init --bare $HOME/.dotfiles
    

    Just to make it easier to use we’ll alias this to dotfiles which we will use instead of regular git to interact with our dotfiles repository.

    alias dotfiles="/usr/bin/git --git-dir=$HOME/.dotfiles --work-tree=$HOME"
    

    Now we are good to track our dotfiles using the dotfiles command, some of the examples are:

    # to check the version history 
    dotfiles log
    
    # to check the status of the tracked and untracked files 
    dotfiles status
    
    # to add a file for tracking
    dotfiles commit .vimrc -m ".vimrc added"
    
    # push new files or changes to the github
    dotfiles push origin main
    

    Use Cases and Advantages of dotfiles

    This method of managing and sharing has various advantages some of them are listed below.

    Easy setup

    Set up of a new machine can be a time consuming task, but with this method we can to use our personalized configs in under a minute. We just need to clone the repository and source the .bashrc or .zshrc file.

    git clone --bare https://github.com//dotfiles.git $HOME/.dotfiles && source ~/.zshrc
    

    Versioned dotfiles

    Best for experimenting with new configurations and keep the change history (Basically all the pros of using git)

    # to check the version history 
    dotfiles log
    

    Share on Multiple devices

    Share the same configs of multiple devices with minimal changes using branch, create a branch for your new machine, example:-

    # Create configurations specific to your aws machines
    dotfiles checkout -b aws
    

    Profiles for dotfiles

    Create configs based on your environment using branch, create a branch and configure according to you work env.

    # Manage multiple profiles - check out to the work profile 
    dotfiles checkout work
    

    I also use this way to sync and store my dotfiles, see my dotfiles repository and can read at Tooling is hard and necessary where I wrote about managing for multiple devices.

提交回复
热议问题