Remove 4 commits from my git history

喜夏-厌秋 提交于 2019-12-05 01:30:14
Blake Taylor

You can do an interactive rebase.

Assuming your head is at ab1c41c from your example, invoke the rebase as follows

git rebase -i HEAD~7

This tells git you want to manipulate the last 8 or so commits. You'll be dropped into your editor with a listing of the commits and some instructions.

Delete the lines that contain the commits to remove, save and quit. Git will preform the rebase, and that's it.

Keep in mind, because of the rebase, if you want to push to the same branch you'll need to pass the option --force.

Disclaimer Rebasing and force pushing can cause you to lose work or piss people off, so just make sure you understand what you're doing. :)

Karthik Bose

As Blake Taylor mentioned here, you can use interactive rebase to reorder commits (or) remove intermediate commits.

But that must be done, before you push those commits into public repository. Refer here. In your case, those commits are already available in the public repo. So, I don't suggest using rebase.

If you don't want those commits in your working directory.

  • a) Create a branch, which points to 86f2509 (the last stable commit in your working tree).

    git checkout -b <branch name> 86f2509

  • b) Cherry Pick those commits you want. In your case ab1c41c and 8b38955.

    git cherry-pick 8b38955 ab1c41c

Now your working directory will not have those unwanted commits.

Just reset with --hard on the commit just before:

git reset --hard 86f2509

Beware: you'll lose those commits forever, no way to go back. Also, if someone has pulled in the commits you want to get rid of, things may get messy. In that case, you'd probably want to look at 'git revert'.

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