1.2 - Git Basics - Environment Variables (2201) - Class
1.2 - Git Basics - Environment Variables (2201) - Class
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.
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
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).
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
18
1.2.8
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
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