Exercise - Change commit history

In this lab, you will learn how to modify commit history in Git to maintain a clean and organized project history. Understanding how to change commit history is an important skill for managing your Git repositories effectively, especially when working in team environments where a clean history helps with code review and debugging.

You will learn how to:

  • Amend the most recent commit
  • Rebase commits interactively
  • Squash multiple commits into one
  • Reword commit messages
  • Understand when and why to change commit history
  • Apply best practices for commit history management

This lab takes approximately 30-45 minutes to complete.

Before you start

To complete the lab, you need:

  • A GitHub user account. If you don't have one, you can create a new account. If you need instructions on how to create a GitHub account, refer to the article Creating an account on GitHub.
  • Basic understanding of Git and commits.
  • A web browser with access to the internet.

Complete the exercise on GitHub

In this exercise, you'll learn to modify commit history through a hands-on GitHub Skills exercise.

Note: This exercise is hosted on GitHub Skills and provides an interactive learning experience. You'll practice various techniques for modifying commit history while understanding the implications of rewriting history.

The exercise consists of the following activities:

  1. Start a web browser and navigate to the exercise repository: https://github.com/skills/change-commit-history

  2. On the exercise page, select the Use this template button to copy the exercise to your GitHub account.

    Note: Simply copy the exercise to your account, then give GitHub about 20 seconds to prepare the first lesson, then refresh the page.

  3. Follow the instructions on the repository's README to complete all the challenges, which include:

    • Amending commits to fix mistakes in the most recent commit
    • Using interactive rebase to modify multiple commits
    • Squashing commits to combine related changes
    • Rewording commit messages for clarity
    • Understanding the risks of rewriting published history
    • Following best practices for commit history management
  4. Work through each step in the exercise, following the prompts and instructions provided.

    Note: Be cautious when rewriting history that has been pushed to shared branches. It's generally safe to rewrite history on feature branches before merging.

  5. When you finish all the challenges, you'll understand how to maintain a clean and professional commit history.

What you've learned

After completing this exercise, you should be able to:

  • Amend the most recent commit message or content
  • Use interactive rebase to modify commit history
  • Squash multiple commits into a single commit
  • Reword commit messages for better clarity
  • Understand when it's safe to rewrite history
  • Apply commit history best practices
  • Use Git commands to clean up your work before review
  • Maintain a professional and readable commit history

Congratulations! You've completed the "Change commit history" exercise and learned how to maintain clean Git history!