Appendix A — Git for Windows

Git for Windows is a set of tools for Windows users. Of main interest, Git for Windows provides a Bash emulation used to run Git from the command line. Using Git Bash, Windows users can use “git” commands exactly like one would do in Linux and Unix environments. Besides the full set of Git core commands, Git Bash is also packaged with additional shell commands that can be useful.

When you open Git Bash, you will see something like this:

~
$

The user_id@computer_id is an ID based on name of the user that is logged in, and the name of the device running Git Bash. The MINGW64 part is the name of the compiler used to build Git Bash. The ~ at the end of the first line indicates the current directory. A tilde means that the current directory is the home directory of the user.

A.1 Shortcut keys

An extensive list of available commands in Bash can be found here. Not all of them are available in Git Bash. Below is a short list of useful shortcut keys.

Description Shortcut
Moving the cursor
Go to beginning of line (Home) Ctrl+a
Go to end of line (End) Ctrl+e
Previous command (Up arrow) Ctrl+p
Next command (Down arrow) Ctrl+n
Go left/back one word Alt+b
Go right/forward one word Alt+f
Go forward one character Ctrl+f
Go backward one character Ctrl+b
Editing
Copy Ctrl+ins
Paste Shift+ins
Clear the screen Ctrl+l
Delete the character before the cursor (Backspace) Ctrl+h
Delete the character after the cursor Ctrl+d
Process control
Interrupt/kill current process (SIGINT) Ctrl+c

Windows users are used to using Ctrl+c and Ctrl+v to “Copy” and “Paste”, but these shortcuts are not available in Git Bash, since they are reserved for other commands. it is possible to enable “Copy” using Ctrl+Shift+c and “Paste” using Ctrl+Shift+v, by right-clicking in the terminal, navigating to Options>Keys, and checking the “Ctrl+Shift+letter shortcuts” box.

A.2 Git BASH commands

It is not easy to understand what commands are (or are not) available in Git Bash, why they are (or are not) available, nor is it straight forward to find documentation on them. Some commands have documentation that can be accessed by typing help [command-name] in the terminal. More generally speaking, CMD, PowerShell, and BASH command documentation can be found many different places, for example here.

Here we will introduce a few basic commands most of which are used extensively in this tutorial. We will also provide some focused documentation that covers how the commands are used in this tutorial, alongside links to more extensive documentation.

Command Description
cd Change working directory
clear Clear terminal window
echo > / echo >> Create file / append content to file
ls List directory content
mkdir Create folder
pwd Print working directory
rm Remove files
start Start a program/open a file/open a directory

A.3 Note on command options

Most of the commands listed here can take different options, e.g. ls has an -a and a -1 option. It is possible to use multiple options at once on the form ls -a1 instead of the more verbose form ls -a -1.

A.3.1 cd

Syntax: cd [DIR]

Description:
Change the current directory to DIR.

If DIR is not provided, changes the current directory to the value of the $HOME shell variable, i.e. the home directory of the user. cd - changes the working directory to the previous working directory ( “-” is converted to $OLDPWD).

Note that forward slashes (/) are used to specify file paths, and that paths can be both on the form C:/path/to/directory and /c/path/to/directory

If the file path contains spaces, enclose the file path in (double) quotes.

Git for Windows also comes with shell integration, so an alternative to using cd we can change the working directory by navigating to the folder in Windows explorer, right click on the folder (shift+right click on Windows 11) and select “Open Git Bash here”.

Link to documentation:
https://ss64.com/bash/cd.html

Example - Change directory

We can use cd to change the working directory by providing a file path to the directory.

~
$ cd C:/path/to/directory
/c/path/to/directory
$

Note that the working directory was set as C:/path/to/directory, but is displayed on the form /c/path/to/directory

Example - file path with spaces

If a file path contains spaces, enclose the file path in (double) quotes.

~
$ cd "/c/path/to/some directory"
/c/path/to/some directory
$

Example - Change to subdirectory

To change the current working directory to a subdirectory of the current working directory, we can simple set DIR to the subdirectory name.

/c/path/to/dir
$ cd subfolder
/c/path/to/dir/subfolder
$

Example - Change to parent directory

We can change the working directory to the parent directory by setting DIR to “..”.

/c/path/to/dir
$ cd ..
/c/path/to
$

You can also use “..” more generally when specifying a file path, to denote the parent directory

/c/path/to/dir_a
$ cd ../dir_b
/c/path/to/dir_b
$

Example - Change to previous directory

We can change the working directory to the previous working directory by setting DIR to “-”.

/c/current/cd/dir
$ cd -
/c/previous/cd/dir
$

Example - Changing working directory to user’s home directory

We can set the current working directory to the user’s home directory by not specifying DIR, or by setting DIR to “~”.

/c/current/dir
$ cd ~
~
$ cd -
/c/current/dir
$ cd
~
$

A.3.2 clear

Syntax: clear

Description:
Clear the terminal screen.

Link to documentation:
No documentation available.

Example

Imagine we had the history shown in the terminal below, ending with a clear command.

~
$ cd C:/path/to/directory
/c/path/to/directory
$ clear

This would result in the terminal shown below.

/c/path/to/directory
$

Clearing the terminal screen can also be done using the shortcut key Ctrl+l

A.3.3 echo > / echo >>

Syntax: echo [STRING] >> [FILE]

Description:
Append STRING content to FILE using the “>>” redirection operator. If the file does not exist it will be created. If “>>” is replaced with “>” then the given STRING overwrites the content of FILE.

Link to documentation:
https://ss64.com/bash/echo.html

Example - Create empty text file

You can create an empty file by not providing any STRING and using the “>” redirection operator (this will overwrite the any existing file with the same name).

~
$ echo > empty_file.txt
empty_file.txt

Example - Create text file with content

We can create a new text file with some content in it (or overwrite an existing file) by using the “>” redirection operator and by providing a STRING with the content.

~
$ echo "Some content" > new_file.txt
new_file.txt
Some content

Example - Append content to existing text file

To append content to an existing text file use the “>>” redirection operator.

~
$ echo "Some content" > append_file.txt
~
$ echo "Some more content" >> append_file.txt
append_file.txt
Some content
Some more content

A.3.4 ls

Syntax: ls [OPTIONS] [DIR]

List directory content.

List directories and files in DIR. If DIR is not provided, lists the content of the current working directory. Content is listed alphabetically.

OPTIONS:
-a
Do not ignore entries starting with dot (.), i.e. hidden files.
-1
List one file per line.

Link to documentation:
https://ss64.com/bash/ls.html

Example - List content of work directory

If we want to see a list of content in the current work directory, we can use ls.

/c/some/dir
$ ls
dir_a/ file_a.txt file_c.txt file_e.txt
dir_b/ file_b.txt file_d.txt file_f.txt

By default multiple entries are listed in each line and directories are colour-coded and has a “/” suffix.

Example - List all content of a directory

By default, entries starting with a dot (.), i.e. hidden files, are ignored. Use the “-a” option to list all files.
/c/some/git_repo
$ ls -a
.git/ file_a.txt file_c.txt file_e.txt
.gitignore file_b.txt file_d.txt file_f.txt

Example - List one entry per line

We can use the “-1” option to list only one entry per line.

/c/some/dir
$ ls -1
dir_a/
dir_b/
file_a.txt
file_b.txt
file_c.txt
file_d.txt
file_e.txt
file_f.txt

A.3.5 mkdir

Syntax: mk [FOLDER]

Create folder.

Create a new folder with name FOLDER in the current working directory, if it does not already exists. If the folder name contains spaces enclose the name in quotes.

Link to documentation:
https://ss64.com/bash/mkdir.html

Example

Create new folders. Note that we don’t have to enclose the folder name in quotes if it contains no spaces.

/c/some/dir
$ ls -1
file_a.txt
file_b.txt
/c/some/dir
$ mkdir new-folder
/c/some/dir
$ mkdir "new-folder"
/c/some/dir
$ ls -1
new-folder/
'new folder'/
file_a.txt
file_b.txt

A.3.6 pwd

Syntax: pwd

Print working directory.

Link to documentation:
https://ss64.com/bash/pwd.html

Example

When working with Git this command is mainly used to see where the current user’s home directory is, since the current working directory is always shown in the terminal.

~
$ pwd
/c/absolute/path/to/home/directory

A.3.7 rm

Syntax: `rm [OPTIONS] [FILE]``

Remove files.

To remove a file you must have write permission on the file and the folder where it is stored.

By default, rm does not remove directories. Use the “-r” option to remove each listed directory too, along with all of its content.

You should, obviously, be VERY careful when deleting files using terminal commands. Especially if you delete directories and their contents recursively.

OPTIONS:
-f
Ignore non-existent files, never prompt.
-r
Remove directories and their contents recursively.

Link to documentation:
https://ss64.com/bash/rm.html

Example - Remove file

/c/some/dir
$ ls
file_a.txt file_b.txt
/c/some/dir
$ rm file_a.txt
/c/some/dir
$ ls
file_b.txt

A.3.8 start

Syntax: start [FILE/DIR/PROGRAM]

Start a program, or open a file/directory in Windows Explorer.

TODO: Try to find some documentation on this command. This does not seem to be the CMD START command? Why does something likestart firefox actually work???

Link to documentation:

Example - Open current working directory in Windows explorer

We can open the current working directory in Windows explorer by simply using start .

/c/some/dir
$ start .