Git and GitHub

1. 🌱 Introduction to Git

    1.1 What is Git?
    1. Git is a distributed version control system. Version control means tracking changes in files over time. Git helps developers: ✅ Track changes in code ✅ Save different versions of a project ✅ Go back to previous versions when needed ✅ Work on new features safely using branches ✅ Collaborate with other developers Git is called distributed because every developer can have a full copy of the project and its history on their own computer. Most Git operations are local. Internet is usually needed only when we push code to a remote repository or pull code from a remote repository.
    1.2 Why Use Git?
    1. Imagine you are working on a Python project. Without Git, you may create files like: project_final.py project_final_2.py project_final_latest.py project_final_latest_fixed.py This becomes confusing. With Git, we can save proper versions using commits. Each commit works like a save point. If something goes wrong, we can check previous versions and recover code.
    1.3 What is GitHub?
    1. Git is the version control tool. GitHub is an online platform where Git repositories can be hosted. Common platforms for hosting Git repositories: - GitHub - GitLab - Bitbucket A project can exist: - Locally on your computer - Remotely on GitHub or another hosting platform Example: Local repository: Stored on your computer. Remote repository: Stored on GitHub.

2. 🛠️ Installing Git

    For different OS
    1. Windows: Click here to Download and install. macOS: Use Homebrew: brew install git Linux: For Ubuntu: sudo apt-get install git git-extrasVerify Installation: After installing Git, check version: git --version

3. 🖼️ Git Operation in Picture.

Git Branch Operation
Git Branch Operation

4. 📚 Git Terminologies

    4.1 Repository
    1. A repository, or repo, is a project folder tracked by Git. It contains: - Project files - Folder structure - Git history
    4.2 Commit
    1. A commit is a snapshot of changes. Each commit has: - Unique ID - Author information - Date and time - Commit message Example: git commit -m "Add login page"
    4.3 Branch
    1. A branch is a separate line of development. The default branch is usually: main or sometimes master Branches allow us to work on new features without directly changing the main branch.
    4.4 Merge
    1. Merge means combining changes from one branch into another. Example: Merging dev branch into main branch.
    4.5 Pull Request
    1. A pull request, often called PR, is a request to merge changes from one branch into another. Pull requests allow: - Code review - Discussion - Suggestions - Approval before merging
    4.6 Remote
    1. A remote is a version of the repository hosted on a server. Example: GitHub repository
    4.7 Clone
    1. Clone means copying a remote repository to your local computer. Example: git clone https://github.com/username/project.git
    4.8 Push
    1. Push means uploading local commits to the remote repository. Example: git push origin main
    4.9 Pull
    1. Pull means downloading changes from remote repository and merging them into your local branch. Example: git pull origin main
    4.10 Staging Area
    1. The staging area is a place where we prepare files before committing. Command: git add file_name

5. 🔁 Basic Git Workflow

    5.1 Common Git Flow
    1. 1. Make changes in files. 2. Check changed files. 3. Stage files. 4. Commit changes. 5. Push changes to remote repository. Commands: git status git add . git commit -m "message" git push origin branch_nameGit WorkFlow in Picture
Git Workflow
Git Workflow
    5.2 Git File States
    1. Files can be in different states: Untracked: Git sees the file, but it is not tracking it yet. Modified: File is changed after being tracked. Staged: File is ready to be committed. Committed: File changes are saved in Git history.

6. ⚙️ Configuring Git

    6.1 Git for minimal configuration
    1. Check current configuration: git config --global --listSet User Name and Email: git config --global user.name "Your Name" git config --global user.email "[email protected]"Validate configuration: git config --global --list

7. 🔐 Configuring SSH for GitHub

    7.1 What is SSH?
    1. SSH is used for secure authentication with remote repositories. If SSH is set up, we do not need to enter username and password every time we push or pull. Generate SSH Key Open terminal and run: ssh-keygen -t rsa -b 4096 -C "[email protected]" Press Enter until the key is generated. Copy Public Key Public key file is usually: id_rsa.pub Open the file and copy its content.
    7.2 Add SSH Key to GitHub
    1. Steps: 1. Open GitHub. 2. Go to Settings. 3. Click SSH and GPG keys. 4. Click New SSH key. 5. Paste copied public key. 6. Click Add SSH key.
    7.3 Test SSH Connection
    1. Command: ssh -T [email protected] If successful, GitHub should confirm authentication.

8. 🚀 Initializing and Pushing Project to GitHub

    8.1 Create Remote Repository
    1. Steps: 1. Go to GitHub. 2. Click New repository. 3. Enter repository name. 4. Create repository.
    8.2 Initialize Git in Existing Project
    1. Open project folder in VS Code terminal. Command: git init Check status: git status
    8.3 Add Remote Repository
    1. Command: git remote add origin repository_link.gitExample: git remote add origin [email protected]:username/project-name.git
    8.4 Set Branch Name
    1. Command: git branch -M main
    8.5 Stage and Commit Files
    1. Stage files: git add . Commit: git commit -m "Initial commit"
    8.6 Push Code to GitHub
    1. Command: git push -u origin main After this, your local project is connected to GitHub.

9. 🙈 .gitignore File

    9.1 What is .gitignore?
    1. .gitignore tells Git which files or folders should not be tracked. Some files should not be pushed to GitHub. Examples: - Virtual environment folders - Log files - Temporary files - Secret files - Large generated files
    9.2 Create .gitignore
    1. Create a file named: .gitignore Place it in the root folder of your project.
    9.3 Example .gitignore
    1. Example: venv/ *.log test.csv .env __pycache__/ *.pyc
    9.4 Why Ignore Files?
    1. We usually ignore: venv/ Because it can be recreated using requirements.txt. .env Because it may contain secrets like passwords and API keys. *.log Because log files are generated automatically. __pycache__/ Because Python creates it automatically.

10. 🌿 Working with Branches

    10.1 Why Use Branches?
    1. Branches help us work on new features safely. Example: main branch: Stable working code. dev branch: New feature or experimental work. This keeps main branch clean and stable.
    10.2 Pull Latest Changes
    1. Before starting work, pull latest changes from main: git pull origin main
    10.3 Create New Branch
    1. Command: git checkout -b dev This creates and switches to dev branch.
    10.4 Check Current Branch
    1. Command: git branch The current branch is usually marked with *.
    10.5 Switch Branch
    1. Command: git checkout main or: git checkout dev
    10.6 Make Changes and Commit
    1. After editing files: git statusgit add .git commit -m "Add new feature"
    10.7 Push Branch to Remote
    1. Command: git push origin dev

11. 🔀 Pull Request Workflow

    11.1 What is a Pull Request?
    1. A pull request is a request to merge one branch into another. Example: Merge dev branch into main branch. Pull requests are useful because they allow: ✅ Code review ✅ Discussion ✅ Suggestions ✅ Summary of changes ✅ Safer merging
    11.2 Common PR Flow
    1. Steps: 1. Pull latest main branch. 2. Create a new feature branch. 3. Make changes. 4. Commit changes. 5. Push branch to GitHub. 6. Create pull request. 7. Review changes. 8. Merge pull request.
    11.3 Create Pull Request on GitHub
    1. Steps: 1. Open GitHub repository. 2. Go to Pull requests tab. 3. Click New pull request. 4. Select base branch as main. 5. Select compare branch as dev. 6. Add title and description. 7. Click Create pull request.
    11.4 What to Write in PR Description
    1. A good PR description should explain: Why: Why this change is needed. What: What was changed. How: How the change was implemented. Example: Why: Users need a blog page. What: Added blog.html page. How: Created new HTML file and linked it from navigation.
    11.5 PR Description with Copilot
    1. You can ask Copilot to help generate PR description. Example prompt: PR description for dev branch

12. 🧭 VS Code Git GUI Workflow

    12.1 Pull Latest Changes
    1. GUI: Click Sync icon in bottom left. Command: git pull origin main
    12.2 Create New Branch
    1. GUI: Click branch name in bottom left. Choose Create new branch. Command: git checkout -b dev
    12.3 Stage Files
    1. GUI: Click Source Control icon on left. Click + beside changed files. Command: git add file_name
    12.4 Commit Changes
    1. GUI: Type commit message. Click Commit. Command: git commit -m "message"
    12.5 Push Changes
    1. GUI: Click Sync changes. Command: git push origin dev

Practice QuestionsNot started

  1. 1. Working with Git

    Question 1 of 2

      1.1 Create a Git repository and push your exercise code to it.
      1. Create a new repository on GitHub.
      2. Open your project in VS Code.
      3. Initialize Git.
      4. Add remote repository.
      5. Stage files.
      6. Commit changes.
      7. Push code to main branch.
  2. 2. Personal Website Git Workflow

    Question 2 of 2

      2.1 Practice a full Git and GitHub workflow using your personal website:
      1. Create a GitHub repository for your personal website.
      2. Push your website code from the AI section to the repository.
      3. Take a domain name and host your website using the reference article.
      4. Create a branch called dev.
      5. Add a new page named blog.html.
      6. Commit the change.
      7. Push dev branch to GitHub.
      8. Create a pull request from dev branch to main branch.
      9. Merge the pull request.
      10. Confirm that the new page is visible on your website.