Hommer Smith
Hommer Smith

Reputation: 27852

Remove 4 commits from my git history

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

Upvotes: 18

Views: 17321

Answers (4)

Nick
Nick

Reputation: 137

You could also run git rebase -i 92d6bcc this will open a vim text editor with something like:

pick <commit hash> <commit message>
pick b942f13 Modified config 
pick b73d792 tried pushing   
pick f20106e Added assets    
pick 408118f Added config    
pick <commit hash> <commit message>
pick <commit hash> <commit message>                                                                                                                     pick <commit hash> <commit message> 

basically change the word pick to squash or s to skip or remove those commits from the commit history the unwanted commits.

Again like @Blake Taylor said you can lose work etc

Upvotes: 0

Blake Taylor
Blake Taylor

Reputation: 9341

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

Upvotes: 28

Karthik Bose
Karthik Bose

Reputation: 36084

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.

Upvotes: 9

Kenneth Hoste
Kenneth Hoste

Reputation: 2971

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'.

Upvotes: -1

Related Questions