0

I have a git repo and currently I'm working in the feature branch.

I have something like that:

dev: ---1--2--3--
                \
fb:              --f1

While I was working on my feature branch, more commits to dev happened, so I rebased on it and now sha of my commits have changed

dev: ---1--2--3--4--5----
                      \
fb:                    --f1(another)

So now I can't push it to the remote feature branch because SHAs are invalid. I can't push with force, cause it is prohibited by the server.

So, what is the correct workflow in such a case, what should I do?

Rahul
  • 1,330
  • 2
  • 13
  • 25

3 Answers3

1

My suggestion would be either merge remote/dev into local/fb or delete remote/fb then push local/fb with git history.

  • Undo rebase and merge remote/dev into local/fb.

    # undo rebase: reset your local/fb with remote/fb
    $ git checkout fb
    $ git reset --hard origin/fb
    
    # merge and push to remote/fb 
    $ git pull origin dev
    $ git push origin fb
    
  • Delete remote/fb branch, then push your local/fb with history.

    $ git push origin --delete fb      # delete remote/fb branch
    $ git push origin fb  
    
Sajib Khan
  • 18,145
  • 4
  • 50
  • 65
0

You can for example do one of:

  • delete the remote branch, then push your local branch
  • push your local branch to a newly named remote branch
  • undo the rebase and do a merge instead
  • convince the repository hoster to allow force-pushes, then force-push your local branch
Vampire
  • 31,050
  • 2
  • 58
  • 88
0

It depends on your overall workflow as intended by your company/team. It is however, possible that they haven't fully thought it out.

Preventing forced-push on all branches, and requiring feature branches to be rebased are contradictory. Both things can be reasonable parts of a workflow, but not the same workflow.

Two Options

If forced pushes to feature branches must be denied within your company, the consequence is that you can't rebase those feature branches. You will need to preform non-fastforward merges instead.

If your company expects you to rebase your feature branches, then they must make the repo accept forced-pushes to those feature branches. Having git deny forced pushes on only certain branches instead of all branches is trivially easy.

Sloppy workarounds

You can of course delete your feature branches every time you rebase, and create a new branch each time. This is very sloppy, and will results in a ridiculous number of unnecessary branches. Your company/team should think through their workflow strategy to avoid this sort of thing.

eddiemoya
  • 5,260
  • 1
  • 19
  • 31