Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

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

like image 246
K M Dilshan Udara Avatar asked Dec 21 '16 11:12

K M Dilshan Udara


People also ask

How do I remove a specific commit in git?

In order to remove a specific file from a Git commit, use the “git reset” command with the “–soft” option, specify the commit before HEAD and the file that you want to remove.

How do you remove one commit from the middle?

Deleting the "Middle" Commit from the History. All you need to do is typing "drop" at the beginning of each commit you want to delete. Be careful while using the git rebase command, as it may cause sudden problems.

How do I delete a specific commit in history?

By deleting line of the commit or writing drop instead of pick, you can remove the commit from history. Thus, we deleted a commit locally. To remove it from remote repository, we should push our changes to remote. The + sign before the name of the branch you are pushing, this tells git to force the push.

How do you delete one commit from a branch?

do the revert (or use git rebase -i <something before the bad commit> <temporary branch> to remove the bad commit) redo the merge. rebase your subsequent work back on: git rebase --onto <temporary branch> <old merge commit> <real branch> remove the temporary branch.


2 Answers

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.

like image 67
Loïc Faure-Lacroix Avatar answered Sep 18 '22 15:09

Loïc Faure-Lacroix


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.

like image 37
Tim Biegeleisen Avatar answered Sep 16 '22 15:09

Tim Biegeleisen