Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
112 views
in Technique[技术] by (71.8m points)

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

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

question from:https://stackoverflow.com/questions/66056149/can-i-boot-out-a-published-git-commit

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...