Over the last several months, I’ve cloned nearly two-hundred Github repos and pushed hundreds of commits. Every time I’ve had to clone a repo and open the resulting directory in two steps, every time I’ve had to add, commit, and push, I’ve thought, “There’s got to be a better way!”
And of course there is. By writing custom Bash scripts, you can clone a repo into a directory and open that directory with one command and run through the add-commit-push cycle with another.
I’m working with a Linux system and I’ll be writing this tutorial from that perspective.
.bashrc configuration file
A Bash script is a series of command-line interface commands called using a single command. For this tutorial, you’ll be writing your scripts in a new file and then importing those commands into your .bashrc file, which is the file that configures your cli. There are lots of cool things you can do with these configurations, but I’ll leave those for another time.
First, locate your .bashrc. This is a hidden file, probably in your home directory. As a reminder, to see hidden files, use the ls -a command. Once you’ve found that file, open it in your preferred editor.
Next, create a new file in the same directory as your .bashrc file. Name this file .bash_aliases and open it in your editor, too.
Scroll through your .bashrc file until you start seeing lines that begin with “alias”. Aliases allow you to rename commands or create new commands using scripts. You might see some interesting aliases here that you weren’t aware of. Poke around a bit.
Once you’re doing poking around, find a nice spot to insert the following:
# Alias definitions
source ~/.bash_aliases
This will import into your configuration file the scripts you write in .bash_aliases. Save your configuration file and switch over to .bash_aliases.
Clone and open
The first script you’ll be writing is one that will allow you to clone a repo and open the resulting directory with a single command.
The structure of your script will look like this:
function function_name () {
commands
}
For example, if you wanted a script that said “Hello, world!” it would look like this:
function hello_world () {
echo Hello, world!
}
Simple enough! One thing to note about writing Bash scripts is that you will not be naming parameters and then referencing those parameters within your code-block. Instead, you’ll be using the following convention:
$1 == first argument
$2 == second argument
$3 == third argument
...
So, if you wanted a function to say whatever argument you pass in, it would look like this:
function print_out () {
echo $1
}
Ok, enough pre-amble. Let’s talk about the script we want to write.
This script will have three parts: first, it will call git clone on whatever ssh we pass into the command. Second, it will isolate the part of the ssh that determines the name of the repo’s directory and assign it to a variable. Third, it will call cd on that variable, thus opening the directory.
function gclop () {
git clone $1
local base="$(basename -s .git $1)"
cd $base
}
Here’s what happens if we run the command:
gclop git@github.com:tloughrist/phase-3-deli-counter.git
First, git clone will be called on git@github.com:tloughrist/phase-3-deli-counter.git. This will clone the indicated repository.
Second, a variable will be created, called “base” that will be available locally, i.e., within the function. To this variable we will assign “phase-3-deli-counter” using “$(basename -s .git $1)”.
The syntax of the Bash script requires us to wrap the command in “$()”. Inside of that we find the basename -s .git command, which strips the directory and file extension off of whatever argument we call it on. In this case, we’re calling it on the first argument, $1, i.e., git@github.com:tloughrist/phase-3-deli-counter.git. The result is the assignment of phase-3-deli-counter to the variable base.
Finally, change directory will be called on base, which will open the directory phase-3-deli-counter.
One script down, one to go.
Add, commit, and push
This next script will also have three parts: first, it will call git add .. Second, it will call git commit -m. Third, it will call git push.
The only tricky bit here will be inserting the commit message.
function gacp () {
git add .
git commit -m "$1"
git push
}
Here’s what happens when we run the command:
gacp first commit
First, git add . is called on the current directory.
Second, git commit is called with the message “first commit”. Recall, $1 refers to the function’s first argument.
Third, git push is called on the current directory.
And that’s it. What once was three now is one. Save your files, open a new terminal window, and try out your new commands.
Conclusion
We’ve only scratched the surface on what you can do with custom Bash scripts. Check out these resources to learn more about Bash scripts and configurations: