0% found this document useful (0 votes)
30 views46 pages

CS1102 Session01

Uploaded by

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

CS1102 Session01

Uploaded by

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

SESSION 01

GIT AND GITHUB

Fundamentals of Computing II Lab (CSCE 1102) MARIAM HASSANEIN


The American University in Cairo
Why to Use Git and GitHub?
◦ Problems Working Alone:
◦ The code is corrupted
and you want an old version back
◦ You deleted files by mistake
◦ Hard drive crash
◦ Usage: to keep track of your previous versions

◦ Problems Working in Teams:


◦ My teammate overwrote a file I worked on for 6 hours
◦ You need to send many emails with many project versions
◦ You waste a lot of time for integration problems with the code of your teammates
◦ Usage: to share and easily integrate files with your teammates

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.

● Git is a version control system.

● Other version control systems: CVS, Subversion (SVN),


Monotone, BitKeeper and Perforce
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
Installing Git
1. Download the latest Git for Windows, while for IOS check the installation guide.
2. Open the installer then follow the **Next** and **Finish** prompts to
complete the installation. The default options are sensible for most users.
3. Open a Command Prompt
4. Go to your project location using “ cd path”
5. Run the following commands to configure your Git username and email, This is
important because every Git commit uses this information.
git config --global user.name "Basem"
git config --global user.email "[email protected]"
Git Project Sections
Git has three main sections that your files can reside in:
● The working directory is one version of the project. These files are pulled
out of the compressed database in the Git directory and placed on disk for
you to use or modify
● The staging area is where git stores information about what files will go into
your next commit
● The .git directory is where Git stores the version history of all committed
files
Git Project Sections
Creating a Git repository
A Git repository is where Git keeps a record of all the changes made to your files
over time. It's where the history of your committed files is stored.
To create a new repo, you'll use the git init command.
1. First open your terminal in the folder where you want to create the Git
repository. this can be an empty folder or an already existing project that you
want to use Git with
2. Run the git init command, This creates a new hidden subdirectory named .git
that contains all of your necessary repository files.
Repositories
◦ Repository (repo): a location storing a copy of all files.
◦ You don’t edit the files directly in the repo.
The process is as follows:
1. You edit files in a local working copy (aka “the working tree”)
2. You add or stage the files into the staging area (aka “the index”) to be tracked
3. You commit the changed files into the repo

What to add to the repo? What not to add to the repo?


- Source files: .cpp, .h, .java, etc. - Files that are automatically generated: .obj,
- Build files: Makefile, build.xml, etc. .exe, .class, etc.
- Resource files: text files, images, etc.

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

3. Use git status to view the file’s state

4. Commit the changes using git commit -m "Your message"

5. Use git status to verify that there are no pending changes


Summary
1. git init : Sets up a new Git repository for version control

2. git add FileName : Adds files to the staging area to be saved in the next commit

3. git commit : Saves your staged files in the Git repository

4. git status : Displays the current project's status, including modified, staged, and
untracked files

5. git log : Lists the commit history


6. git restore FileName : Restores an unstaged file to its last committed version
Git Hub
Collaboration With Git
At this point, you can use Git locally to work on your code, make commits, create branches, and perform
most Git operations. However, to do any collaboration using Git, you’ll need to have a shared remote Git
repository available on some hosting service for other developers to access to.

The most popular Git hosting services are:

● 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

Enter the following data


● Email
● Password
● Username
Creating a new Repository on GitHub

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

● Note: in general, to download a specific remote repo to your local repo:

■ 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 : )

You might also like