0% found this document useful (0 votes)
25 views23 pages

1.2 - Git Basics - Environment Variables (2201) - Class

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views23 pages

1.2 - Git Basics - Environment Variables (2201) - Class

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

CSEC/SWEN-123

Software Development & Problem Solving


Unit 1.2: Git Basics &
Environment Variables
Windows 10 ● The goal of this week’s lectures is to
Computer Literacy establish a common, minimum level of
computer literacy amongst the students in
the class.
● We will explore:
○ The Command Line ✅
○ The File System ✅
○ Version Control (with Git) ⮜
○ Environment Variables ⮜
○ More Advanced Git
○ Scripting
● Today we will learn about version control
software (VCS) - Specifically, you will begin
learning how to use Git. You will learn how
If you’d like to use a non-Windows computer for to:
your out of class work, you will need to do some ○ Initialize a repository.
extra learning on your own. ○ Add new/changed files to your repository.
○ Commit your changes.
Many of the commands you will use work much ○ Connect your repository to GitHub.
the same in Linux or OSX. Others you may ○ Push your changes to GitHub.
easily Google. ● We will also learn about creating and 2
modifying the system environment
● A version control system (VCS) is used to
manage a persistent, versioned backup of What is Version
your code.
○ If stored on a different computer (e.g. Control?
www.github.com), the backup provides a secure
duplicate of your code that can be recovered in There are many alternative implementations of
the event of a disaster such as a lost or stolen source control software including CVS, SVN,
laptop. Microsoft TFS, and many others.
○ The backup is also versioned meaning that you
can revert to an older version of the code This semester, we will be using Git, which is
if/when necessary. used throughout industry.
● Directories and files are tracked in a
repository.
○ Each developer has their own workspace, i.e.
The first step is to make sure that Git is installed
their local file system on a laptop of a desktop. and configured properly. We can do this using
○ Some developers have more than one, e.g. one the git --version command.
on a laptop and a second one on a desktop PC.
○ Teams working together share the same remote PS C:\Users\ron> git --version
repository., e.g. on GitHub.
git version 2.26.2.windows.1
● Developers can:
○ Make changes to the workspace including
adding, removing, modifying, or moving files If installed correctly, you should see output
3 (including directories). similar to the above.
○ Commit changes from the local workspace to
1.2.1

Git Version
Verify that Git is installed and configured properly.
● Launch the command prompt and use git --version to verify that
Git is installed and configured properly. You should see output
similar to the example below.

PS C:\Users\ron> git --version


git version 2.26.2.windows.1

4
Creating a New Local Repository
● Before you can do anything else, you The git init command will make a new,
need to create a local repository in empty repository in the directory from which it is
which to store your files (code, images, executed.
PDF, etc.) on your local file system.
● There are multiple ways to create an PS C:\Users\ron\Workspace> git init
empty Git repository: Initialized empty Git repository in
○ Create the repository on GitHub and C:/Users/ron/Workspace/.git/
download it to your computer.
○ Use a tool like GitHub desktop or Tortoise
Git. Once initialized, you can begin to start creating
○ Create the repository as part of a project files and directories to track in the repository.
in an integrated development
environment like Pycharm or VS Code.
○ Create the repository from the command
You will also be able to add, modify, move, or
prompt using the git init command.
delete files in the repository using Git
commands.
● This semester we will start by using the
command prompt. 5
1.2.2
Create a Local Repository
Use the git init command to create a git repository in your
local file system.
● Create the following directory structure inside your user directory:
SoftDevI\Unit01\Day02
○ e.g. C:\Users\ron\SoftDevI\Unit01\Day02
● Change into the Day02 directory.
When Git creates a repository, ● Run the git init command to create a new, empty repository.
it creates a hidden directory ○ You should see output similar to that shown below.
with lots of files inside it.
PS C:\Users\ron\SoftDevI\UNit01\Day02> git init
Initialized empty Git repository in
You can use ls -Force to
C:/Users/ron/SoftDevI/Unit01/Day02/.git
see the directory. You can cd
into it as well, e.g. cd .git
● Try running the ls –force command inside the directory. What do you
Be careful not to modify any of see?
the files inside - doing so can
corrupt your repository!
6
How Git Works
This is where you do This is a temporary
your work; create, read, staging area where
update, and delete files. changes are cached.
● As you work with Git, the files in your Local File System
project will move through four different
stages. Working add Staging
○ The working copy is the directory on your Copy Area
file system were you create, update, and
○ The
deletestaging area is where you temporarily
your files. commi
cache files that you would like to commit to t
your local repository.
○ The local repository is a copy of your
repository in the local file system, complete
Local push Remote
with version history for all of your files.
○ The remote repository is a backup of your Repository Repository
repository on a separate computer (server).
● You must execute separate Git commands
to move your files from one stage to the This is your local copy This is the remote
of the repository backup of your
next. repository (on another
including the version
history. computer).
Git Status
● The git status command can be used to
check the status of all of the files in your
PS C:\users\ron\SWEN-123\Unit01\Day02> git status
repository. On branch master
● The output is color coded depending on Changes to be committed:
(use "git restore --staged <file>..." to unstage)
the status of each file. modified: modified.txt
○ Untracked files are files that have never
been added to the repository and appear in Changes not staged for commit:
red. (use "git add <file>..." to update what will be
committed)
○ Modified files are existing files already
(use "git restore <file>..." to discard changes in
being tracked by the repository have not working directory)
been staged. They also appear in red. modified: changed.txt
○ Modified files are existing files that have
Untracked files:
been staged appear in green.
(use "git add <file>..." to include in what will be
● Depending on the current state of your committed)
project, you may see different files in new.txt

different states, e.g. untracked files,


modified files, and staged files. The output from git status also offers helpful advice
● If there are no changes in your workspace, regarding common actions that you may want to
you will see fairly benign output from Git, perform on each file, e.g. “use git add <file>...”
e.g.: for untracked files.
8
○ No commits yet
● A version control system (VCS) is an
Development Workflow excellent way to back up your files and
Start preserve previous versions (your version
history).
Add or Modify Verify
Files Changes (test) ● But a VCS is only as good as the process
that a software developer practices when
using it.
● A VCS is more effective when:
No Tests ○ A small, incremental change is made - when
Pass?
programming, this should only be a few lines
Programming Yes of code!
○ The change is verified via testing.

Cache New/
● ○ The changeitisisadded
Unfortunately, oftentothe
thecase that
repository.
Check Status ○ Repeat every few minutes!
Modified Files inexperienced developers get stuck in the
programming loop and only seldomly
commit. This causes lots of potential
Commit Cached Upload Cached
Files (locally) Files (remote) problems, including:
○ Increased odds that a disaster will result in
significant lost work.
○ Harder to move between computers.
No Yes ○ Harder to roll back specific changes.
Work End
Done? ○ Too few commits obfuscate the history of
Version Control changes. 9
A Closer Look at the Development Workflow
Start

Add or edit files using a text Add or Modify Verify Run tests to verify that your
editor like Notepad or VS Files Changes (test) new code works (and doesn’t
Code. break anything).

If the tests fail, go back and


modify the code to fix the No Tests
problems. Pass?
If the tests pass, confirm the
Programming Yes files that have been
added/changed:
git status
Cache added/changed files in Cache New/
the staging area: Modified Files
Check Status
git add <filename> or
git add .

Commit Cached Upload Cached Upload the changed files to


Files (locally) Files (remote)
Commit changes to local the remote repository:
repository: git push
git commit -m “message”

No Work Yes
End
Done?
Version Control 10
1.2.3
Adding Files
Create a new file and commit it to your repository.
● If necessary, launch a command prompt and navigate to your Day02
folder.
● Use the git status command.
○ You should see that there is nothing to commit.
● Use Notepad to create a new file named “me.txt” with your first
name, last name, email address, and today’s date each on a separate
line.
○ Don’t forget to save (CTRL-S)!
● Use git status again to see the status the new file.
○ You should see that the file is untracked.
● Use the git add command to stage the file, e.g. git add me.txt
● Use git status again to see that the status of the file has changed.
○ You should see that the file is staged (“Changes to be committed”).
● Use git commit to commit the file into your local repository.
○ Specify a comment using the -m option, e.g. git commit -m “initial
commit”
11
○ If prompted to do so, enter your GitHub email and/or username.
1.2.4

Modify a File
Make a change to your file and practice the Git workflow.

● Edit your “me.txt” file and add the day of the week on a separate line
at the end of the file.
● Practice the Git workflow to properly commit the file to your local
repository.
○ Status
○ Add
○ Commit (with comment)

12
Oops! I forgot to use -m!
If you forget to use -m, Git will open a text editor into
● In some version control systems, adding which you may type a comment. By default, this editor
a comment each time you commit to will be vim, which is installed with Git.
the repository is optional, however, Git
You will type your comment here!
requires that there is a comment with # Please enter the commit message for your changes. Lines
every single commit to the repository. starting
# with '#' will be ignored, and an empty message aborts the
○ This is a good thing! When used properly commit.
the commit comment can be used to #
quickly find a specific change. # On branch master
# Your branch is up to date with 'origin/master'.
● The fastest and easiest way to do this is #
# Changes to be committed:
by using the -m option when executing # new file: new_file.txt
the commit to specify the command. #


While very powerful, vim is not the most user friendly
e.g. git commit -m "clever comment"
text editor. You will need to type i (to insert), type your
● But what happens when you forget to comment on the first line, then press ESC and type :x
use the -m option? into the prompt at the bottom of the window to save.
13
Pushing to GitHub
● As stated previously, keeping a versioned
Begin by opening your repository in a browser, and
backup of your project on your local file
copying the URL from the address bar.
system has lots of advantages!
● However, ensuring that your code is backed
up to a remote server has all of the same
advantages, plus several more:
○ Secure backup in the event of a disaster (e.g. Next, use git remote add origin <URL> to
lost, stolen, broken computer). configure your local repository to use your remote
○ View your files and version history in a repository.
browser.
○ More easily share you code. PS C:\Users\ron\Repo> git remote add origin
○ Work from anywhere by downloading your https://github.com/RonWGit/my-repo-123
project to any computer.
● You must first use the git remote
Then use git push to upload your changes.
command to connect your local repository
to the remote.
PS C:\Users\ron\Repo> git push -u origin master
● You must then must use git push to upload 14
...lots of output
your code (and version history).
1.2.6

Push to GitHub
Configure your local repository to use your assignment
repository as the remote repository and push your files to
GitHub.
● Copy the URL to your assignment repository from your browser.
● If necessary, launch a command prompt and navigate to your
Day02 directory.
● Use the git remote add origin <URL> command to connect
your local repository to your assignment repository.
○ Replace <URL> with the URL to your assignment repository.
○ This will connect your local repository to the one at the specified
<URL>.
● Use the git push -u origin master command to push your
files to your assignment repository on GitHub.
○ You only need to use -u origin master the very first time that you
push.
○ This will push your code to the master branch on the origin (your
remote repository)
15
● Open your repository in your browser. Refresh if necessary. You
● So far you have only backed up versions of
your files on your local computer.
● There are still lots of benefits to doing this!
GitHub &


You still have a local backup.
You can revert to previous versions.
GitHub Classroom
○ You can see the history of changes to your
project.
● However, you do not have disaster
recovery; if your computer is broken, lost,
or stolen, you will lose all of your work.
● VCS (like Git) works best when you GitHub Classroom is integrated into GitHub. It makes it
routinely move your latest changes to a easy for instructors to make assignments that include
separate, private repositories for every student.
separate computer. There are lots of
advantages:
○ You should have already accepted your first GitHub
A safe, secure backup of your code.
Classroom assignment for your homework. As part of
○ More easily share your code with others. this, you will have linked your GitHub account to your
○ Download your code on multiple computers. name in the roster.
○ View your code and version history in a
browser.
We will now use another GitHub Classroom
● GitHub provides a free service that allows assignment to teach you how to connect your local
you to move your changes to their servers repository to a remote repository on GitHub.
16
in “the cloud.”
● You do not always start a new project by
Cloning from GitHub creating a new, empty repository on your
local computer.
git init != git clone ● It is often the case that you would like to
download an existing repository from GitHub
It is important to note the differences between git to the file system of the computer on which
init and git clone.
you are working.
● For example:
The git init command creates a brand new,
○ You may use a lab computer to start your
empty repository in the local file system on your
current computer. assignment in class, and you want to continue
working on your laptop or PC in your dorm
You can then use git remote to connect it to a room.
remote repository and push your code. ○ A GitHub Classroom assignment may include
starter code that you need to download
On the other hand, you can use git clone to before you can start working.
download an existing remote repository to your ● Downloading a repository from GitHub into a
local file system (even if the repository is empty). new workspace on your computer is called
cloning.
This is especially useful when the remote repository ○ You use the git clone <URL> command to do
already contains some code, and you won’t need to this.
use git remote to connect your local repository ○ Make sure to use replace <URL> with the
(when you clone, it is automatically connected). location of the repository that you want to
17
clone, e.g.
1.2.7

Clone from GitHub


Create a new directory and clone your GitHub repository into
it.● If necessary, launch a command prompt and navigate to your
Unit01 directory.
● Use the git clone <URL> command to clone your repository
into the directory.
○ Be sure to replace <URL> with the URL to your assignment
repository.
● Verify that your files have been created including:
○ A subdirectory named the same as your repository.
■ e.g. assignment-1-1-2-ronwgit
○ The “me.txt” file inside the directory; open the file in Notepad to
verify its contents.
○ The hidden .git folder (ls -Force).

18
1.2.8

Add Another File


Add a new file to your cloned repository and push it to the
remote repository.
● If necessary, launch a command prompt and navigate to the
repository that your cloned from GitHub in the previous activity.
○ e.g. SoftDevI\Unit01\assignment-1-1-2-ronwgit
● Use Notepad to create a new file called “primes.txt”. Add the
first 10 prime numbers to the file, each on its own line.
○ Hint: 1 is not prime.
● Use the Git workflow to push the file to your remote repository.
○ status, add, commit, push.
● Open your repository in a browser and verify that the primes.txt file
has has been uploaded.
● Now navigate to your Day02 directory and list the files. What do you
see?
○ e.g. cd ..\Day02 19
● There may be more than one local repository
connected to a single remote repository. Pulling from Git
○ This is the case when you are working on a
team; every team member has their own local
When working from multiple computers (either
repository.
○ It is also the case when you are working alone,
by yourself or on a team) one or more local
but use more than one computer, e.g. a lab
repositories will become out of sync.
computer in class and a PC in your dorm room.
● The remote repository always has the most
Remote
recent changes that were pushed from any of Repository
the local repositories that are connected to it.

gi
sh

t
○ Conversely, it will not have changes in any local

pu

pu
repository that have not been pushed.

ll
t
gi
○ This is one very good reason to follow the Git
workflow, where changes are pushed every few
minutes!
● This means that some of the local repositories
may be out of sync with the remote
repository. For this reason, when switching from one
○ Meaning that the local repository is outdated computer to another, the very first thing you will
and doesn’t have the most recent version of the do is a git pull to ensure that you have the
20 project. most recent changes.
● In order to download the most recent changes
1.2.9

Pull from GitHub


Your Day02 repository is now out of sync with the remote
repository on GitHub. Pull the most recent changes from
GitHub into your Day02 repository.
● If necessary, launch a command prompt and navigate to your Day02
directory.
○ e.g. SoftDevI\Unit01\Day02
○ List the files in the directory to verify that “primes.txt” is not present.
● Use the git pull command to pull the latest version of the files in the
repository into this local copy.
○ List the files in the directory to verify that “primes.txt” is now present.

21
Summary & Reflection
B a s i cs i t
Git The G ow
What is one thing
● that you wish you
○ rkfl
Wo ting a What is one knew more about
a
Cre sitory s
as we move
new thing that
○ o forward?
rep ing File iles you learned
F
Add ifying today?
○ b
M od G i tHu What one
○ b&
t H u question do
Gi sroom g
● s in you have about
Cla Push g today’s class?
○ ni n
C l o ng
○ i
Pull ent

v i ro nm
En ables
● i i ng
Var View g
○ in
Add
○ Please answer the questions above in your notes for
today. 22
32

You might also like