CS1102 Session01
CS1102 Session01
2
Let’s start with
Save it till you make it
● It’s 1 am and you are working on a really important code project, the
delivery deadline is tomorrow, unfortunately, you accidentally
deleted a really important file and lost a lot of work
● Thankfully you were given another chance, but having learned your
lesson, you begin searching for a way to backup your work
Local Backup
● A basic backup method is to copy files into another directory with a different
name, and perhaps a time-stamp
● This approach is very common because it is so simple, but it is also incredibly
error-prone. It is easy to forget which directory you’re in and accidentally write
to the wrong file or copy over files you don’t mean to
What is a Version Control System?
● Version control systems are tools that help software teams manage, and track
changes to source code over time, such that you can recall specific versions of the
code later.
13
Creating a Git Repository
Git Files Status
● The main tool you use to determine which files are in which state is the git
status command (It lets you see which changes have been staged, which
haven't, and which files aren't being tracked by Git)
● Start by adding a file to your git project folder, for example create a basic hello
world cpp file
● Run git status again after adding a new file to your git project
● You’ll see an Untracked files section which means that Git sees a file you didn’t
have in the previous commit, and it hasn’t yet been staged, Git won’t start
including it in your next commit until you explicitly tell it using the git add
command
Git Files Status
File States In Git
● Untracked: Files in this state are not yet tracked by Git. Git is unaware of their existence
or changes. To start tracking them, you need to use the git add command
● Staged: Staged files are ready to be included in the next commit. You've told Git that you
want to save the changes in these files in the upcoming commit
● Modified: Modified files are ones that have been changed since the last commit. Git is
aware of these changes, but they haven't been staged for the next commit yet. You can
stage them with git add before committing
● Unmodified or Committed: Committed files have been saved to Git's version history. They
are part of the project's record and can be retrieved at any time
Tracking Files With git add
● The git add command in Git is used to stage changes in your working directory so that they
can be included in the next commit
● Add the cpp file you just created using git add main.cpp or git add . to add all files in
your project folder, then run git status again, you’ll see now that the file is listed in the
**Changes to be committed** section
Committing Your Changes
● Now that your staging area is set up, you can commit your changes using
git commit -m "Your message"
● Remember anything that is still unstaged such as any files you have created or modified and haven’t
run git add on since you edited them won’t go into this commit, They will stay as modified files on
your disk.
● You can see that the commit command has given you some output which includes the commit ID (
ef2d332 ) also known as a hash, how many files were changed, and statistics about lines added and
removed in the commit.
The Local Repository
The Index
The Working Tree
20
Committing Your Changes
Viewing The Commit History
● First try creating multiple commits to make the output of the history more clear
● After you have created several commits, you’ll probably want to look back to see
what has happened.
● You can use the git log command to view all the previous commits or
git log -p -2 to view the last 2 commits for example
Viewing The Commit History
Switching to a previous commit
● First, you need to identify the specific commit you want to checkout. You can
use the git log command to list all commits and find the commit hash of the
one you're interested in
● Once you have the commit hash, use the git checkout commitHash
command to switch to the old commit, this updates the working directory to
reflect the state your project was in at this commit
● When you're done viewing the old commit and want to return to the latest
commit you can use the git checkout main
Switching to a previous commit
Resetting to a Previous Commit
● If you want to return to a previous commit and make it the latest one, discarding
every commit after it you can use
git reset --hard CommitHash
● This command should be used with caution because it permanently discards
changes, and those changes cannot be easily recovered unless you have a
backup or another reference to them
Resetting to a Previous Commit
Some Helping Commands
◦ $ cd "C:\Users\Eman\Desktop" ❑ $ touch index.html
changes the current directory/folder
creates a new file
◦ $ ls
lists the files and folders of the current directory ❑ $ rm index.html
◦ $ ls -a deletes the specified file
lists all the files and folders of the current directory
including the hidden
❑ $ mkdir "learning-git"
creates a new directory/folder
❑ $ rm -rf "learning-git"
deletes the specified folder
28
Hands-on
1. Create a new directory and use git init to create a new Git repository.
2. Create a text file with some text then add it to the staging area using
git add
2. git add FileName : Adds files to the staging area to be saved in the next commit
4. git status : Displays the current project's status, including modified, staged, and
untracked files
● Github
● BitBucket
● Gitlab
Benefits Of Version Control
1. It allows you to revert files to a previous version, as it stores a complete long-
term change history of every file
2. Compare changes between different project versions
3. Enables multiple developers to work on the same project concurrently
4. See who last modified something that might be causing a problem
5. VCS serves as a backup for the code If it is lost or becomes corrupted
Creating a Github Account
Visit GitHub.com
Creating a Github Account
To create a shared
repository that multiple
developers can access
and collaborate on, Click
on create repository
Creating a new Repository on GitHub
Enter the following data:
● Repository name
● Whether its public or private
● The README.md file typically
includes a brief project
description, installation
instructions, and configuration
details that is shown on the
repository’s home page
Cloning A Repository from GitHub
Cloning A Repository from GitHub
● To get a copy of a repository available on a hosting service we use the git clone
command followed by the repository URL Like so :
git clone https://github.com/SystemAinShams/Game.git
● This creates a new folder in the current directory with the same name as the remote
repository which contains an exact copy of the entire Git repository, including all files,
commit history, branches, and configuration settings. This copy is saved on your local
machine
Cloning A repository from GitHub
Sharing Your Work
Once you have a local copy of a remote Git repository, you can carry out various Git operations,
such as creating commits and branches. When you're done working on your local changes and
want to upload them to the remote repository, you can use the git push command
Getting Updates from the Remote Repo
● You can also use the git pull command which is a shortcut for running
git fetch followed by git merge, this will look up what remote branch your current branch is tracking,
fetch from that branch, and then try to merge it in your current branch
■ First, you need to git clone the remote repo once (the first time you’re connecting to it)
■ Then, use git pull every time you want to download the last updated version.
References
◦ Dr. Aya Abdelhady’s slides from the previous semesters.
◦ Video Tutorials:
◦ https://www.youtube.com/watch?v=3fUbBnN_H2c&t=6843s (Recommended)
◦ https://www.youtube.com/watch?v=VUuGa1QFmcA
◦ https://www.youtube.com/watch?v=8JJ101D3knE&t=2297s
◦ https://www.youtube.com/playlist?list=PLeo1K3hjS3usJuxZZUBdjAcilgfQHkRzW
◦ Text Tutorials:
◦ https://git-scm.com/docs/gittutorial
◦ https://git-scm.com/docs/user-manual
45
Thank You : )