How to delete a only a specific commit in the middle of the git log

Git

Git Problem Overview


I want only to delete a specific commit in the middle of a git log. It shouldn't be affected to above commits.

Git Solutions


Solution 1 - Git

If the commit were not pushed to a remote server, you can use git rebase to modify the git history.

You can do that:

git rebase -i commit-hash^

It will show a list of commits. The first line should be the commit you want to delete. Remove that line from the file you edited, save and close.

If you did push to a remote server prior to do that, note that the commits after the commit you just deleted were rewritten based on the commit before the one you just deleted from the history. For that particular reason, all of the commits are changed because your history just diverged. So if you try to push that, it won't be fast-forward and you'll have to force the push.

For that reason, if you're not familiar with git rebase I suggest keeping a branch pointing to the branch you were modifying the history in case something goes wrong.

Also, if that particular commit is shared accross multiple branches, diverging the history of one branch will cause multiple problems as you won't be able to easily merge one branch from the old history to the branch in the new history. It will duplicate many commit and the commit you deleted in one branch won't be deleted in the others.

Think of it has modifying git history is like time travel. Any change at one point create a new parallel universe. Everything that was after the change in the past really is impacted by the change you did in the past.

Solution 2 - Git

> It shouldn't be affected to above commits.

If you remove a commit from the middle of a branch, or really anywhere except possibly the HEAD of a branch, you will rewrite the history of that branch. This is because the tools you would use to effect this, namely git rebase -interactive and git filter-branch, would need to recommit everything which comes after the commit you removed. This is usually not desirable for a public shared branch because it forces everyone to take pretty draconian measures to keep up with that change.

Rather, a safer bet for you would be to use git revert:

git revert <SHA-1 of commit to delete>

This will add a new commit on the top of your branch which effectively undoes everything which the middle commit did.

By the way, if you really want to remove that commit, I would recommend doing an interactive rebase.

Solution 3 - Git

You can use git cherry-pick for this. Let's say your git log looks like this. and you want to remove the 2nd commit from your branch.

COMMITS    MESSAGES
abcd123    4th commit message
xyze456    3rd commit message
98y65r4    2nd commit message wants to be removed.
bl8976t    1st commit message

STEP 1 - Checkout the last correct commit: git checkout bl8976t

STEP 2 - Create a new branch from there: git checkout -b commit-remove

STEP 3 - Cherry pick commits you want to keep: git cherry-pick xyze456 and git cherry-pick abcd123

STEP 4 - Checkout your original branch: git checkout master

STEP 5 - Reset original branch to last usable commit: git reset --hard bl8976t

STEP 6 - Merge the new branch onto the original branch: git merge commit-remove

STEP 7 - Check that the original branch has the correct commits: git log

STEP 8 - Push the original branch to the remote repo: git push --hard origin original-branch-name

You are done.

Beware that this could have adverse effects on other users who are working on the same repository/branch.

Solution 4 - Git

if you're using intelliJ

Say your branch X is something like this

commit [A] <-- last commit

commit [B]

commit [C]

commit [D]

And you want to delete the 2 commits B and C.

1- Right click commit D using the git log dialog (last commit before the one you want to delete) and create a new branch from it call it Y, intellij will automatically checkout at branch Y

New branch

3- You can see your branches in the left column of the git log dialog, right click on branch X and select Compare with current

Compare with current

Now you'll see this dialog

Compare dialog

4- You can right click commit A and choose Cherry-pick

Cherry pick

Now in branch Y you have commit A and D, resolve the conflicts, commit and push.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionK M Dilshan UdaraView Question on Stackoverflow
Solution 1 - GitLoïc Faure-LacroixView Answer on Stackoverflow
Solution 2 - GitTim BiegeleisenView Answer on Stackoverflow
Solution 3 - GitSureshView Answer on Stackoverflow
Solution 4 - GitAlya GomaaView Answer on Stackoverflow