0

To set up a scene:

I am working on new feature and have branch X based on branch A. I have multiple commits in X. My coworker has done some changes which required force push to branch A. Now I want to be in a state where I have my coworkers branch A and my commits from branch X on top of it.

If I would now use git rebase on my coworkers newly-rewritten branch A, I would have a lot of conflicts from his history-rewriting.

To get in the state I want, I would usually just copy my branch X to backup Y, reset hard X to A and cherry pick my commits from Y to X again. (If I am not mistaken, I could do the same and instead of cherry pick use git rebase --onto).

tldr: Is it possible to do this with one command, without the branch copies? What I basically want is
"reset this branch to remote and apply current last X commits on top of it"

1 Answers1

0

You are not mistaken. Assuming you have your branch X checked out and are up to date with git fetch, then the single command is what you suspected (because rebase does do a reset first):

git rebase --onto origin/A A

This assumes A represents your local copy of A before the force-pushed version which would then be represented by origin/A. (If that's not the case, you can substitute the commit-IDs in their place.)

Note, whether you cherry-pick or rebase, you'll have the same potential for conflicts; there's no way to avoid that.

TTT
  • 22,611
  • 8
  • 63
  • 69