Git Bash

A Programmer's Magic Wand

Git Bash

Git Bash is an application for the Windows operating system. It allows Windows users to use the Bash command language and more easily work with the Git version control system.

Bash and Git are separate technologies, but Git Bash is a single program that makes it easy to use both of them on Windows.

Git Bash is already installed on your We Can Code IT machine. We will learn about Git later. First, let's focus on Bash.

Bash

Bash is a command language. Programmers type specific words or abbreviations to execute commands. They can use Bash one command at a time, or they can write a script file with many commands in it.

Computers running Linux and OS X already come with the ability to use Bash. The Git Bash program allows Windows users to use Bash as well.

Why Bash?

Using Bash, we can perform many actions on our computers using just text commands.

Often, this speeds up the process because we don't need to open a new program, click around, type something in, click around some more, and then close the program, all for a single task. We just type a few words and watch the magic happen.

Bash is not an optional technology. Developers use Bash. As you read about the different commands that Bash offers, try them on your own machine and start getting comfortable with them.

It's okay to go back and forth through these slides as you experiment with different commands. You don't need to memorize a slide to move forward.

Getting Started

Open Git Bash on your machine. You should see several things in the terminal:

UserName@MachineName MINGW64 /
$

This is the Bash prompt, which is what the terminal displays when it is waiting for you to type a command. As you can see, the Bash prompt shows several pieces of information:

  • Your computer account username
  • The name of your machine
  • MINGW64 (the environment of Git Bash - always the same)
  • The current working directory

Your prompt will look slightly different because it will display your own details.

The Filesystem

Your computer has files. Those files are organized into directories (on Windows, directories are often also called folders). These files and directories are called the filesystem.

The first step to using Bash is knowing how to navigate through your computer's filesystem.

You can use Bash to create, rename, move, and delete files and directories.

In Bash, there are two special directories:

  • / refers to the root directory. In Windows, this is the same as the C:/ drive.
  • ~ refers to the home directory. This is the directory where your user account files are stored. In Windows this is C:/Users/YourUsername.

The Current Working Directory

The Bash prompt shows the current working directory (CWD):

UserName@MachineName MINGW64 /
$

See how the / at the end means Bash is currently working in the root directory?

You can navigate to any directory with the cd command (change directory). Just pass it the name of a directory, and it will change the current working directory:

cd ~

This will take you to your home directory. You should now see ~ as your current working directory.

Listing Directory Contents

Your current working directory is, of course, a directory. Directories can have files and other directories inside them. You can use the ls command (list) to list all items inside the directory:

ls

This will simply list the items in the current working directory.

If you are used to Windows Command Prompt, you will notice this is very similar to the dir (directory) command in the Command Prompt. In fact, dir still works in Bash! The output isn't formatted as nicely as the ls command, though.

Command: ls

Bash commands have a certain format to them. First comes the command itself, then any switches/flags for how the command should behave, then any specific arguments - the items the command will operate upon.

Take a look at the ls command below.

Command Description
ls List directory contents.
ls -l The -l switch gives a long listing of directory contents, including date, permissions, etc.
ls -a The -a switch lists all directory contents, including those starting with a period(.) which Bash considers hidden.
ls -al This is how to call the ls command with both the -a and -l switches, resulting in a long listing of all directory contents.
ls otra Without the - in front of it, otra is an argument and not a switch. This lists the contents of the otra directory instead of the current working directory.
ls -al otra A combination of everything above, resulting in a long listing of all contents of the otra directory.

You can easily move up and down to a parent or child directory in Bash using the cd command:

Command Description
cd .. moves up to the parent directory.
cd dirname moves down into the subdirectory named dirname.
cd ../.. moves up two levels, to the parent directory of the parent directory.
cd sub1/sub2 moves into the sub2 directory within the sub1 directory within the current working directory.
cd /Users/Admin/code moves to the code directory within the Admin directory within the Users directory within the root directory.
cd ~/Downloads/PDFs moves to the PDFs directory within the Downloads directory within the home directory.

Recap: Filesystem Symbols

Remember that a few symbols have a specific meaning in the filesystem:

Symbol Meaning
. or ./ The current directory
.. or ../ The parent directory
/ When alone or at the beginning of a file path, this is the root directory.
Otherwise it is used as a directory separator: dir/subdir
~ or ~/ The home directory

Note that this means cd ./down and cd down are the same. Either command would navigate into the directory named down within the current working directory.

Bash Directory Trivia

Fun Fact: calling cd without any arguments is the same as calling cd ~. It will navigate to your home directory.

Fun Fact: the pwd command (Print Working Directory) will print out the full path to your current working directory. This is mostly useful when you are somewhere within the home directory (~) but you want to see the full path from the root directory (/).

If you are in ~/wcci/code and run pwd, you will see /c/Users/WeCanCodeIT/wcci/code.

Opening the File Explorer

If you want to open the Windows File Explorer in a directory, just call the explorer command:

Command Description
explorer Opens File Explorer as though you had just clicked on the File Explorer icon. No particular directory will be shown.
explorer . Opens File Explorer to the current working directory.
explorer code Opens File Explorer to the code subdirectory within the current working directory.

It is easy to switch between using File Explorer and Git Bash to work with the filesystem.

Creating Files

You can create a file with the touch command: touch file.txt will create file.txt in the CWD. If the file already exists, nothing happens.

You can echo any text you type in the terminal. echo "my string" will display "my string" in your terminal.

You can also echo strings (pieces of text) into files. Use the redirect operator (>) for this:

echo "my string" > file.txt

This will write my string into file.txt. Note that this replaces the contents of file.txt. Since there is no undo command, be careful when writing to files with Bash.

Viewing File Contents

To see the contents of a file, use the cat command. "Cat" comes from "concatenate," which means adding pieces of text together. You cat a file to add its text to the terminal output:

cat file.txt

This will display the text of the file:

UserName@MachineName MINGW64 /
$ cat file.txt
my string

UserName@MachineName MINGW64 /
$

Creating Directories

The mkdir command (make directory) can create a directory. Just tell it the name of the directory to create:

mkdir html-examples

You can use the -p flag (parents) to create nested directories. For example, Java projects have a src/main/java directory structure in them. java is within main within src, all within the project folder:

mkdir -p src/main/java

We have other tools to set up our Java projects, but if you need to create nested directories the -p flag will speed up the process.

Deleting Files and Directories

The rm command (remove) can delete a file or directory: rm filename.txt

The rmdir command (remove directory) can delete an empty directory: rmdir my-dir

To delete a directory with all of its contents, you must use the rm command with two flags, -r (recursive) and -f (force). The recursive option removes directory contents before removing the directory. The force option will delete items without prompting you for confirmation:

rm -rf large-directory

Note that this will delete the files without using the Recycle Bin, so be sure to only use this command when there is no chance you will need the files again.

Moving and Renaming Files and Directories

The mv command (move) is used to rename files and directories. It makes sense to "move" an item into another directory. It also makes sense to "move" the contents of a file to a different filename, which is why mv is also how we rename files.

mv needs two arguments: a source and a destination.

You can rename a file: mv oldname.txt newname.txt

You can move one or more files into a directory:

  • mv file.txt new_directory
  • mv file1.png file2.jpg images_directory

You can rename a directory: mv old_dirname new_dirname

If the destination directory already exists, mv will move the first directory into the second.

mv first second will result in second/first

Editing Files

You can use Bash to open other programs on your computer, such as a text editor. Often, these programs accept an argument to tell them which file or directory they should open.

For example, subl file.txt will open file.txt in Sublime Text.

Similarly, code my-project will open the my-project directory in Visual Studio Code, and code . will open the current working directory in VS Code.

Both subl and code can be used to open a file or a directory.

Note that subl only works if Sublime Text is installed and on your system path, and code only works if Visual Studio Code is installed and on your system path.

Recap

We have used these Bash commands in this tutorial:

Command Mnemonic
cd Change Directory
ls List
touch (create a file)
echo Echo Input
cat Concatenate
mkdir Make Directory
rmdir Remove Directory
rm Remove
mv Move

We have also opened these other programs from within Bash:

Command Program
explorer Windows File Explorer
subl Sublime Text
code Visual Studio Code

Summary

This has been a brief overview of Bash. There are many more commands available to make your life easier: We will use git (Git) to manage our source code.

If you want to learn more about other Bash features, try this tutorial: