Home > Back-end >  Git rebase when master has been changed
Git rebase when master has been changed

Time:03-17

I created a branch feat/add-access from master, made a single change. Then I tried to rebase to master, but I realized changes have been made to master, so I got:

Your branch and 'origin/feat/add-access' have diverged,
and have 1 and 1 different commits each, respectively.
  (use "git pull" to merge the remote branch into yours)

nothing to commit, working tree clean

Then I removed my changes with:

 git fetch origin
 git reset --hard origin/master
 HEAD is now at 9cc1c2f Jenkins Pipeline PR Merger

HEAD is now at 9cc1c2f Jenkins Pipeline PR Merger

Which is OK, because 9cc1c2f is the last commit on master. I also can see my changes has been removed.

Now I have:

project git:(feat/add-access) git status
On branch feat/add-access
Your branch and 'origin/feat/add-access' have diverged,
and have 2 and 1 different commits each, respectively.
  (use "git pull" to merge the remote branch into yours)

nothing to commit, working tree clean

Then I do:

➜  project git:(feat/add-access) git rebase master             
Successfully rebased and updated refs/heads/feat/add-access.

But I still get diverging branches:

project git:(feat/add-access) git status
    On branch feat/add-access
    Your branch and 'origin/feat/add-access' have diverged,
    and have 1 and 1 different commits each, respectively.
      (use "git pull" to merge the remote branch into yours)
    
    nothing to commit, working tree clean

git pull doesn't work.

fatal: Not possible to fast-forward, aborting. 

git log shows no issue. In local, I am in the desired state, a copy of master branch.

I could simply delete remote feature branch, and just push it again, but I have an open PR on it, it may affect it, so I won't do it.

What should I do ? I'm a bit newbie with rebase, and I don't want to mess up the master branch !

CodePudding user response:

You rebased your feature branch on master, so your local feature branch has diverged from the remote feature branch.

# remote
master ---- 9cc1c2f
           \
             feature 

# local
master ---- 9cc1c2f
                    \
                    feature 

Once you rebased your local feature branch on master, just git push -f to update the remote.

If someone else is working on the feature branch, communicate with your team so that you don't override other commits on the remote feature branch

CodePudding user response:

I think I could solve it, what I did was to rebase local feat branch changes into distant one with:

git rebase origin/feat/add-access feat/add-access
git push

and made a fixup to add my changes

  • Related