I need to pop up and delete the "medium" commit in my main branch. How should I do it?

For example, in the following main branch, I need to delete only commit af5c7bf16e6f04321f966b4231371b21475bc4da, which is the second because of the previous reboot:

commit 60b413512e616997c8b929012cf9ca56bf5c9113 Author: Luca G. Soave <luca.soave@gmail.com> Date: Tue Apr 12 23:50:15 2011 +0200 add generic config/initializers/omniauth.example.rb commit af5c7bf16e6f04321f966b4231371b21475bc4da Author: Luca G. Soave <luca.soave@gmail.com> Date: Fri Apr 22 00:15:50 2011 +0200 show github user info if logged commit e6523efada4d75084e81971c4dc2aec621d45530 Author: Luca G. Soave <luca.soave@gmail.com> Date: Fri Apr 22 17:20:48 2011 +0200 add multiple .container at blueprint layout commit 414ceffc40ea4ac36ca68e6dd0a9ee97e73dee22 Author: Luca G. Soave <luca.soave@gmail.com> Date: Thu Apr 21 19:55:57 2011 +0200 add %h1 Fantastic Logo + .right for 'Sign in with Github' 

I need mantain

  • First fix 60b413512e616997c8b929012cf9ca56bf5c9113,
  • The third lock is e6523efada4d75084e81971c4dc2aec621d45530 and
  • Last commit 414ceffc40ea4ac36ca68e6dd0a9ee97e73dee22

"discard" only second commit af5c7bf16e6f04321f966b4231371b21475bc4da

How can i do this? thanks in advance Luke

+48
git git-rebase git-reset git-revert github
Apr 22 '11 at 15:48
source share
3 answers

Rebase or revert are parameters. Rebase will actually remove the commit from the story so that it looks like the second commit never existed. This will be a problem if you have moved the main branch to other repositories. If you try to press after reinstalling in this case, git will give you an error with an error not related to fast forward.

Revert is the right decision when the branch has been transferred to other repositories. git revert af5c7bf16 will create a new git revert af5c7bf16 that just changes the changes made by af5c7bf16. This way the history is not overwritten, you keep a clear record of the error, and other repositions will receive push.

Here's a good way to erase: git rebase -i <commit>^ This will take you to a commit just before the one you want to remove. The interactive editor will show you a list of all commits back to this point. You can choose squash, etc. In this case, delete the commit line you want to delete and save the file. Rebase will complete its work.

+63
Apr 22 '11 at 16:23
source share

If rebase is an option, you can reinstall and simply abandon it:

 $ git rebase -i 414ceffc^ 

If rebase is not an option, you can simply return it:

 $ git revert af5c7bf16 
+19
Apr 22 '11 at 16:11
source share

Despite all the credit source answers received here, I did not find them to satisfactorily answer the question. If you find yourself in a situation where you need to remove a commit or collection of commits from the middle of the story, this is what I suggest:

  • Create a new branch from the head containing all the commits, and switch to it.
  • Return the new branch back to the point from which you want to start a new base.
  • Then (here's the key point) cherry pick the subsequent commits that you actually want to apply after that from the original branch to the new one, and skip the commits that you no longer need (i.e. those that you delete).
  • If desired, rename the original branch to something that points to its old code, and then rename the new branch from which the original one was issued.
  • Finally, push your changes to the remote repo (if used). You will probably have to use a push. If your collaborators are having trouble reviewing, it might be easiest to just clone the repo again from a remote source. One way or another, you will most likely want to talk to them if you still vomit from the middle of your story!

Cherry Picking info: What does cherry picking do with git?

Here are some of them doing it with Tortoise git (as I just did). It is definitely easier to use the gui utility for these kinds of operations! Cherry Picks Using TortoiseGit

+5
Aug 03 '17 at 23:16
source share



All Articles