Skip to content

Instantly share code, notes, and snippets.

@aflashyrhetoric
Created September 15, 2016 17:38
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save aflashyrhetoric/7570752c3c0e2bd922ec005ae5835216 to your computer and use it in GitHub Desktop.
Save aflashyrhetoric/7570752c3c0e2bd922ec005ae5835216 to your computer and use it in GitHub Desktop.

Using Github Gists to VC bash_aliases

Version Control your .bash_aliases file using:


I'll use a sample .bash_aliases file for the tut:

# ~/.bash_aliases
alias hi="echo 'hi'"
alias bye="echo 'bye'"

Next, install defunkt's gist library:

brew install gist

Test to see it's working:

gist -v // gist v4.5.0

Login to your github account through gists interactive prompt:

gist --login

Assuming your .bash_aliases file is in your home directory (~), upload .bash_aliases as a gist:

gist ~/.bash_aliases

A URL should be returned, like this:

https://gist.github.com/597ffdb59610887fb64ae838a00ee7e6

The numbers after https://gist.github.com/ is the ID for your new gist. Save this for later, you'll need it in a sec. (If you lose it, just visit the .bash_aliases gist by going to gist.github.com)

Anyway, let's go back to our sample .bash_aliases file, which currently looks like this:

# ~/.bash_aliases
alias hi="echo 'hi'"
alias bye="echo 'bye'"

We want to be able to easily do the following by typing simple aliases:

  • Edit our .bash_aliases
  • Push our updated .bash_aliases from our current machine
  • Pull & source the updated .bash_aliases on other machines

Append the following code snippets to .bash_aliases (be sure to swap out YOUR_GIST_ID with your actual gist ID from earlier):

############################
# .bash_aliases management #
############################

// Edit
alias eba="vim ~/.bash_aliases"

This bit adds a section comment (to keep .bash_aliases tidy). The alias eba is created for easily editing ~.bash_aliases in Vim. o0o meta.

Simple - moving on...


// Source changes
alias srcba="source ~/.bash_aliases; echo '.bash_aliases sourced.'"

This one sources the CURRENT .bash_aliases file and echoes a notification.

// Update gist remotely
alias pushba="gist -u YOUR_GIST_ID ~/.bash_aliases; echo '.bash_aliases gist updated.'"

// Pull updated gist to local (warning, will overwrite current ~/.bash_aliases)
alias pullba="gist -r YOUR_GIST_ID > ~/.bash_aliases; echo 'Retrieved updated .bash_aliases file.'; srcba;"

This is the juicy bit. It aliases pushba and pullba to the gist library to update, retrieve, and source your .bash_aliases.

Manually type source ~/.bash_aliases to enable your new special powers. You can now use the pushba and pullba commands on the current machine.

Obviously, though, the point is to use this with other machines. For the initial setup, you'll have to do the manual "copy-and-paste" method for each machine. If I have time, I may create a long one-liner that will do everything. But once everything is configured, your new workflow works like this:

  1. Edit your .bash_aliases with eba (or another editor) and save.
  2. Push changes with pushba
  3. To pull and source on another machine (after setup), simply type pullba
  4. An additional handy shortcut is now installed: srcba, which will source your aliases.

Done - enjoy!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment