Table of contents
- Weapons you need in your arsenal
- Git Exercises for College Students
- Exercise 1: Initializing a Repository
- Exercise 2: Making Changes
- Exercise 3: Viewing Changes
- Exercise 4: Branching
- Exercise 5: Merging
- Exercise 6: Resolving Conflicts
- Exercise 7: Remote Repositories
- Exercise 8: Cloning
- Exercise 9: Pulling Changes
- Exercise 10: Collaboration
- Exercise 11: Undoing Changes
- Exercise 12: Stashing Changes
- Exercise 13: Rebasing
- Exercise 14: Viewing History
- Exercise 15: Tagging Releases
- Exercise 16: Collaborative Workflow
- Exercise 17: Gitignore
- Exercise 18: Git Hooks
Welcome, everyone, to this one-day crash course on GIT! Whether you're new to version control or looking to refresh your skills, get ready for an exciting journey packed with learning and exploration.
Today, we'll cover the essentials of Git through a series of 18 exercises. Don't worry if you're new to this โ I'll take it step by step, ensuring you grasp each concept with ease.
Let's dive in! Below, you'll find the exercises designed to help you understand Git fundamentals. Feel free to move at your own pace, and remember, there's no rush!
Weapons you need in your arsenal
GIT Installed on your system (Download Here).
Any IDE (VS Code is recommended).
An account on Github (Create Here).
Git Exercises for College Students
Exercise 1: Initializing a Repository
Description: Initialize a new Git repository and create a
Readme.md
file.Steps:
Open your terminal.
Navigate to the desired directory for your project.
Run the following commands:
git init echo "# My Project" > Readme.md git add Readme.md git commit -m "Initial commit: Add Readme.md"
Exercise 2: Making Changes
Description: Make changes to the
Readme.md
file and commit them.Steps:
Open
Readme.md
in your preferred text editor.Add some text or make modifications to the file.
Save the changes.
Run the following commands:
git add Readme.md git commit -m "Update Readme.md: Add project description"
Exercise 3: Viewing Changes
Description: View the differences between commits.
Steps:
Make additional changes to the
Readme.md
file.Save the changes.
Run the following command to view the differences:
git diff commit_id_1 commit_id_2
Exercise 4: Branching
Description: Create a new branch and switch to it.
Steps:
Run the following commands:
git branch feature git checkout feature
Make changes to the
Readme.md
file within thefeature
branch.Add and commit the changes.
Exercise 5: Merging
Description: Merge changes from the
feature
branch into themain
branch.Steps:
Switch back to the
main
branch:git checkout main
Merge changes from the
feature
branch:git merge feature
Exercise 6: Resolving Conflicts
Description: Simulate a merge conflict and resolve it.
Steps:
Make conflicting changes to the
Readme.md
file in both themain
andfeature
branches.Attempt to merge the
feature
branch intomain
:git merge feature
Resolve the conflicts in the file.
Add and commit the resolved file.
Exercise 7: Remote Repositories
Description: Connect your local repository to a remote repository on GitHub.
Steps:
Create a new repository on GitHub.
Follow the instructions on GitHub to add the remote repository URL to your local repository.
Push your local changes to the remote repository:
git push origin main
Exercise 8: Cloning
Description: Clone a repository from GitHub to your local machine.
Steps:
Find a repository on GitHub that you want to clone.
Copy the repository URL.
Open your terminal and navigate to the directory where you want to clone the repository.
Run the following command:
git clone <repository_url>
Exercise 9: Pulling Changes
Description: Fetch and merge changes from a remote repository.
Steps:
Navigate to your local repository.
Run the following command to fetch and merge changes from the remote repository:
git pull origin main
Exercise 10: Collaboration
Description: Collaborate with a classmate on a Git repository.
Steps:
Find a classmate to collaborate with.
Add them as a collaborator on your GitHub repository.
Have your classmate clone the repository to their local machine.
Make changes to the repository and push them to GitHub.
Fetch and merge their changes into your local repository.
Exercise 11: Undoing Changes
Description: Undo changes made to a file and discard them.
Steps:
Make changes to the
Readme.md
file.Run the following command to discard the changes and revert to the last committed state:
git checkout -- Readme.md
Exercise 12: Stashing Changes
Description: Temporarily store changes to work on something else.
Steps:
Make changes to the
Readme.md
file.Run the following command to stash the changes:
git stash
Make additional changes or switch branches.
Retrieve the stashed changes:
git stash pop
Exercise 13: Rebasing
Description: Reapply commits on top of another branch.
Steps:
Create a new branch:
git branch experiment git checkout experiment
Make changes and commit them.
Switch back to the
main
branch:git checkout main
Run the following command to rebase the
experiment
branch ontomain
:git rebase experiment
Exercise 14: Viewing History
Description: View commit history and explore different options.
Steps:
Run the following command to view commit history:
git log
Explore different options such as formatting, filtering by author, date range, etc.
Exercise 15: Tagging Releases
Description: Tag a specific commit as a release.
Steps:
Identify the commit you want to tag:
git log
Run the following command to tag the commit:
git tag v1.0 <commit_id>
Push the tag to the remote repository:
git push origin v1.0
Exercise 16: Collaborative Workflow
Description: Simulate a typical collaborative workflow with branching and merging.
Steps:
Divide into pairs (A and B).
A creates a new branch, makes changes, and commits them.
A pushes the branch to the remote repository.
B pulls the branch, reviews the changes, and merges them into the
main
branch.Switch roles and repeat the process.
Exercise 17: Gitignore
Description: Ignore certain files or directories from version control.
Steps:
Create a
.gitignore
file in the root of your repository.Add file patterns or directory names to ignore.
Commit the
.gitignore
file.Verify that ignored files are not included in Git's tracking.
Exercise 18: Git Hooks
Description: Automate tasks with Git hooks.
Steps:
Create a Git hook script (e.g., pre-commit, post-commit) in the
.git/hooks
directory of your repository.Add desired commands or scripts to the hook.
Make changes and commit to trigger the hook.
Verify that the hook executes the specified tasks.
Congratulations on embarking on this learning journey! By the end of the day, you'll have a solid foundation in Git and be well-prepared to continue exploring on your own.
Thank you for joining us today. Let's make the most of our time and dive into the exciting world of version control with Git!