
Occasionally, you may push unwanted changes or sensitive data to a Git repository, whether local or remote. Git provides flexible tools to rewrite commit history, allowing you to remove sensitive information, undo mistakes, and clean up your commit log. This article demonstrates how to remove commits from your Git history using safe and effective techniques for both local and shared repositories.
You can view the commit history in Git either in a compact single-line format or a detailed full log. This helps you inspect previous commits and decide which ones to remove or recover.
git log: Displays the full commit history with commit hashes, author, date, and commit message.[OPTIONS]--oneline: Condenses each commit to a single line showing the commit hash and commit message.View compact commit history.
Output:
View detailed commit history.
Output:
Use the arrow keys to scroll through the log and press q to quit the view.
Use the git reset command to undo your most recent commit by moving HEAD backward. You can choose whether to keep, unstage, or discard your changes depending on the reset mode you use.
--soft: Keep changes staged.--mixed: Unstage changes, leave them in working directory (default).--hard: Discard all changes (permanently resets working directory and index).HEAD~N: Moves HEAD back by N commits (e.g., HEAD~1 targets the last commit).Keep changes staged.
Unstage changes.
Discard changes entirely.
To remove a specific commit that is not the most recent one, use an interactive rebase. This allows you to review a range of recent commits and selectively remove (drop) individual ones.
git rebase -i: Starts an interactive rebase.HEAD~N: Specifies how many commits before HEAD to display for editing (for example, HEAD~8 shows the last 8 commits).drop any commit you want to remove.Start an interactive rebase to edit the last 8 commits.
Output.
In the editor:
i to enter insert mode.pick to drop for the commit you want to remove:To save:
Esc to exit insert mode.:wq and press Enter.Git will process the rebase:
If you accidentally remove a commit using git reset or git rebase, you can often recover it using the git reflog command. The reflog records where your HEAD and branch pointers have been, even after destructive changes.
However, recovery depends on:
If you performed a hard reset, use:
If you performed a mixed reset, use:
If you performed a soft reset, use:
First, display your reflog to locate the commit you want to recover.
Output.
HEAD@{0}: Current HEAD.HEAD@{1}, HEAD@{2}: Prior reflog entries.HEAD@{3}: Entry representing the state before the hard reset.You can also preview a reflog entry before recovering:
To reset your branch to a prior reflog state:
Output.
Alternatively, you can reset using the exact commit hash.
You can preview it first:
In Git, you can remove a file or directory from future commits using:
However, this does not erase the file or directory from your commit history, it only prevents it from appearing in future commits. To permanently remove a file or directory from all commits in your repository history, you must rewrite your commit history.
git filter-repogit filter-repo is the modern replacement for git filter-branch. It is faster, safer, and more reliable for permanently removing files or directories from commit history. It is not included with Git by default but can be installed as a plugin.
Install git filter-repo.
Back up your repository.
Remove a file from history.
Verify the file is gone.
No output indicates success.
Remove a directory from history.
Verify the directory is gone.
No output indicates success.
In this article, you explored multiple ways to remove commits from a Git commit history. You learned how to safely undo recent commits with git reset, remove specific commits using interactive rebase, and permanently rewrite history with tools like git filter-repo. You also saw how to recover removed commits using git reflog.
When working with shared repositories, always prefer git revert to create a new commit that undoes changes without altering history. Rewriting commit history should be used with caution, especially on branches that have already been pushed and shared with others.
0 Comments
Be the first to comment and share your perspective with the community.