Artisanal Scripting: Home Git Home

David
3 min readMay 29, 2019

--

Hello there, it’s David again. Over the next few weeks I will be publishing a series of posts I’m calling Artisanal Scripting.

As a developer who spends much of his time on the terminal, I’ve crafted dotfiles and other scripts over the years. I call these scripts artisanal because while there may already be applications to do the things my scripts do, there’s a certain sense of ownership when you build your own. Not only are they tailored to fit your particular needs, but they also don’t have to be perfect, and that’s something we should embrace.

It’s like knitting a piece of clothing, building your own furniture, or brewing your own coffee or beer. They might not be the best, but they’re yours. Check the first one out below!

Photo by Nynne Schrøder on Unsplash

Home Git Home

Surely losing my scripts due to a crashing hard drive would be an unfortunate event. After a bad experience, I decided to put these files on Dropbox and symlinked them to my home directory. However, I didn’t like the mental burden of having to remember that any new dotfile needed to be symlinked and placed somewhere else. Additionally, I was precluding myself from the joy of tidying up and removing unused aliases, functions and other clutter out of fear that I might need them sometime in the future.

Git is a digital hoarder’s best friend. One of my favorite attributes of git is that pretty much anything can be initialized as a local repository. A simple git init on my home directory and I was good to go. Now, if you run git status you will see a lot of untracked files and directories, most of them you probably don’t want to commit into the repository. The status.showUntrackedFiles git config can help with this. I used that for a while, but then I decided to just use a .gitignore file and explicitly exclude the items I don’t want to keep in the repository.

Since I didn’t want to have to remember to commit modified files every now and then, I just added this to my .bash_logout script: git -C $HOME commit -am “$(date)” this way, every time I end my login session I automatically get a snapshot of my changes.

For ease of recovery you have a few options. Depending on your preferences, you might want to push your repository to a hosting service. To do this, you can add git -C $HOME push to .bash_logout

Another option could be to move your .git directory to Dropbox or a similar system and replace the original location with a .git file whose contents are “gitdir: /path/to/gitdir.” One caveat is that a single git directory is not recommended to be shared across multiple computers, so if you’re planning on sharing this approach it would be best to use git worktrees.

A git worktree works by keeping a subset of the git internals in a subdirectory within the .git directory. The typical use case is to have the ability to check out multiple branches in a separate directory while sharing references to remote branches and the git stash. When you add a new worktree via git worktree add you can see a .git file is created, similar to the one mentioned above, so that can be used to give each computer its own separate branch on a worktree.

That’s all for today. This is how I keep my files in the home directory checked in a safe place.

--

--

David

#Git #ContinuousDelivery #ContinuousIntegration #RelEng #Gradle #Vagrant #Docker #Vim #tmux