Introduction To Terminal Bash Script and Git Notes
Introduction To Terminal Bash Script and Git Notes
● What is Bash? It is better to it define terminal, GUI and shell before jumping into Bash
▪ Terminal: GUIs, allow a user to accomplish daily tasks by easily interacting with
windows and icons. However, there are commands that need to be manually
entered via texts to instruct the computer directly. We usually do this through
terminals. Terminals, also known as command lines or consoles the “black” screen
programmers use to send text commands to do things like navigate through a folder
or copy a file and do more complex tasks.
▪ Shell: When you type commands on your terminal, what you’ve typed must be
understood before the computer executes the task. Shell is the software that
interprets the commands typed on terminal so that your computer understands them
and return a response to your command. There are different shells
(programs/CLI/script) that we can use to manipulate our OS. Example: Bash (for
Mac and Linus OS), Command Prompt (CMD) or PowerShell. We will focus on
Bash in our course.
● Why are we learning Bash and Bash script for this course?
▪ As web developers you should learn one of the shell scripts, be it Bash, Command
Prompt or PowerShell for the following reasons:
● Great control over your operating system: There are commands that
you can’t use with your mouse and keyboard, but you can achieve these
commands only via shell.
● The piping feature on bash is what makes it super special: The Pipe (|)
is a command that lets you use two or more commands at once such that
the output from command serves as an input to the next.
● Better utilization of the Git Version Control tool: You will need your
shell command experiences to use the Git commands as well. There is a
desktop application created to make Git use easier. However, there are a
lot of Git functionalities that still require the use of Git commands from
your terminal.
▪ If you have a Mac computer, you can use the default built-in terminal on your Mac.
▪ For Windows computers, you can applications such as GitBash and Cygwin.
● Git Bash comes included as part of the Git for Windows package.
● If you have not downloaded and installed Git, please download and install
Git for Windows here, just like other Windows applications.
● Directory: when we discuss Bash commands, we will use the term “directory” more often.
Please know that directory is another name for folder.
▪ cd: It will change the directory you are currently in so that you can manipulate the
different files and directories in your system. It is basically going to a different
folder and double clicking it
▪ ls: Lists contents of the current directory, whether files or folders. It’s the same as
you opening a folder in file explorer to see its contents
▪ mkdir: Makes/creates a new directory. It is the same as you right clicking and
creating a folder
▪ touch: This is going to be the easiest way to create a new file in a current
directory. It is the same as right clicking on a folder and creating a new file
▪ echo:
● Using echo with fie name only: It creates a file. Example: echo >
style.css creates the style.css file
● Using echo with text and file name: creates the file and writes the text
in the file. Example: echo "Hello" > index.html, creates an index.html
file and types “Hello” on the file
▪ rm and rmdir: Both remove directories. However, rmdir will only delete empty
directories, whereas rm will remove directories that may contain content such as
files and subdirectories.
• rm -r -rf removes recursively and forcefully all files in that directory and
the directory itself.
• Example: rm -r CSS will removes the CSS folder (this folder will not be
removed by rm unless it has files or folders in it)
• Example: rmdir CSS will remove the CSS folder, if it has not files or
folders in it.
▪ The first set of rwx is for the owner: File owner has
permission to read, write and execute
▪ The third set of rwx is for all other users of the file:
All other users also has permission to read, write and
execute
□ The above values are added together for any one user
category:
▪ The second 6: The group owners of the file also have the
permission to read and write (4 + 2 + 0)
▪ exit: The exit command will close a terminal window/ends the session. You can
just type the “exit” command while you are on your current directory
▪ crtl + C: If you don’t want to close your terminal, but want to stop a running
command, just use the “Ctrl + C” using your keyboard
▪ Please refer to this link for the list of further commands bash commands
• https://www.educative.io/blog/bash-shell-command-cheat-sheet
● What is Git?
▪ Git is a Version Control System. There are multiple Version Control Systems, but
Git is by far and large the most popular.
▪ In simple terms, Git is a software that tracks the changes you make to files, to be
specific, it tracks the changes you make in your code. By tracking it is meant that
Git will record every change you make in a file and saves the different versions of
the file. If you want to look at a specific version of your file, you can go back and
see that version.
▪ The .git directory: This the directory/folder where Git stores the information about
your project. Without the .git directory, your project is a local project and not a Git
project. Meaning, you cannot perform any git commands and the project remains
untracked for any changes. The .git directory does the following:
● Branch information (on which branch is your current project state (HEAD)
pointing to)
● All logs of all local commits you have ever made (including revert
changes)
● In case of new projects, this directory is created when you open your new
project folder in terminal and type the command “git init”
● Ways to use Git: Git can be accessed via terminal commands or desktop app (GitHub
Desktop)
1.5 Why do we use Git and GitHub? How do we create a repository on GitHub?
▪ As stated above, Git is useful to anyone who writes code and tracks changes made
to a file. Git simplifies the process of working with other people. Having a
centrally located place (remote place such as GitHub) where you can upload your
changes and download changes from other members of your team, enable you to
collaborate more easily with other developers.
▪ If different people are working on the same file, Git can automatically merge the
changes from each contributor, without losing everyone's work.
● Diffing is used mostly when comparing two different versions of the same
file/code, meaning comparing an older with the newer version.
● Diffing is used in Git to specifically show what has changed between two
versions of the same file/code/commits/branches
● What is GitHub?
▪ Git is software that runs locally. Your files and their history are stored on your
computer. However, if you want to work on a project together with others from
anywhere, you will need to store a copy of your files in a clude/remote place. That
is when GitHub comes in handy.
▪ GitHub is a website/online host where you can remotely store your git
project/repositories so that they are accessible from anywhere in the world for
anyone working on the project. It offers the distributed version control and source
code management functionality of Git, plus its own features
● Git repository/repo: It is a virtual location on GitHub where a user can store code, and a
collection of files of various versions of a Project. Repository is nothing but a folder where
files and other folders are saved remotely in cloud services like GitHub.
o If you do not have a GitHub account already, click the link below
to sign up. Note: You will need an email address to sign up
▪ https://github.com/
o Once you sign up with GitHub, log into your GitHub account
o In the upper-right corner in your GitHub account, you will see the
“new” button. Click that button to start creating your repo
▪ Cloning: If it is not a brand-new project and there exists a Git repository already,
each developer needs to create a local copy of the existing repository, a process
called cloning. Here is the command you use to clone a remote repository:
● Go to any folder of your choice on your computer where you want to store
the remote repository and open your terminal
o You can find the address of the central repo by going to your
GitHub first and opening the repository. Click on “Code” green
button and either copy the HTTPS or SSH URL
▪ Committing: After cloning the project locally, each developer can make changes
to the files and then commit. Committing is basically saving the changes the
developer made to the local repository.
● Type: git commit –m “_______” (where you fill in the space with a
comment)
▪ Pulling: After committing changes locally, if you just push changes to the remote
repository, it is possible that another developer had already pushed updates that
contain code that conflicts with yours. Git will notify the conflict between your
version and the other developer’s version. Before publishing your updated version,
you need to download the current version of the central repository by the process
called, “git pull”. Pulling the current version of the remote repository will allow
you to add your updated version on top of what everyone else has already done.
▪ Branching: Branching in Git allows you to separate your work into different
branches so you can work freely on your source code without affecting anyone
else’s work or the actual code in the main branch.
● Main/master branch: This is basically the first branch created when you
clone and save a repository in your local computer.
● Other branches: Git allows you to create branches in addition to the main
branch. To create other branches, you can either create them from the
main branch or from the central remote repository.
● Why forking: Forking a repository allows you to freely make changes without affecting
the original project. Usually, people want to fork a publicly available repository because
they want to contribute to someone else’s project or want to use someone’s project as the
starting point for their own.
● What happens after forking? Forking without the need to make changes to the forked
repository is useless. The very reason of forking someone’s project is because you want to
contribute to the project my making changes. Once you forked someone’s repo, it will be
saved in your GitHub. In order to make changes to the repo, you will need to:
▪ Clone: You will need to clone it and make it available in your local computer so
that you make changes
▪ Push: Push the changes. Note: pushing the changes here means, the changes will
be pushed to the remote forked copy found in YOUR GitHub. It will not be pushed
to the original git repo. If you want to send the changes you made to the original
repo owner, you will need to send a pull request.
● Sending pull request: This is to tell the owner of the project that you wish to publish the
changes you made into their repo. Steps to send a pull request:
▪ Go to your GitHub and then to the forked project. You will see the change you just
committed
● You can download and use the desktop App from the link below:
o https://desktop.github.com/
o https://www.youtube.com/watch?v=ci3W1T88mzw
o https://code.visualstudio.com/docs/editor/github
● Here is a short video on how to use Git from your VSC terminal
o https://youtu.be/Fk12ELJ9Bww
▪ https://gitforwindows.org/
▪ To check if Git is installed on your computer, run this
command on your terminal
● git --version
o For Mac computers: The default terminal (or zsh) comes with
Git pre-installed on it, so no need to install Git Bash
● Step 4: Add the files you want to track to the staging area: Staging tells
Git that you want to include the updates you made in your next commit.
However, your changes are not actually recorded until you run “git
commit”
o If you want to remove a file you just added to the staging area:
▪ Command: git rm --cached [file-name]
o Note: You must explicitly tell Git which changes you want to
include in a commit (by adding these changes to the staging area)
before running the "git commit" command. This is because the
"git commit" command will add only the changes that are added
to the staging area
o Note: Using the "git commit" command only save the change you
made in your local Git repository
o Adding files/folders to the .gitignore file: to and add all the files
you want to be ignored in your .gitignore
o Note: Once you added a file/folder in your .gitignore file, all your
Git repositories will ignore the files and folders listed in the
.gitignore file.
● Step 8: Add the remote repo location to our local git folder: This is to
tell Git that we need a remote repo to push whatever we do with our local
folder.
o For that, you will need the address/URL of your GitHub
repository. To get this address, just
▪ Open the repository you just created on GitHub, and click
on “code” button on green and either use the HTTPS or
SSH address of the remote repo
o Now tell git that you want to add the remote repo location to push
your local changes to:
▪ Command: git remote add origin
https://github.com/yourgithubusername/repoName.git
o To check if remote repo is added successfully:
● Step 9: Push whatever files and changes you made locally to the remote
Git repo
o Now, go to your GitHub and open your remote repo. You will see
the commit you made
▪ Step 5: Pull the current version of the central repo: Command: git pull
▪ Step 6: Publish your changes to the remote repo: Command: git push
● https://dev.to/dhruv/essential-git-commands-every-developer-should-know-2fl
● To learn more about Git, watch “The Git crash course for beginners, click below:
● https://www.youtube.com/watch?v=SWYqp7iY_Tc