Git

How to Remove Merge Commit From History

When a developer wants to combine the content of two branches or repositories, the “git merge” command can be used to merge them. When the merging operation is performed, developers are required to update the Git repository by committing for later use. Sometimes, users want to delete the merging commit from the log history. For this purpose, they need to use the “git rebase” command iteratively.

This post will describe the way of removing merged commits from the log history.

How to Remove Merge Commit From History?

To remove the merged commit from the Git reference log history, check out the provided steps:

    • Move to the Git root directory.
    • Check the short version of the merged commit SHA-hash history.
    • Execute the “git rebase i <commit-id>” command to remove the merge commit from the Git history and verify it.

Step 1: Switch to Git Root Directory

At first, execute the “cd” command and redirect to the Git root folder:

$ cd "C:\Users\nazma\Git"

 
Step 2: Check Merge Log History

Then, get the merge log history of the current working branch by running the “git log” command:

$ git log --merges --oneline

 
Here, the “–merge” option represents the merged commit history, and the “–oneline” flag is used to get output in a single line. Now, we want to remove the most recent merge commit from the history and move the HEAD pointer to the below-highlighted commit SHA-hash:


Step 3: Rebase Selected Commit

To remove the git merge commit from the history to a new base commit, execute the “git rebase” command with the “i” flag for an iterative process and particular commit id:

$ git rebase -i d4f4e96

 
When the above-provided command is executed, a text editor will open along with the merge commit detailed, place the word “pick” with the “d” for deleting beside all the desired commit hash id:


After that, save the changes and close the file. When the opened file will close, the below-given message will appear as output:


Step 4: Verify Removed Merge Commit

To ensure that the selected merge commit is removed from the log history or not, run the provided command:

$ git log --merges --oneline

 
As you can see, the selected merged commit is successfully removed from the Git log:


That’s it! You have learned the process of deleting merge commits from the Git log history.

Conclusion

To remove the merged commit from the Git reference log history, first, move to the Git root directory and view the short version of the merged commit SHA-hash history. Then, run the “git rebase i <commit-id>” command to remove the merge commit from the Git history and verify it. This post demonstrated the way of removing merged commits from the log history.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.