-1

I am working with a team project. There is this branch "current_feature" made by other developer.

So I did git fetch origin current_feature and then git checkout current_feature.

With that I got both the remote and local "current_feature" in the same commit.

Now this branch is separated from the main branch. They had a common commit in the past, but then diverged.

The problem started the next day. I don't know how but the developer made some changes and now current_feature is rebased over main.

So the situation is something like

enter image description here

My question is what should I do to have the local and remote current_feature in the same commit? I am afraid git merge would not do because it is not a fast forward merge. and I am bit hesitant of rebase since the local current_feature is over another branch called other_branch. How can I have both local and remote current_feature in the same commit?

KansaiRobot
  • 7,564
  • 11
  • 71
  • 150

2 Answers2

1

If your diagram is correct, this is a textbook case showing exactly why it is wrong to rewrite the history of a shared branch. It makes life difficult for all other users of the repo.

"what should I do to have the local and remote current_feature in the same commit?"

There are two completely different interpretations of that question. You have two different things and you want to make them "the same" as one another. Which way round do you want the change? Which one is to be changed to match the other? In other words, which one is the "correct" one in your view? I will give two answers, depending which way you want to go:

  • To make current_feature identical to the remote origin/current_feature, move the local branch name:

    git switch -C current_feature origin/current_feature
    
  • To make the remote origin/current_feature identical to the local current_feature, push the local branch with force:

    git push -f origin current_feature:current_feature
    

Note that either way some commits will be lost.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Perhaps worth mentioning that the first method only moves the local branch name `current_feature` to the same commit at which the remote branch is, and then updates the working directory to match the re-seated branch's state. There's no Git wizardry involved, and it's a completely legit procedure. Other branches are left unchanged. – j6t May 16 '23 at 06:21
1

If you dont need the changes in your local current_branch and your purpose is to just sync the local current_branch to remote current_branch you can do a hard reset as below,

git fetch origin
git reset --hard origin/current_branch

But if you have any new changes in your local which are not there in you remote current_branch and you want to keep it, then rebase might be the better option as below,

git fetch origin
git rebase origin/current_branch

Another possible option here would be to create a new branch based on the remote current_branch and cherry pick the additional changes that you have if any in you local branch,

git fetch origin
git checkout -b new_current_branch origin/current_branch
git cherry-pick {commit hash of changes you want from your local current_branch}

NOTE: when you have changes in you local current_branch and you want to keep them with the rebase/cherry-pick method mentioned above, there are possibilities of conflicts in files, which will need to be resolved before pushing the code

Jaison
  • 113
  • 5