0

So we have a develop branch which we base our feature branches from.

Two days ago someone pushed a bad commit to develop, but I based my feature branch of it and already have few commits pushed into it. They removed the unnecessary commit from the develop branch but it still stays in my feature branch commits. It looks something like this:

   bad commit
       \
o---o---X develop
         \
          X---o---o---o feature
           \
        bad commit

What I need to do is to remove the commit that I based my branch of and instead rebase to the commit before it. Do i use git rebase --i or git rebase --onto, I'm not really sure. Thank you in advance!

MindPhuq
  • 87
  • 1
  • 10
  • You will need to `git rebase` your banch on the updated `develop` branch. When presented with the pick list you will need to delete the bad commit. – larsks Oct 17 '22 at 19:57
  • 1
    Either `git rebase -i` or `git rebase --onto` can do the job; so can a plain `git rebase` when using the fork point mode. There's no single right answer: just use one that works. – torek Oct 17 '22 at 20:29

1 Answers1

1

To remove a single commit with commit ID (hash) X from branch feature:

git rebase X feature --onto X~1

Side Note: what just happened to you is the reason force pushing a shared branch is frowned upon. (Someone force pushed develop to create this issue.) The usual course of action when you have a bad commit on a shared branch is to revert that commit instead of rewriting and force pushing. Had it been reverted, you could have updated your branch the same way you always do, perhaps:

git fetch
# then either:
git rebase origin/develop
# or
git merge origin/develop
TTT
  • 22,611
  • 8
  • 63
  • 69
  • That is the answer, thanks! And your side note was the exact case what happened. Just to add to your answer - after runing the rebase a force push should be done - ``git push origin feature -f`` – MindPhuq Oct 18 '22 at 10:38
  • @MindPhuq Yes, you must force push after rebasing an already pushed branch. Btw, when force pushing, I (generally) would suggest the safer options of `--force-with-lease` or `--force-if-includes`, or [both](https://stackoverflow.com/q/65837109/184546). – TTT Oct 18 '22 at 14:10