Bogdan Popa
Bogdan Popa

Reputation: 1099

Remove several git commits from parent branch

I pushed some commits to a feature_branch and integrated the latest state of the parent branch staging. Then, pushed other few ones.

I did not use rebase and now my feature_branch's history looks like this:

feature_commit_Y

feature_commit_X

staging_commit_D

staging_commit_C

feature_commit_B

feature_commit_A

How can I get rid of those 2 staging commits so I can pull rebase and then push my future feature_commits to come?

Upvotes: 0

Views: 298

Answers (2)

Melebius
Melebius

Reputation: 6695

  1. Assuming both feature_commit_X and feature_commit_Y are single commits:

    git checkout feature_commit_B
    git cherry-pick feature_commit_X feature_commit_Y
    

    git cherry-pick copies a set of commits to the current branch.

  2. In any case:

    git rebase --onto feature_commit_B staging_commit_D feature_commit_Y
    

    This will “transplant” the commits starting with the one after staging_commit_D up to feature_commit_Y onto feature_commit_B. If you use a branch name in the place of feature_commit_Y, this branch will be updated.

Upvotes: 1

TheGeorgeous
TheGeorgeous

Reputation: 4061

Use

git rebase -i HEAD~4

The -i option will start an interactive rebase and HEAD~4 will include the last 4 commits for rebase. Commenting out the commits will take out the commits from the list of commits to be applied. You can then continue the rebase which will apply feature_commit_X and feature_commit_y

Upvotes: 1

Related Questions