How to remove a commit from the middle of a branch

一世执手 提交于 2020-02-01 05:28:04

问题


I've made some commits and have pushed it to my remote repo. One of those I want to remove. It includes only one binary file, which was changed only in that commit in that branch. How to remove it without harm for later commits?


回答1:


You can use interactive (-i) rebase to remove a previous commit.

$ git log                          # copy the target commit 

$ git rebase -i <target-commit>~1  # start rebase from the previous commit of target commit

An editor will open with a list of commits, one per line. Each of these lines begins with pick. Comment out your target commit's line (Put # at the start of target commit line).

OR, you can put drop or d instead of commenting out the line with #.

$ git rebase --continue      # repeat the command until finish rebase

Now, you need force (-f) push to remote since git history is changed.

$ git push -f origin HEAD 


来源:https://stackoverflow.com/questions/42518104/how-to-remove-a-commit-from-the-middle-of-a-branch

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!