1.1 Git and the Command Line#
Before jumping into the world of Git, let’s cover the essential of the command line. This section will introduce you to the command line and the basic commands you need to use Git.
The Command Line#
The command line is a powerful tool used to interact with your computer’s operating system through commands. It allows you to perform tasks, manage files, and run programs without a graphical user interface.
The prompt#
When you open the command line, you are greeted by the prompt. It typically shows your current user and the host you’re logged into, followed by the current directory. For example:
root@debian:~#
This indicates that you are logged in as the root
user on a host named debian
, and you are currently in the root’s home directory (~
). The #
symbol indicates that you are logged in as the root user. If you are logged in as a regular user, the prompt will end with a $
symbol.
Your first commands#
First we need to check that Git is installed in our system, and as we will discuss later in the course, we would also like to know which version of Git we are using. To check if Git is intalled and which version we are using, we can use the following command:
git --version
git version 2.28.0
Commands: options and arguments#
Commands can have options (shortened with a single dash -
or full word with double dashes --
) and arguments (values that the command acts on) to modify their behavior or specify what they should act on. For example, in the previous command, --version
is an option that tells Git to display its version.
Clearing the terminal#
To clear the terminal and get a clean screen, use:
clear
Creating a new directory#
To create a new directory named test
, use the mkdir
command followed by the name of the directory you want to create. For example:
mkdir test
Creating a new file#
To create a new file named hello.txt
, use the touch
command followed by the name of the file you want to create. For example:
touch hello.txt
Adding content to a file#
To add content to a file, use the echo
command followed by the content you want to add and the name of the file you want to add it to. For example, to add the text “Hello, world!” to the hello.txt
file, use:
echo "Hello, world!" > hello.txt
Displaying the content of a file#
To display the content of a file, use the cat
command followed by the name of the file you want to display. For example, to display the content of the hello.txt
file, use:
cat hello.txt
Hello, world!
Deleting a file#
To remove a file, use the rm
command followed by the (relative or absolute) path to the file you want to remove. For example, to remove the hello.txt
file, use:
rm hello.txt
Deleting a directory#
To remove a directory and all its content, use the rm
command followed by the -r
option and the (relative or absolute) path to the directory you want to remove. For example, to remove the test
directory, use:
rm -r test
You can add the -f
option to force the removal of the directory and its content without asking for confirmation. For example, to remove the test
directory without confirmation, use:
rm -rf test
Danger
The rm
command is a powerful and potentially dangerous command. Always double-check the path you are providing to the rm
command to avoid removing the wrong files or directories.
Configuring Git#
Now that we have covered the basics of the command line, let’s configure Git. The first thing you should do when you install Git is to set your username and email address. This information will be used to identify your work.
To set your global username and email for Git:
git config --global user.name "msdp-book"
git config --global user.email "msdp.book@gmail.com"
To list your global Git configuration:
git config --global --list
user.name=msdp-book
user.email=msdp.book@gmail.com
http.sslverify=false
init.defaultbranch=main
Know more: Sytem vs Global vs Local Configuration
Git has three levels of configuration: system, global, and local. The system configuration applies to all users on the system and all repositories. The global configuration applies to the current user and all repositories. The local configuration applies to the current repository only. You can use the --system
, --global
, and --local
options to specify which level of configuration you want to work with.