How to Delete a Commit but Preserve Changes in Git

John Wachira Mar 11, 2025 Git Git Commit
  1. Using git reset
  2. Using git revert
  3. Using git checkout
  4. Using git cherry-pick
  5. Conclusion
  6. FAQ
How to Delete a Commit but Preserve Changes in Git

When working with Git, it’s not uncommon to find yourself in a situation where you need to delete a commit but want to keep the changes made in that commit. Whether it’s a mistake in your code, an unnecessary commit message, or simply a need to reorganize your commit history, knowing how to remove a commit while preserving your changes is crucial.

In this article, we will explore several methods to achieve this. Each method will be detailed with clear Git commands, ensuring you can follow along easily. By the end, you’ll have a solid understanding of how to manage your Git commits effectively while keeping your changes intact.

Using git reset

One of the most straightforward methods to delete a commit while preserving changes is by using the git reset command. This command allows you to move the HEAD pointer back to a previous commit without losing any changes made in the deleted commit. Here’s how to do it:

Bash
 bashCopygit reset --soft HEAD~1

This command does a soft reset, which means it moves the HEAD pointer back one commit but keeps all the changes in the staging area. You can then review your changes, amend them if necessary, and create a new commit.

Output:

 textCopyHEAD is now at <commit_hash> Your last commit message

After running this command, your last commit is removed from the history, and all the changes are staged. You can use git status to see the staged changes and decide if you want to create a new commit or make further modifications before committing again.

This method is particularly useful when you realize that the last commit contains mistakes or needs adjustments. Instead of starting from scratch, you can simply modify the changes and commit again, keeping your workflow efficient.

Using git revert

If you want to delete a commit but still maintain a clear history, git revert is the way to go. This command creates a new commit that effectively undoes the changes made in the specified commit. Here’s how to use it:

Bash
 bashCopygit revert HEAD

This command will prompt you to enter a commit message for the new commit that undoes the changes made in the last commit. Once you save and exit the editor, the changes will be reverted.

Output:

 textCopyReverting <commit_hash> ... 
[master <new_commit_hash>] Revert "Your last commit message"

Using git revert is beneficial when you want to keep a record of your changes while still undoing them. This is especially useful in collaborative environments where maintaining a clear commit history is essential for your team. Unlike git reset, which alters the commit history, git revert allows you to preserve the integrity of your project’s history while correcting mistakes.

Using git checkout

Another method to delete a commit while preserving changes is by using git checkout. This command allows you to switch to a previous state of your project without losing your current changes. Here’s how you can do it:

Bash
 bashCopygit checkout HEAD~1

This command will take you back to the state of your project before the last commit. However, it’s important to note that this will detach your HEAD, meaning you are no longer on a branch. To keep your changes, you can create a new branch from this state.

Output:

 textCopyNote: checking out 'HEAD~1'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

After checking out the previous commit, you can create a new branch and commit your changes there. This method is particularly useful for exploring different states of your project or when you want to experiment without affecting the main branch.

Using git cherry-pick

If you have multiple commits and want to delete a specific commit while keeping others, git cherry-pick can be handy. This command allows you to apply changes from specific commits. Here’s how to use it:

Bash
 bashCopygit cherry-pick <commit_hash>

First, you can create a new branch from your current state. Then, use git cherry-pick to apply the changes from the specific commit you want to keep.

Output:

 textCopy[branch_name <new_commit_hash>] <commit_message>

This command will apply the changes from the specified commit onto your current branch. If you need to remove a commit, you can revert that specific commit and then cherry-pick the others you want to keep.

This method is particularly useful when managing a larger project with multiple contributors. It allows you to selectively choose which changes to keep while maintaining a clean commit history.

Conclusion

In summary, deleting a commit while preserving changes in Git can be accomplished through various methods, each serving different needs and scenarios. Whether you prefer the straightforward approach of git reset, the history-preserving nature of git revert, or the selective capabilities of git cherry-pick, understanding these commands will enhance your Git proficiency. Remember, the key is to choose the method that best fits your workflow and project requirements. With these tools at your disposal, you can manage your Git commits with confidence.

FAQ

  1. What happens when I use git reset?
    git reset moves the HEAD pointer back to a previous commit without losing changes, depending on the reset type (soft, mixed, or hard).

  2. Can I recover a commit after using git reset?
    Yes, you can recover a commit if you haven’t performed a hard reset. You can use the reflog to find the commit hash.

  3. What is the difference between git revert and git reset?
    git revert creates a new commit that undoes changes, while git reset removes commits from history without preserving them.

  4. Is it safe to use git reset on a shared branch?
    No, using git reset on a shared branch can cause issues for others. It’s better to use git revert in collaborative environments.

  5. How do I see my commit history in Git?
    You can see your commit history by using the command git log.

Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe
Author: John Wachira
John Wachira avatar John Wachira avatar

John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.

LinkedIn

Related Article - Git Commit