You are on page 1of 12

ID: 23125068

Name: Nguyễn Nam Quân


Class: 23APCS1

CS162 Week 3 Reports


Subject: Github and Python projects

1. Creating a GitHub Account and Private Repository:


First, initialize a GitHub account using Gmail. After initialization, you will have an interface like this.
Create a repository by clicking on "Create Repository" in the top left corner of the screen to get an
interface to initialize the repository.

After filling in all necessary information, remember to switch to private mode to protect source code
and data from unauthorized access. This ensures safety and efficient management for the project.

Upon completing these steps, there will be a repository on your GitHub account with an interface like
this.
1.5 Git Bash: Connecting for Windows Users:

Git, in its basic state, is designed to operate in a Unix-style command-line environment.


While Linux and macOS naturally have such an environment, Windows relies on a
non-Unix-style command line, the Windows command prompt. To bridge this gap, Git Bash
serves as an important solution for Windows users, providing a Unix-like command line
experience.

If using Windows: Git Bash is included as part of the Git For Windows package. Download
and install Git For Windows like installing other Windows applications. After downloading,
locate the accompanying .exe file and open it to run Git Bash.

Eventually, when opening the terminal, there will be a bash to easily use Git commands. (In
this case, it's macOS.)
2. Clone Repository to Your Computer:

Note*: There is an important distinction between cloning private projects and public projects on
GitHub or similar version control systems. Below are some key points to note:

Public project: Can be cloned publicly without requiring special authentication.


Có thể thực hiện như thế này:

1. Open Git and Navigate to Storage Folder using the command:


cd /path/to/destination/folder
2. Clone Repository:
git clone <URL_repository>

Private project: Cloning a private project requires authentication, usually through the use of
SSH keys or username/password.

Therefore, you need to create SSH keys on your account to use commands like this:

(Interface when creating an SSH key)

Lệnh: ssh-keygen -t rsa -b 4096


Explanation:
ssh-keygen: Generate a new SSH key.
-t rsa: Specify the type of key to create. In this case, it's RSA, short for
Rivest-Shamir-Adleman. RSA is a widely-used public key cryptosystem.
-b 4096: Specify the number of bits in the key. A longer key usually provides higher security
but may also be slower for some operations. In this case, a key with a length of 4096 bits is
being created, considered strong and secure.

We get an SSH key generated in a storage path using:


cat <storage path> to open that SSH key for use on GitHub.
Then, open settings in your account to add the SSH key to your account.

After obtaining the SSH link from within your private repo, you can clone this project using
the command:
git clone <ssh>
(Image after successful cloning)

3, 4: Create source code folder and .gitignore file:


● .gitignore File:
The .gitignore file in a Git project plays an important role in specifying which files and
directories should not be tracked by Git. When you add a file or directory to .gitignore, Git
will ignore them during the process of tracking changes or committing.

Here are some important roles of the .gitignore file:


_ Exclude unnecessary files and directories.
_ Protect sensitive information.
_ Exclude files generated during development or building.
_ Keep Git history clean.

(Image after initializing .gitignore and source folder.)

About the application of the .gitignore file, there will be sections after starting to use git add,
git commit, git push, or git pull.
5. Git init, git add, git commit, git push, git pull Commands:

Git init:
Git init is a command in Git used to initialize a new Git repository. When you run this
command in an empty directory, it creates a new repository and starts tracking changes in
your project. Below are the meanings of git init:
_ Create a New Git Repository
_ Start Tracking Changes
_ Create a Git Working Directory
_ Prepare the Project for Version Control

*Note: If you copy a repository from GitHub using git clone, you don't need to run git
init again. The git clone command automatically initializes a new Git repository for you.
When you run git clone, it will:

_ Create a new directory with the same name as the repository being copied.
_ Initialize a new Git repository in that directory.
_ Fetch the contents of the repository from remote (from GitHub in this case) and set up a
working copy locally.

Git add:
In Git, the git add command is used to add changes from the working directory to the staging
area to prepare for committing. This command plays an important role in the Git workflow.
Syntax:
Add a Specific File:
git add <file> (File in this case can be a single file or a directory)
Add all changes:
git add .
In this case, using git add . adds file1.txt to the src. Using git add . will add both file1.txt and
.gitignore to the staging area to prepare for commit. Use the git status command to observe
the working status and status of files.

Git commit:
The git commit command in Git is used to save the changes from the staging area to the
version history of the repository. Each commit corresponds to a record in the history,
preserving the state of the project at that time. Below is how to use git commit:

git commit -m "Commit description message"


The main branch initialized initially is now with a commit named first commit that I
initialized.

Git push:

The git push command in Git is used to push commits (committed changes) from your local repository
to the repository on the server (remote repository), usually on services like GitHub, GitLab, or
Bitbucket. Below is how to use git push:

Syntax:

git push [remote_name] [branch_name]

remote name: The name of the remote repository you want to push to. Usually origin if you
have cloned from a remote repository.

branch_name: The name of the branch you want to push to. In this case, it's main.
Result: At this point, your repository on GitHub has been changed, and the main
branch has a new commit added.

Optional: Using git push -u and setting up upstream relationships, you can use git push
without specifying the remote repository name and branch name because Git already knows
which branch on the remote repository corresponds to your local branch.
Syntax: git push -u origin main

Git pull:

The git pull command in Git is used to update your project with the latest changes from the remote
repository. It is equivalent to running git fetch to download data from the remote repository and then
git merge to merge the changes into your local branch.

Syntax:
git pull [remote_name] [branch_name]

Ghi chú:
If there are no conflicts between your local changes and changes from the remote repository,
Git will automatically merge the changes.

If there are conflicts, Git will pause and allow you to resolve the conflict before completing
the merge.
If you have cloned the initial repository with git clone, you may not need to specify remote
and branch names because default values (usually "origin" and "main" or "master") have been
set during cloning.

Always ensure you have committed or stashed local changes before performing the pull
command to avoid conflicts.

6. Learning how to use projects with multiple files in Python:


In Python, when a project has multiple files, they are often divided into modules to manage
source code in an organized way. Each Python file (with the .py extension) can be considered
as a module and can be imported into other files to use the definitions (variables, functions,
classes) within them. This helps increase code organization and reuse.

7. Learning how to create classes, properties, methods in Python:


In the Python programming language, a class is a data structure used to encapsulate variables
(often called attributes) and functions (often called methods) into a single unit. A class is a
model or blueprint for creating objects.

Each object created from a class will have the attributes and methods of that class. This
helps organize the source code clearly, makes it easy to maintain, and increases code
reuse.

An example of a simple class in Python:


Code explanation:

Initialize a class: here it's a dog.

Properties: are the attributes of the class (in this case, name and age).

And Methods are the functions that the class can perform. For example, bark here.

After initializing the my_dog object belonging to the dog class, we give this object the
attributes of the dog class with the name "Buddy" and age "3". Now my_dog can
perform the methods of the dog class, such as bark.

You might also like