Version Control – How to Version Control Your Bash Profile

bashgitversion control

So I'm very comfortable with version control, and just thought to start tracking versions of my bash profile: ~/.bash_profile with the added benefit of being able to share my various aliases and such on GitHub.

Assuming that my .bash_profile file needs to stay in my home directory(I can't wrap it in a directory to track it alone like a normal file), what would be the best way to go about this? I don't want to initialize a git repo in my home directory, and have to ignore every other file present.

So what might be a good solution?

I suppose I could just make a copy of it in a separate directory and update it / commit it from time to time, but I'm curious if there's a good way to version control a single file in a populated directory?

Best Answer

Afaik, typically there're two ways of doing this: a) symbolic link b) syncing script. In both case, you have to create another repo (name it dotfile below) for bash_profile to be version controlled.

Use symbolic links

bash_profile gets moved to your version controlled dir(containing .git), $HOME/.bash_profile is a soft link ⇢ $HOME/dotfile/bash_profile.

$HOME/dotfile/bash_profile is where your actual bash_profile resides, see holman dotfile as an example.

Use syncing script

bash_profile stays in your HOME dir but it's only a copy of the 'latest' one . The latest bash_profile still lives in $HOME/dotfile.

Yep, it's a copy and paste approach, this is why you need some syncing scriptto save you from DRY work. see mathiasbynens dotfiles as an good example. His syncing script https://github.com/mathiasbynens/dotfiles/blob/master/bootstrap.sh.

Furthermore

As you suspect:

I'm curious if there's a good way to version control a single file in a populated directory

Version controlling a single file doesn't make much sense, copy & paste or just drop it into some cloud drives saves you much hassle.

The real point is that things don't get scaled this way, when the dotfiles in your $HOME grows, when you want to version control your favorite text editor's config(say vimrc), when you use SSH to work with multiple shell remotely, you might have zshrc, bashrc, fishrc etc.

This means in the long run you might want to version control all your dotfiles. Github's dotfile is a good starting point.

Related Topic