Remove 4 commits from my git history

12,485

Solution 1

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. :)

Solution 2

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.

Share:
12,485
Hommer Smith
Author by

Hommer Smith

Updated on June 23, 2022

Comments

  • Hommer Smith
    Hommer Smith about 2 years

    I have done some commits and pushed them into my repository. Then I did a pull request but I realized there are some commits I don't want to be in the pull request.

    They look like this:

    My commits look like this:
    
    Correct HTML    
    ab1c41c 
    
    HTML escaping   
    8b38955 
    
    Merge branch 'master' into internationalized    
    2854662
    
    Modified config 
    b942f13 
    
    tried pushing   
    b73d792 
    
    Added assets    
    f20106e 
    
    Added config    
    408118f 
    
    Fixed views conflicts   
    86f2509 
    
    added layouts   
    da27e11 
    
    Fixed layout markup
    92d6bcc 
    

    If I want to get rid of these commits:

    Modified config 
    b942f13 
    
    tried pushing   
    b73d792 
    
    Added assets    
    f20106e 
    
    Added config    
    408118f 
    

    How do I do it? Notice that I want to keep these ones which are before in time that the ones i want to delete:

    Correct HTML    
    ab1c41c 
    
    HTML escaping   
    8b38955