I’m lazy and so I seek ways to reduce repetitious activities. For instance, I’ve spent a lot of time in a terminal typing Git commands. A few of the more common commands, I’ve aliased. If I want to see a list of branches, I used to type:
1
|
|
But after adding an alias to my bash profile, I simply type gb
. I’ve done this for a few commands like git commit
, which is gc
and gca
for the -a
flag.
Occasionally, aliases aren’t enough and when it comes to Git, you can create custom commands that can be referenced like so:
1
|
|
To create a custom command, you first need to create a file named git-my-command
; second, you must place the resultant file on your path. Finally, you need to make the file executable. You can write this file in Bash, Ruby, or Python – it doesn’t matter.
For example, I tend to find myself stashing some uncommitted changes and then later popping those stashed changes onto a new branch. I end up executing the following steps:
1 2 |
|
The key step I want to simplify is the last one – I’m lazy and I’d rather not type 4 words. I’d rather type git unstash some_branch
because it saves me one word.
Following the three simple steps I mentioned above, I’ll first create a file in my ~/bin
directory called git-unstash
. The ~/bin
directory is in my path because my .bashrc
has this line: PATH=$PATH:$HOME/bin
.
My git-unstash
script will be simple – it takes an argument (the branch name, i.e. $1
); therefore, the script does a simple check to ensure the branch name is provided.
1 2 3 4 5 |
|
After I’m done writing it, I’ll do a quick chomd +x
and all three steps are accomplished.
Now my new flow is this:
1 2 |
|
Custom Git commands are that simple to invent – first, create a file named git-my-command
. Next, place it on your path; and, finally, make it executable. Be lazy and carry on, baby!