Skip to content

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

  1. What is Git, and how does it differ from other version control systems?
  2. Explain the concept of distributed version control
  3. Compare Git with centralized systems like SVN

  4. What is the purpose of the "git init" command?

  5. Describe what happens when you initialize a repository
  6. Explain the .git folder structure

  7. What does the "git clone" command do, and how is it used?

  8. Explain the difference between cloning and downloading
  9. Provide examples of cloning from different sources

  10. Describe the significance of the ".gitignore" file in a Git repository.

  11. Explain why certain files should be ignored
  12. Provide examples of commonly ignored files

  13. What is the difference between a Git commit and a Git push?

  14. Explain local vs remote operations
  15. Describe the relationship between commits and pushes

Intermediate Level

  1. Explain the difference between Git's "fetch" and "pull" commands.
  2. Describe what each command does
  3. Explain when to use each one

  4. What is a Git branch, and why is it useful?

  5. Explain the concept of branching
  6. Describe common branching strategies

  7. Explain the difference between Git's "merge" and "rebase" commands.

  8. Compare the two approaches to integrating changes
  9. Discuss the pros and cons of each

  10. How do you resolve merge conflicts in Git?

  11. Explain what causes merge conflicts
  12. Describe the resolution process step by step

  13. 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

  1. 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
  2. Explain the purpose of Git's "stash" feature and when it is typically used.

    • Describe the stash workflow
    • Provide practical use cases
  3. What are Git hooks, and how can they be used to automate tasks?

    • Explain different types of hooks
    • Provide examples of automation use cases
  4. 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
  5. 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

  1. Create a new directory called git-practice
  2. Initialize a Git repository
  3. Create a README.md file with project description
  4. Stage and commit the file
  5. 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

  1. Create the following file structure:

    git-practice/
    โ”œโ”€โ”€ src/
    โ”‚   โ”œโ”€โ”€ index.html
    โ”‚   โ””โ”€โ”€ styles.css
    โ”œโ”€โ”€ docs/
    โ”‚   โ””โ”€โ”€ notes.txt
    โ””โ”€โ”€ README.md
    

  2. Add content to each file

  3. Stage and commit all files
  4. Modify index.html and styles.css
  5. Stage only the HTML file and commit
  6. 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

  1. Create a new branch called feature/navigation
  2. Switch to the new branch
  3. Add a navigation menu to index.html
  4. Commit the changes
  5. Switch back to main branch
  6. Create another branch called feature/footer
  7. Add a footer to index.html
  8. 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

  1. Create a new repository on GitHub
  2. Add the GitHub repository as a remote origin
  3. Push your local repository to GitHub
  4. Make changes on GitHub (edit README.md)
  5. Pull the changes to your local repository
  6. 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

  1. Create two branches: branch-a and branch-b
  2. In both branches, modify the same line in README.md differently
  3. Merge branch-a into main
  4. Try to merge branch-b into main
  5. Resolve the conflict manually
  6. Complete the merge

Learning Goal: Understand conflict markers and resolution process

Exercise 6: Git Ignore

Objective: Practice ignoring files

  1. Create temporary files: temp.log, cache.tmp, secret.env
  2. Create a node_modules/ directory with dummy files
  3. Stage all files and notice what gets included
  4. Create a .gitignore file to ignore:
  5. All .log files
  6. All .tmp files
  7. All .env files
  8. The node_modules/ directory
  9. Check git status and verify files are ignored

Sample .gitignore:

# Logs
*.log

# Temporary files
*.tmp

# Environment files
*.env

# Dependencies
node_modules/

Exercise 7: Advanced Git Operations

Objective: Practice advanced features

  1. Make several commits with different messages
  2. Use git log --oneline to view history
  3. Create an alias: git config --global alias.lg "log --oneline --decorate --graph"
  4. Use git stash to temporarily save work
  5. Switch branches and apply the stash
  6. Practice using git diff to see changes

๐Ÿงช Challenge Projects

Challenge 1: Collaborative Website

Scenario: Work with a partner (or simulate multiple users)

  1. Setup: Create a shared repository for a simple website
  2. Branching: Each person creates feature branches
  3. Development: Add different pages/features simultaneously
  4. Integration: Practice merging and resolving conflicts
  5. 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

  1. Find Project: Look for "good first issue" labels on GitHub
  2. Fork: Fork the repository to your account
  3. Clone: Clone your fork locally
  4. Branch: Create a feature branch for your contribution
  5. Develop: Make your changes and test them
  6. 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

  1. Hooks: Create pre-commit hooks for code formatting
  2. Actions: Set up GitHub Actions for testing
  3. Branching: Implement Git Flow workflow
  4. Release: Practice semantic versioning and releases

Skills Practiced: - Git hooks - CI/CD basics - Workflow automation - Release management

โœ… Self-Assessment Quiz

Quick Knowledge Check

  1. What command shows the current status of your repository?
  2. How do you unstage a file that was added with git add?
  3. What's the difference between git checkout and git switch?
  4. How do you view changes before committing?
  5. What command shows the difference between two branches?
Click to see answers 1. `git status` 2. `git restore --staged ` or `git reset HEAD ` 3. `git switch` is newer and specifically for switching branches; `git checkout` has multiple purposes 4. `git diff` (for unstaged) or `git diff --staged` (for staged changes) 5. `git diff branch1..branch2`

๐ŸŽฏ Next Steps

After completing these exercises:

  1. Practice Daily: Use Git for all your projects
  2. Explore Advanced Topics: Learn about rebasing, cherry-picking, and bisect
  3. Join Communities: Participate in open-source projects
  4. 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!