© We Can Code IT, LLC
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 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.
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.
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 prompt will look slightly different because it will display your own details.
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
~refers to the home directory. This is the directory where your user account files are stored. In Windows this is
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:
This will take you to your home directory. You should now see
~ as your current working directory.
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:
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.
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.
||List directory contents.|
||This is how to call the
||A combination of everything above, resulting in a long listing of all contents of the
You can easily move up and down to a parent or child directory in Bash using the
||moves up to the parent directory.|
||moves down into the subdirectory named
||moves up two levels, to the parent directory of the parent directory.|
||moves into the sub2 directory within the sub1 directory within the current working directory.|
||moves to the code directory within the Admin directory within the Users directory within the root directory.|
||moves to the PDFs directory within the Downloads directory within the home directory.|
Remember that a few symbols have a specific meaning in the filesystem:
||The current directory|
||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:
||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.
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
If you want to open the Windows File Explorer in a directory, just call the
||Opens File Explorer as though you had just clicked on the File Explorer icon. No particular directory will be shown.|
||Opens File Explorer to the current working directory.|
||Opens File Explorer to the
It is easy to switch between using File Explorer and Git Bash to work with the filesystem.
You can create a file with the
touch file.txt will create
file.txt in the CWD. If the file already exists, nothing happens.
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.
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:
This will display the text of the file:
UserName@MachineName MINGW64 / $ cat file.txt my string UserName@MachineName MINGW64 / $
mkdir command (make directory) can create a directory. Just tell it the name of the directory to create:
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
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.
rm command (remove) can delete a file or directory:
rmdir command (remove directory) can delete an empty directory:
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.
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
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.
subl file.txt will open
file.txt in Sublime Text.
code my-project will open the
my-project directory in Visual Studio Code, and
code . will open the current working directory in VS Code.
code can be used to open a file or a directory.
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.
We have used these Bash commands in this tutorial:
|touch||(create a file)|
We have also opened these other programs from within Bash:
|explorer||Windows File Explorer|
|code||Visual Studio Code|
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: