Last Updated: August 15, 2019
·
3.711K
· blackfalcon

One command to rule them all

If you are like me, then you are tired of going to the command line, pulling updates into your repo from master and then having to run command after command to get things in order.

For those not in the know, bash alias is the key to all things happy.

So, lets go through the common steps that you may do when you are ready to write some new code and need to update your local repo.

Check your branch

When I update my local repo, I always make sure I am on the master branch. But we are busy and distracted, how many times have you pulled master into your feature branch when you didn't want to? Yeah, me too.

git checkout master

Those annoying OS files

Yup, computers have a way of making those annoying local files that you don't want in your project. While we can .gitignore these files, I just don't want them there.

find . -name '*.DS_Store' -type f -delete

Pull latest from master

Now we are ready to pull and merge the latest code from the remote repo master branch.

git pull

Clean up my local branches

Now that I have merged in all the code from the remote master branch, I need to update my local database of remote branches. The -p means 'prune'. This simple flag will mare sure that your list of branches is the same as the remote repo. It's helps to keep those things in sync.

git fetch -p

Update gems

Now there is a good chance that when you pull updates, there have been some changes to the Gemfile. There is NOTHING more annoying then trying to start a server and then getting Could not find ... error. So, run bundler.

bundle install

The only thing more annoying then missing gems is missing database migrations. So you run ...

rake db:migrate

Too many commands? There is an alias for that!

Ok, that is a lot of manual steps we just walked through, I am pretty sure that there is a better way to do this.

If you are not familiar with how bash and/or aliases work, it's really pretty simple.

In your home directory $cd ~/, there is a hidden file called .bash_profile. Open that up using your text editor of choice and use the following syntax alias [name]="[command]". Open a new Terminal (this article assumes you are running bash) you now have access to this new alias command.

Because the command for deleting .DS_Store files is a mouth full, I am going to make a quick alias for that before I make my main alias. Yes, you can reference an alias inside another alias. Cool huh?

alias deleteDS_Store="find . -name '*.DS_Store' -type f -delete", that's better.

Now that we have that, let's create our one command to rule them all and call it refrepo, short for "Refresh Repo".

alias refrepo="git checkout master && deleteDS_Store && git pull && git fetch -p && bundle install && rake db:migrate"

BOOM!!

And there it is. From your Terminal prompt, run refrepo and you are WINNING!!

Customize per your setup

There are a lot of things you can do with this tip. Add/remove commands. If you make it a solid habit of killing your local server with each update, something I typically suggest, then you could add the && rails s command at the end as well.

1 Response
Add your response

This is great! I often forget the power of aliases, thanks for the reminder. :)

over 1 year ago ·