Remove 4 entries from my git story

I made some commits and put them in my repository. Then I made a pull request, but I realized that there are some commits that 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 can I do it? Please note that I want to save those that are earlier than the ones I want to delete:

 Correct HTML ab1c41c HTML escaping 8b38955 
+8
git git-rebase
source share
3 answers

You can do an interactive rebase .

Assuming your head is in ab1c41c from your example, call rebase as follows

 git rebase -i HEAD~7 

This tells git that you want to manipulate the last 8 or so. You will be taken to your editor with a list of commits and some instructions.

Delete lines containing commits to delete, save, and exit. git will transform rebase and what it is.

Keep in mind that due to redirection, if you want to click on one branch, you need to pass the --force option.

Disclaimer Restoring and forcibly pressing can result in job loss or people being kicked out, so just make sure you understand what you are doing. :)

+12
source share

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

But this needs to be done before you push these commits to the public repository. See here . In your case, these commits are already available in a public repo. So, I do not suggest using rebase.

If you do not want these commits in your working directory.

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

    git checkout -b <branch name> 86f2509

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

    git cherry-pick 8b38955 ab1c41c

Now your working directory will not have these unwanted commits.

+3
source share

Just reset with --hard on commit just before:

 git reset --hard 86f2509 

Beware: you will lose those commits forever, you will not be able to return. Also, if someone has pulled in the commits that you want to get rid of, everything can become messy. In this case, you probably want to watch "git revert".

+1
source share

All Articles