Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove 4 commits from my git history

Tags:

git

git-rebase

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
like image 848
Hommer Smith Avatar asked Jun 20 '12 05:06

Hommer Smith


People also ask

How do I remove multiple commits in git?

To remove the last commit from git, you can simply run git reset --hard HEAD^ If you are removing multiple commits from the top, you can run git reset --hard HEAD~2 to remove the last two commits. You can increase the number to remove even more commits.

Can you delete git commit history?

If you commit sensitive data, such as a password or SSH key into a Git repository, you can remove it from the history.


2 Answers

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

like image 160
Blake Taylor Avatar answered Sep 20 '22 14:09

Blake Taylor


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.

like image 24
Karthik Bose Avatar answered Sep 18 '22 14:09

Karthik Bose