Practice Questions and Exercises¶
Test your Git and GitHub knowledge with these comprehensive exercises. Each section builds upon the previous one, helping you develop practical skills.
๐ Theoretical Questions¶
Beginner Level¶
- What is Git, and how does it differ from other version control systems?
- Explain the concept of distributed version control
-
Compare Git with centralized systems like SVN
-
What is the purpose of the "git init" command?
- Describe what happens when you initialize a repository
-
Explain the
.git
folder structure -
What does the "git clone" command do, and how is it used?
- Explain the difference between cloning and downloading
-
Provide examples of cloning from different sources
-
Describe the significance of the ".gitignore" file in a Git repository.
- Explain why certain files should be ignored
-
Provide examples of commonly ignored files
-
What is the difference between a Git commit and a Git push?
- Explain local vs remote operations
- Describe the relationship between commits and pushes
Intermediate Level¶
- Explain the difference between Git's "fetch" and "pull" commands.
- Describe what each command does
-
Explain when to use each one
-
What is a Git branch, and why is it useful?
- Explain the concept of branching
-
Describe common branching strategies
-
Explain the difference between Git's "merge" and "rebase" commands.
- Compare the two approaches to integrating changes
-
Discuss the pros and cons of each
-
How do you resolve merge conflicts in Git?
- Explain what causes merge conflicts
-
Describe the resolution process step by step
-
How do you view the commit history in Git, and what information does it provide?
- Explain different ways to view history
- Describe the information contained in commits
Advanced Level¶
-
How do you revert changes to a single file in Git without affecting other files?
- Explain different scenarios for reverting changes
- Provide commands for each scenario
-
Explain the purpose of Git's "stash" feature and when it is typically used.
- Describe the stash workflow
- Provide practical use cases
-
What are Git hooks, and how can they be used to automate tasks?
- Explain different types of hooks
- Provide examples of automation use cases
-
Describe the steps to create a new Git repository on GitHub and push your local repository to it.
- Walk through the complete process
- Include both command line and web interface steps
-
Describe the process of creating and applying a Git patch.
- Explain when patches are useful
- Provide step-by-step instructions
๐ ๏ธ Practical Exercises¶
Exercise 1: Repository Basics¶
Objective: Master basic repository operations
- Create a new directory called
git-practice
- Initialize a Git repository
- Create a
README.md
file with project description - Stage and commit the file
- Check the commit history
Expected Commands:
mkdir git-practice
cd git-practice
git init
echo "# Git Practice Project" > README.md
git add README.md
git commit -m "Initial commit: Add README"
git log
Exercise 2: Working with Files¶
Objective: Practice file management and staging
-
Create the following file structure:
-
Add content to each file
- Stage and commit all files
- Modify
index.html
andstyles.css
- Stage only the HTML file and commit
- Stage and commit the CSS file separately
Verification: Use git log --oneline
to see your commits
Exercise 3: Branching Workflow¶
Objective: Learn branching and merging
- Create a new branch called
feature/navigation
- Switch to the new branch
- Add a navigation menu to
index.html
- Commit the changes
- Switch back to
main
branch - Create another branch called
feature/footer
- Add a footer to
index.html
- Commit and merge both branches to main
Challenge: What happens if both branches modify the same part of the file?
Exercise 4: Remote Repository¶
Objective: Practice remote operations
- Create a new repository on GitHub
- Add the GitHub repository as a remote origin
- Push your local repository to GitHub
- Make changes on GitHub (edit README.md)
- Pull the changes to your local repository
- Make local changes and push them
Commands to Research:
- git remote add origin <url>
- git push -u origin main
- git pull origin main
Exercise 5: Conflict Resolution¶
Objective: Handle merge conflicts
- Create two branches:
branch-a
andbranch-b
- In both branches, modify the same line in
README.md
differently - Merge
branch-a
intomain
- Try to merge
branch-b
intomain
- Resolve the conflict manually
- Complete the merge
Learning Goal: Understand conflict markers and resolution process
Exercise 6: Git Ignore¶
Objective: Practice ignoring files
- Create temporary files:
temp.log
,cache.tmp
,secret.env
- Create a
node_modules/
directory with dummy files - Stage all files and notice what gets included
- Create a
.gitignore
file to ignore: - All
.log
files - All
.tmp
files - All
.env
files - The
node_modules/
directory - Check git status and verify files are ignored
Sample .gitignore:
Exercise 7: Advanced Git Operations¶
Objective: Practice advanced features
- Make several commits with different messages
- Use
git log --oneline
to view history - Create an alias:
git config --global alias.lg "log --oneline --decorate --graph"
- Use
git stash
to temporarily save work - Switch branches and apply the stash
- Practice using
git diff
to see changes
๐งช Challenge Projects¶
Challenge 1: Collaborative Website¶
Scenario: Work with a partner (or simulate multiple users)
- Setup: Create a shared repository for a simple website
- Branching: Each person creates feature branches
- Development: Add different pages/features simultaneously
- Integration: Practice merging and resolving conflicts
- Deployment: Use GitHub Pages to deploy the site
Skills Practiced: - Collaborative workflows - Branch management - Conflict resolution - Deployment process
Challenge 2: Open Source Contribution¶
Scenario: Contribute to a real open-source project
- Find Project: Look for "good first issue" labels on GitHub
- Fork: Fork the repository to your account
- Clone: Clone your fork locally
- Branch: Create a feature branch for your contribution
- Develop: Make your changes and test them
- Submit: Create a pull request
Skills Practiced: - Fork workflow - Pull request process - Code review - Community interaction
Challenge 3: Git Workflow Automation¶
Scenario: Set up automated workflows
- Hooks: Create pre-commit hooks for code formatting
- Actions: Set up GitHub Actions for testing
- Branching: Implement Git Flow workflow
- Release: Practice semantic versioning and releases
Skills Practiced: - Git hooks - CI/CD basics - Workflow automation - Release management
โ Self-Assessment Quiz¶
Quick Knowledge Check¶
- What command shows the current status of your repository?
- How do you unstage a file that was added with
git add
? - What's the difference between
git checkout
andgit switch
? - How do you view changes before committing?
- What command shows the difference between two branches?
Click to see answers
1. `git status` 2. `git restore --staged๐ฏ Next Steps¶
After completing these exercises:
- Practice Daily: Use Git for all your projects
- Explore Advanced Topics: Learn about rebasing, cherry-picking, and bisect
- Join Communities: Participate in open-source projects
- Teach Others: Share your knowledge with fellow developers
๐ Exercise Solutions¶
Detailed solutions and explanations for these exercises can be found in our GitHub repository. Feel free to compare your approaches and learn from different solutions!
Learning Tip
The best way to learn Git is by using it regularly. Don't be afraid to experiment with test repositories - you can always start over if something goes wrong!