1

On question: How can I remove a commit on GitHub?

I can't to use same solution on my project.

I want to go to commit 30735f3d by command:

git rebase -i HEAD~2


*   823da1cf7976466da40a5720191d09b080e046d6 Merge branch 'hotfix#1-issue
|\  
| * 71b732ef9ecbf36153ab2729cc78bc522341399e fixed: changed salary type f
|/  
*   be87e784da99fbad8b575b5433bb33a35954064c Merge branch 'dev'
|\  
| *   90b23296a3a6c2883f2c434947da4896d8afef5c Merge branch 'issue#2240' 
| |\  
|/ /  
| * 947b43b12763acbd0ca2733f0fd88e3882c086ab issue#2240:create user class
|/  
*   30735f3d4884c4156a070f75dbc5ae3608f9c312 Merge branch 'dev'
|\  
| * 88b06701fc4a66511da3096b2383c8c26ea2e203 added field salaryDefault,bo
| * d364b0b381ed099e48b614955996696dbe39bba4 added entity start salary
|/  
*   4de63ced11d978e10f5c81cc23e743fbe06ea9cc Merge branch 'dev'
|\  
| * f16c849b25028a376dbdf03b5d0aaee0b3966d74 setup persistence
|/  
* cb88a650a26905d6a911706bda609162338c57ed init

after rebase some of above commits that remain e.g. fixed: changed salary type f, issue#2240:create user class

* 12f20a44e3ea7eaf34ca62fb6a7a51f5729bcba1 fixed: changed salary type from int t
* 947b43b12763acbd0ca2733f0fd88e3882c086ab issue#2240:create user class
*   30735f3d4884c4156a070f75dbc5ae3608f9c312 Merge branch 'dev'
|\  
| * 88b06701fc4a66511da3096b2383c8c26ea2e203 added field salaryDefault,bonus,tax
| * d364b0b381ed099e48b614955996696dbe39bba4 added entity start salary
|/  
*   4de63ced11d978e10f5c81cc23e743fbe06ea9cc Merge branch 'dev'
|\  
| * f16c849b25028a376dbdf03b5d0aaee0b3966d74 setup persistence
|/  
* cb88a650a26905d6a911706bda609162338c57ed init

But i want a result like this:

*   30735f3d4884c4156a070f75dbc5ae3608f9c312 Merge branch 'dev'
|\  
| * 88b06701fc4a66511da3096b2383c8c26ea2e203 added field salaryDefault,bonus,tax
| * d364b0b381ed099e48b614955996696dbe39bba4 added entity start salary
|/  
*   4de63ced11d978e10f5c81cc23e743fbe06ea9cc Merge branch 'dev'
|\  
| * f16c849b25028a376dbdf03b5d0aaee0b3966d74 setup persistence
|/  
* cb88a650a26905d6a911706bda609162338c57ed init

And now i have only branch master. Or that's about --no-ff or anything else?

Community
  • 1
  • 1
user1162226
  • 13,223
  • 3
  • 14
  • 8
  • 1
    `30735f` is only `HEAD~2` (the second "first" parent). Which commit do you actually want to remove? – knittl Feb 21 '12 at 20:52
  • OK i get it but when i use HEAD~2 some of top commits that remain e.g fixed: changed salary type f , issue#2240:create user class – user1162226 Feb 21 '12 at 20:57

2 Answers2

0

If you just want to change the commit that the branch points to you need to reset.

git reset --hard 30735f3d4884c4156a070f75dbc5ae3608f9c312

However, this will put you "behind" in history when compared to origin. As a result you can't simply push. You have to force push.

git push -f origin

The problem with this is this. If other developers have copies of this branch locally, they will each have to dot he following.

git fetch
git reset --hard origin/<branch>

This will get complicated if they have commits locally that they need to preserve, as the command above will mean that they will lose those. Its generally a bad idea to modify history that has already been shared with others.

Alternatively, if this is code that other developers already share with you, you may want to use `git-revert' to "undo" all those merge commits after the one want.

git revert 823da1cf7976466da40a5720191d09b080e046d6
git revert be87e784da99fbad8b575b5433bb33a35954064c

This will create two "revert" commits, which will undo the changes that those merge commits made. Making your history look like this.

*   ######################################## Revert be87e78 Merge branch 'dev'
*   ######################################## Revert 823da1c Merge branch 'hotfix#1-issue
*   823da1cf7976466da40a5720191d09b080e046d6 Merge branch 'hotfix#1-issue
|\  
| * 71b732ef9ecbf36153ab2729cc78bc522341399e fixed: changed salary type f
|/  
*   be87e784da99fbad8b575b5433bb33a35954064c Merge branch 'dev'
|\  
| *   90b23296a3a6c2883f2c434947da4896d8afef5c Merge branch 'issue#2240' 
| |\  
|/ /  
| * 947b43b12763acbd0ca2733f0fd88e3882c086ab issue#2240:create user class
|/  
*   30735f3d4884c4156a070f75dbc5ae3608f9c312 Merge branch 'dev'

This you can push, and everyone can pull - it will be the same state of code that existed in 30735f3d4884c4156a070f75dbc5ae3608f9c312.

Important

If you chose to revert merge commits, be aware of what is actually happening. If you want to re-introduce the changes from the reverted merges, you will need to revert the revert commits. Read this answer from me to a different question regarding reverting and merge commits.

Community
  • 1
  • 1
eddiemoya
  • 6,713
  • 1
  • 24
  • 34
0

If your example of "the result you want" is actually accurate, do git reset --hard 30735f3d4884c4156a070f75dbc5ae3608f9c312 and you're there.

Borealid
  • 95,191
  • 9
  • 106
  • 122