Feature and origin/Feature are separated in a Y shape

29 views Asked by At

I was reading some code in a repository in which I have to work in a branch called Feature1. While doing it, another member has changed the structure of the branch so now that I have done git fetch origin Feature1 I have something like

image

The commits in Feature1 is similar to origin/Feature1 except that there is one commit in origin/Feature1 that is not present in the local Feature1 and some other reordering. Also in the end there are some additional commits.

I don't think a good idea is to merge them.

But I want to have the local Feature1 in origin/Feature1

What should I do?

1

There are 1 answers

0
TTT On

The reason this happened is someone else rewrote the branch and force pushed it. Fortunately this won't mess you up, since you haven't started working on it yet:

I was reading some code in a repository in which I have to work in a branch called Feature1.

Before you start, you need to fix up your local copy. There are 2 easy ways to do this (pick one):

  1. Reset your local copy of the branch:
# with the branch checked out
git reset --hard origin/Feature1

# or if your local branch is already tracking origin/Feature1
git reset --hard @{u} # this is shorthand for the remote branch you are tracking
  1. Delete your branch and check it out again
git switch --detach # you can't delete a branch you're currently on

# now delete the branch
git branch -d Feature1

# now check it out again, which will grab it from origin/Feature1
git switch Feature1

Once you've fixed up your branch, the next step is to coordinate with the other person (or people) working on that branch, and ask them if they plan on rewriting it again. If they will rewrite it, you could consider waiting until they're done, or, ask them to let you know when they rewrite so you can rebase your branch onto their new version. The way you do this is by using the --onto option in git rebase, as described in eftshift0's comment:

git fetch
git rebase <commit-where-you-started> Feature1 --onto origin/Feature1