codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
📦

Git & GitHub

48 / 63 topics
47Using BFG Repo-Cleaner and Filter-Repo48Rewriting Commit History49Squashing Commits50Using git amend for Commit Corrections
Tutorials/Git & GitHub/Rewriting Commit History
📦Git & GitHub

Rewriting Commit History

Updated 2026-04-20
4 min read

Rewriting Commit History

Introduction

Git is a powerful version control system that allows developers to track changes in their codebase over time. However, sometimes the commit history can become cluttered or contain errors. In such cases, rewriting commit history becomes necessary. This tutorial will guide you through the process of rewriting commit history using Git commands.

Why Rewrite Commit History?

  • Cleaning Up: Remove sensitive information, fix typos in commit messages, or squash multiple commits into a single one.
  • Collaboration: Ensure a clean and understandable commit history for better collaboration among team members.
  • Performance: Reduce the size of the repository by removing unnecessary files.

Prerequisites

Before you start rewriting commit history, ensure that:

  • You have Git installed on your machine.
  • You are familiar with basic Git commands like git clone, git add, git commit, and git push.
  • You have a local copy of the repository where you want to rewrite the commit history.

Basic Commands for Rewriting History

1. Interactive Rebase (git rebase -i)

Interactive rebase allows you to edit, reorder, or squash commits in your history.

Steps:

  1. Start an interactive rebase:

    git rebase -i HEAD~n
    

    Replace n with the number of commits you want to modify.

  2. Edit the commit list: A text editor will open displaying a list of commits. Each line corresponds to a commit, and the action (e.g., pick, squash) is listed at the beginning of each line.

  3. Modify actions:

    • pick: Keep the commit as is.
    • reword or r: Edit the commit message.
    • squash or s: Combine this commit with the previous one.
    • edit or e: Pause at this commit to make changes.
  4. Save and close: Save the changes and exit the editor. Git will then apply the specified actions to the commits.

  5. Edit commits (if needed): If you chose edit, Git will pause at that commit, allowing you to make changes. After making changes, continue the rebase:

    git add .
    git rebase --continue
    
  6. Resolve conflicts: If there are any conflicts during the rebase, resolve them and continue:

    git add .
    git rebase --continue
    

Example:

Suppose you have the following commit history:

commit 3: Add feature X
commit 2: Fix bug Y
commit 1: Initial commit

You want to squash commits 2 and 3 into a single commit. You would start an interactive rebase with git rebase -i HEAD~3. The editor might display:

pick 1 Initial commit
pick 2 Fix bug Y
pick 3 Add feature X

Change it to:

pick 1 Initial commit
squash 2 Fix bug Y
squash 3 Add feature X

Save and close the editor. Git will then combine commits 2 and 3 into a single commit.

2. Amend Last Commit (git commit --amend)

If you need to make changes to the most recent commit, use git commit --amend.

Steps:

  1. Make changes:

    git add .
    
  2. Amend the last commit:

    git commit --amend
    
  3. Edit the commit message (if needed).

  4. Force push if necessary: If you have already pushed the commit to a remote repository, you will need to force push:

    git push origin <branch-name> --force
    

Example:

Suppose you made a typo in your last commit message. You can fix it with:

git commit --amend -m "Corrected commit message"

3. Reset to a Previous Commit (git reset)

If you want to undo changes and go back to a previous commit, use git reset.

Types of Resets:

  • Soft Reset: Unstages the changes but keeps them in your working directory.

    git reset --soft <commit-hash>
    
  • Mixed Reset (Default): Unstages the changes and resets the index to match the specified commit, but keeps the changes in your working directory.

    git reset --mixed <commit-hash>
    
  • Hard Reset: Discards all changes and resets both the index and the working directory to match the specified commit.

    git reset --hard <commit-hash>
    

Example:

Suppose you want to undo the last two commits without losing any changes:

git reset HEAD~2

Advanced Techniques

1. Filter-Branch (git filter-branch)

Filter-branch is a powerful tool for rewriting history, especially useful for removing sensitive information.

Steps:

  1. Identify the commit or range: Determine which commits or files you want to modify.

  2. Run filter-branch:

    git filter-branch --tree-filter 'rm -f path/to/sensitive/file' HEAD
    
  3. Force push the changes:

    git push origin <branch-name> --force
    

Example:

Suppose you want to remove a sensitive file from all commits:

git filter-branch --tree-filter 'rm -f path/to/sensitive/file' HEAD

2. BFG Repo-Cleaner

BFG Repo-Cleaner is a faster alternative to git filter-branch for removing large files or sensitive data.

Steps:

  1. Download and install BFG:

    wget https://repo1.maven.org/maven2/com/madgag/bfg/1.13.0/bfg-1.13.0.jar
    
  2. Run BFG:

    java -jar bfg-1.13.0.jar --delete-files path/to/sensitive/file my-repo.git
    
  3. Clean and rewrite history:

    cd my-repo.git
    git reflog expire --expire=now --all && git gc --prune=now --aggressive
    
  4. Force push the changes:

    git push origin <branch-name> --force
    

Example:

Suppose you want to remove a large file from all commits:

java -jar bfg-1.13.0.jar --delete-files path/to/large/file my-repo.git

Best Practices

  • Communicate with Team Members: Rewriting commit history can affect others who have already cloned the repository. Always communicate with your team before making significant changes.

  • Backup Your Repository: Before rewriting history, ensure you have a backup of your repository. This way, you can restore it if something goes wrong.

  • Use Force Push Sparingly: Only use git push --force when necessary and communicate with your team about the changes.

  • Test Changes Locally: Always test your changes locally before pushing them to a remote repository.

Conclusion

Rewriting commit history is a powerful feature in Git that can help you maintain a clean and efficient codebase. Whether you need to fix typos, squash commits, or remove sensitive information, understanding how to use git rebase, git commit --amend, and other tools will greatly enhance your workflow. Always remember to communicate with your team and back up your repository before making significant changes.

By following the guidelines and best practices outlined in this tutorial, you can effectively manage your Git repositories and maintain a high-quality commit history.


PreviousUsing BFG Repo-Cleaner and Filter-RepoNext Squashing Commits

Recommended Gear

Using BFG Repo-Cleaner and Filter-RepoSquashing Commits