Supercharge your Laravel development workflow with aliases
Command line aliases are a convenient way of shortening an existing commands or combining a bunch of commands into one meaningful word or an abbreviation.
Today I'll share with you some of my most used aliases with help me save time every single day. Over time this can compound to hours or even days of time saved and even fewer mistakes made. Let's dig in.
How to add aliases
Open up your ~/.bashrc
(or, if you use ZSH, .zshrc
). The structure of your aliases is pretty straightforward and consists of a simple key/value pair:
alias gs="git status"
First, you have the keyword alias
which, then the aliased name (gs
) which you will be typing in the command line and the actual command that your terminal recognises and runs (such as git status
in the example above). With this alias saved, every time you type gs
in your command line, it will actually run git status
. This is the simplest example, but it can go far beyond that, such as multiple commands in sequence. You can already see how that's going to save you time.
Keep in mind, after you save your ~/.bashrc
file, your aliases will not work immediately in the same command line session. For the new changes to take effect, either relaunch the command line, or reload the configuration by typing source ~/.bashrc
in the command line.
Git aliases
These will most likely be the most used aliases throughout your daily workflow. How many times have you made a typo writing git add
, git commit
or even git status
? I know I have lots of trouble typing out git branch
and especially the git push
/git pull
commands. So let's start with a few small and easy improvements.
# Display the status of the current branch alias gs="git status" # Display a list of branches on your local machine alias gb="git branch" # Add all files to the staging area alias ga="git add ." # Commit staged files with a message alias gc="git commit -m" # Add all files to the staging area and commit them with a message alias gac="git add . && git commit -m" # Checkout to a new branch alias gnew="git checkout -b" ### functions can also be called from the command line, ### which is kind of like an alias, but with advanced capabilities # Pull the latest changes from a remote branch # (current by default, or you can speficy which specific branch you'd like to pull from) function gpull () { if [ -z "$1" ] then git pull origin `git rev-parse --abbrev-ref HEAD` else git pull origin $1 fi } # Push the local changes to the remote branch # (current branch by default, or you can specify a different one) function gpush () { if [ -z "$1" ] then git push origin `git rev-parse --abbrev-ref HEAD` else git push origin $1 fi } # Checkout to a different branch and pull the latest changes from it automatically function ch() { git checkout $1; gpull; }
Here's my usual workflow using these commands:
- I start working on a new feature, so let's checkout to a new branch:
Typegnew feature/uploading-profile-photos
We're now in a new feature branch. - I do some work and I'm ready to commit my code.
Typegac "Users can now upload their own profile images"
The files have now been added and committed in a single line. - Let's push the code to the remote!
Typegpush
That's it, the code is now in a remote branch :)
Now let's say someone else has picked up your feature branch and made some changes. Usually you would have to type out the whole name of your branch when you want to pull the latest changes, like git pull origin feature/uploading-profile-photos
. But how often have you found yourself making a typo and then having to re-type the whole thing again? All this wasted time and sanity can be saved with a simple alias gpull
! It will pull down the current branch for you without having to type the full name. The same thing applies to the gpush
alias, which uses the current branch name to push the code to the remote.
A week goes by and your team has made some important changes to the master branch and you want to keep your feature branch up to date. Normally (on your feature branch) you would type git pull origin master
. The same thing can now be achieved with the alias gpull master
.
Once you're done with the feature branch and you're ready to start on a few feature, just do ch master
("checkout master") which will checkout to the master branch and pull the latest changes from it, and then do gnew <branch name>
to start off a new branch :)
Laravel and Node aliases
Now I personally work a lot with Laravel and VueJS and I have found a few aliases to be incredibly helpful in saving time and avoiding lots of headache in the long term.
Often times teams will work on new feature sets which will include new or updated dependencies. You can either ask your team if there are any, look at the changes on the relevant feature branch, or try running the code and see if anything breaks. Best case scenario - you'll hit an error immediately and hopefully realise to update your dependencies. Worst case - the code will actually run on incorrect and outdated dependencies which will give you incorrect results in the end. That's why, more often than not, you will want to always run composer install
and npm install
whenever you checkout to a different branch so you're always up to date with the correct dependencies used in that branch. It can be tedious to type one command at a time and wait for each to finish running before typing the next one.
Enter aliases.
# Install Composer and Node packages. Replace "npm" with "yarn" if you're using Yarn alias install="composer install && npm install" # Just an even shorter alias to "install" above alias i="install" # I call it "Build Resources" - br. Install packages, run the migrations and build the frontend resources alias br="install && php artisan migrate && npm run dev" # "Build Resources & Watch" - brw. Same as above, but also watch for file changes alias brw="install && php artisan migrate && npm run watch" # PHP Artisan commands alias art="php artisan" # Even shorter alias for Artisan commands, if you perfer alias a="php artisan" # Launch a Tinker session alias tinker="php artisan tinker" # Run the Laravel Queue worker. Add your additional queues there if you have any, separated by commas alias work="php artisan queue:work --queue=default --tries=3 --timeout=90"
Here's my workflow with them:
- Whenever I checkout to a new branch, I'll just type
br
to install all of the dependencies, run the migrations (if any) and also build the frontend assets, so I can immediately preview the project knowing I have it in the right state with the correct dependencies in place. - If I'm going to work on the branch, I'll run
brw
instead, which does exactly the same thing except it watches for file changes and automatically re-builds the frontend assets, which is usually what I want when I'm working on a feature. - If I have any queued Laravel jobs in my dev environment ready to be processed, I can start the queue worker with a simple
work
alias. Work, Laravel, work!
That's it! Combine that with a few git aliases described at the top, you'll be saving a lot of time in unnecessary typing or occasional typing mistakes.
Don't forget to signup below to receive more tips and tricks straight to your inbox!