0

Please, before closing as duplicated question, take in account I am trying to follow some recommendations found around.

I have two branches and I don't want to create a new branch at all: master and develop in my company. I don't want to revert because it will keep the history as far as I understand (git reset --soft mylast-good-commit). I really don't want to touch on master. Lets say my last two commits in develop I did something very ugly and I would like to delete them completetely from github.

My last commit: 50021514 (I want to delete from github including the history)

My second last commit: 0b63ca64 (I want to delete from github including the history)

My third commit: 571ebdfa (I want this to be the head from now on)

Tentatives:

1)

C:\dev\my-app>git rebase -p --onto 571ebdfa

result in

pick 0b63ca6 bower dependency versions reseted  to fd042953
pick 4cce2c4 revert "bower dependency versions reseted"

# Rebase 571ebdf..4cce2c4 onto 571ebdf (2 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#

then I typed :wq and I got

interactive rebase in progress; onto 571ebdf
Last command done (1 command done):
   pick 0b63ca6 bower dependency versions reseted  to fd042953
Next command to do (1 remaining command):
   pick 4cce2c4 revert "bower dependency versions reseted"
You are currently rebasing branch 'MyBranch' on '571ebdf'.

nothing to commit, working tree clean
The previous cherry-pick is now empty, possibly due to conflict resolution.
If you wish to commit it anyway, use:

    git commit --allow-empty

If you wish to skip this commit, use:

    git reset

Then "git cherry-pick --continue" will resume cherry-picking
the remaining commits.
Could not apply 0b63ca6... bower dependency versions reseted  to fd042953

But I still see all alst two commits in github. Am I missing some extra step here or did I understand wrong that it is possible to use "rebase -p onto" to reach what I want?

2) git rebase -i 571ebdfa

pick 0b63ca6 bower dependency versions reseted  to fd042953
pick 4cce2c4 revert "bower dependency versions reseted"

# Rebase 571ebdf..4cce2c4 onto 571ebdf (2 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
#
# These lines can be re-ordered; they are executed from top to bottom.
#
/c/dev/my-app/.git/rebase-merge/git-rebase-todo [unix] (10:23 14/06/2018)                    1,1 Top"C:/dev/my-app/.git/rebase-merge/git-rebase-todo" [unix] 21L, 755C

then I tried :wq

and I get

Last command done (1 command done):
   pick 0b63ca6 bower dependency versions reseted  to fd042953
Next command to do (1 remaining command):
   pick 4cce2c4 revert "bower dependency versions reseted"
You are currently rebasing branch 'MyBranch' on '571ebdf'.

nothing to commit, working tree clean
The previous cherry-pick is now empty, possibly due to conflict resolution.
If you wish to commit it anyway, use:

    git commit --allow-empty

If you wish to skip this commit, use:

    git reset

Then "git cherry-pick --continue" will resume cherry-picking
the remaining commits.
Could not apply 0b63ca6... bower dependency versions reseted  to fd042953

Isn't rebase aimed to be used in this case: when we want to return to a previous commit not only in our local but in our remote repository as well?

3) git revert 0b63ca64 50021514

This reverts commit 0b63ca6484cc9225c75b19ede27fe90e39e6d094.

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# On branch MyBranch
# Your branch is up to date with 'origin/MyBranch'.
#
# Last command done (1 command done):
#    pick 0b63ca6 bower dependency versions reseted  to fd042953
# Next command to do (1 remaining command):
#    pick 4cce2c4 revert "bower dependency versions reseted"
# You are currently rebasing branch 'MyBranch' on '571ebdf'.
#
/c/dev/my-app/.git/COMMIT_EDITMSG [unix] (10:34 14/06/2018)                                  1,1 Top"C:/dev/my-app/.git/COMMIT_EDITMSG" [unix] 20L, 738C

then I typed :wq

and I get

[MyBranch 9276513] Revert "bower dependency versions reseted"
 2 files changed, 2 insertions(+), 1 deletion(-)
error: commit 50021514f6484eecff50e088686ed6b2f2203d47 is a merge but no -m option was given.
fatal: revert failed

Well, from user perspective I just want to delete the last two commits from a specific branch deleting as well their history.

Enrico Campidoglio
  • 56,676
  • 12
  • 126
  • 154
Jim C
  • 3,957
  • 25
  • 85
  • 162
  • Commits are the history...? – evolutionxbox Jun 14 '18 at 08:47
  • My last three commits pushed to github are: 50021514 (today), 0b63ca64 (yesterday) and 571ebdfa (before yesterday). Today and yesterday I pushed very ugly changes. Now I want my branch in github be exactly as it was before yesterday, so no history from today and yesterday visible. – Jim C Jun 14 '18 at 09:26
  • @rkta I totaly desagree my question is the same. You probably didn't read what I tried and the result I got. – Jim C Jun 14 '18 at 11:59

2 Answers2

1

do this

git checkout develop

git checkout -b temp_branch

git reset --hard 571ebdfa #sha code from the commit where you want the head to point

git log #you should see all clean 

now do a force push to origin

git push -f origin develop

Check the logs in github as well.

Do remember you just pushed your temp_branch to develop, now you need to set your local/develop as well.

if everything looks fine then do

git branch -D develop

git fetch 

git checkout develop

git branch -D temp_branch
Rajesh V
  • 146
  • 4
  • so, the only way is really by first duplicating locally my branch, reset locally the duplicated and then override completely the original one with my duplicated reseted branch? Just to guarantee we are in same page, I mentioned in my question: I don't want to create a new branch at all. Well, if there is no other straighforward way, that is ok for me. But I would expected as answer someway to directly delete a range of commites if it is possible. – Jim C Jun 14 '18 at 09:57
  • 1
    Jim, you can do that in the same branch, I just wanted you to be on the safer side and I asked you to create a new branch. just do `git reset --hard 571ebdfa` `git push -f origin develop` – Rajesh V Jun 14 '18 at 10:25
1

My last commit: 50021514 (I want to delete from github including the history)

My second last commit: 0b63ca64 (I want to delete from github including the history)

My third commit: 571ebdfa (I want this to be the head from now on)

Just put this into perspective, you want this:

---571ebdfa--0b63ca64--50021514
                           ^
                        develop

to become:

---571ebdfa
       ^
    develop

You were on the right track with git rebase --onto, you were just missing the second argument:

git checkout develop
git rebase --onto 571ebdfa 0b63ca64

You can think of it as:

git rebase --onto <new-base> <old-base>

which will effectively remove commits 0b63ca64 and 50021514 from develop.

You can read a more detailed explanation of the different ways to use git rebase --onto in this answer.

Of course, since you're removing the latest commits in a branch, you can simply say:

git checkout develop
git reset --hard 571ebdfa 

At this point, you'll have to force push your rewritten develop branch to the upstream repository with:

git push -f origin develop

Or just:

git push -f

if your local develop branch is set up to track the remote origin/develop branch (which is likely the case).

Community
  • 1
  • 1
Enrico Campidoglio
  • 56,676
  • 12
  • 126
  • 154
  • My questions is already well answered by both. A short comment if it is possible, why when I do git rebase --onto 571ebdfa 0b63ca64 I get "It seems that there is already a rebase-merge directory ... rm -fr "C:/dev/ing-app-sf-bmid/.git/rebase-merge"? Why I could "git reset"but I was blocked to do "git rebase --onto"? Is there a significance difference between such two approaches? – Jim C Jun 14 '18 at 11:33
  • The error you're getting when you do `git rebase --onto` indicates that a previous rebase operation didn't finish correctly, leaving behind the `.git/rebase-merge` directory. As for your second question, there is a big difference between `git-rebase` and `git-reset`; simply put, [`git-reset`](https://git-scm.com/docs/git-reset) moves `HEAD` _directly_ to a specific commit as a single operation, while [`git-rebase`](https://git-scm.com/docs/git-rebase) applies a series of commits on top of a new commit _one at a time_. – Enrico Campidoglio Jun 18 '18 at 08:32