Day 3: Git & GitHub - Basics, Branching, Merging, PRs

Day 3: Git & GitHub - Basics, Branching, Merging, PRs

Initial Tasks:

✅ Install Git on your system: Git Installation
Configure your Git username and email:

git config --global user.name "Your Name"
git config --global user.email "your@email.com"

✅ Create a new folder and initialize a Git repository:

mkdir git-project && cd git-project
git init

✅ Create two files (index.html, style.css), add content, and commit:

touch index.html style.css
git add .
git commit -m "Initial commit"

Modify index.html, check changes, and make another commit:

git status
git diff
git add index.html
git commit -m "Updated index.html"

Check commit history using:

git log --oneline

✅ Create a GitHub Repository, add a remote, and push:

git remote add origin https://github.com/yourusername/git-project.git
git branch -M main
git push -u origin main

LEARN:

Challenge 1: Fork and Clone an Open-Source Project

Goal: Learn how to fork and clone a repository from GitHub.

Steps:

  1. Go to the GitHub repository: eks-auto-mode-workshop

  2. Click the Fork button to create a copy under your account.

  3. Clone the forked repository:

     git clone https://github.com/YOUR_USERNAME/eks-auto-mode-workshop.git
    
  4. Navigate into the repository:

     cd eks-auto-mode-workshop
    
  5. Verify remote repositories:

     git remote -v
    

Challenge 2: Create a New Branch (feature-branch), Switch, and Commit Changes

Goal: Learn how to create and switch branches, and commit changes.

Steps:

  1. Create a new branch and switch to it:

     git checkout -b feature-branch
    
  2. Create a new file and add content:

     echo "This is a new feature" > feature.txt
    
  3. Stage and commit the changes:

     git add feature.txt
     git commit -m "Added feature.txt"
    

Challenge 3: Merge feature-branch into main and Push the Changes

Goal: Learn how to merge branches and push updates.

Steps:

  1. Switch to the main branch:

     git checkout main
    
  2. Merge the feature-branch:

     git merge feature-branch
    
  3. Push changes to the remote repository:

     git push origin main
    

Challenge 4: Use git reset or git revert to Undo a Commit

Goal: Understand commit undoing methods.

Steps:

  1. Use git log to find the commit hash:

     git log --oneline
    
  2. Undo last commit (soft reset):

     git reset --soft HEAD~1
    
  3. If you want to revert changes without losing history:

     git revert HEAD
    

Challenge 5: Rebase feature-branch onto main

Goal: Learn the difference between merge and rebase.

Steps:

  1. Switch to the feature branch:

     git checkout feature-branch
    
  2. Rebase onto main:

     git rebase main
    
  3. Push changes (if necessary, use force push):

     git push origin feature-branch --force
    

Challenge 6: Create a PR on GitHub from a Feature Branch

Goal: Learn how to create a pull request.

Steps:

  1. Push your feature branch to GitHub:

     git push origin feature-branch
    
  2. Go to your repository on GitHub and click Compare & pull request.

  3. Add a description and submit the PR.


Challenge 7: Create Conflicting Changes in Different Branches and Resolve Them

Goal: Understand how to resolve merge conflicts.

Steps:

  1. Make changes in the same file in both main and feature-branch.

  2. Try merging feature-branch into main:

     git checkout main
     git merge feature-branch
    
  3. Resolve conflicts manually and commit the merge.


Challenge 8: Use git stash to Save and Restore Uncommitted Changes

Goal: Learn how to use stash.

Steps:

  1. Stash uncommitted changes:

     git stash
    
  2. List stashes:

     git stash list
    
  3. Apply the most recent stash:

     git stash apply
    

Challenge 9: Add Version Tags to Commits and Push Them

Goal: Learn how to tag commits.

Steps:

  1. Tag the latest commit:

     git tag -a v1.0 -m "Version 1.0"
    
  2. Push the tag to the remote repository:

     git push origin v1.0
    

Challenge 10: Use git commit --amend and git rebase -i to Edit Past Commits

Goal: Modify past commits.

Steps:

  1. Modify the last commit message:

     git commit --amend -m "Updated commit message"
    
  2. Rebase interactively to edit multiple past commits:

     git rebase -i HEAD~3
    
  3. Modify commits as needed and save changes.


This completes all the Git challenges!