1

I have 2 branches with commits.

  • Branch A and its commits is clean and was pushed to remote
  • Branch B has commits from Branch A, but was pushed to remote as well

Branch B looks something like:

  1. commit from Branch B
  2. commit from Branch B
  3. commit from Branch A
  4. commit from Branch A
  5. commit from Branch A
  6. commit from Branch A
  7. commit from Branch A

This happened because of a wrong rebase..

Before I make a merge request for Branch B I would like to fix it and remove all commits from Branch A.

What is the best way of doing this?

paswes
  • 51
  • 2
  • 10
  • Do you mean that commit 7 is the newest and 1 is the oldest commit? – mkrieger1 Dec 23 '21 at 21:06
  • What should branch B look like instead? – mkrieger1 Dec 23 '21 at 21:06
  • I would like to remove commit 3-7 without changes in my code – paswes Dec 23 '21 at 21:13
  • Does this answer your question? [How do you roll back (reset) a Git repository to a particular commit?](https://stackoverflow.com/questions/1616957/how-do-you-roll-back-reset-a-git-repository-to-a-particular-commit) --> `git checkout ` and then `git reset --hard <2>` where you insert branch name `` and commit hash of commit `<2>`. – mkrieger1 Dec 23 '21 at 23:26

1 Answers1

2

You can use git rebase -i HEAD~n, Here n is the number of commit you want see in the bash and change them.

Steps(In your case) :

  • git rebase -i HEAD~7
  • Then simply replace the pick word with drop for commit's you want to delete from branch B
  • Then save the bash file and exit.
  • git push --force-with-lease <remote> branch B OR git push -f <remote> branch B it will push your lastest changes (without branch A's commit)
Monish Khatri
  • 820
  • 1
  • 7
  • 24