Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What am I doing wrong with my git rebase workflow?

My team is moving from a git merge workflow to a rebase workflow, and I'm not understanding what I'm doing wrong. I'm working largely from this blog post as a template. Ultimately, the desire is to be able to submit a github pull request with the commits from my branch that will have no merge conflicts with master. It'd be great if that pull request could only contain my commits (for code review purposes).

The scenario is as follows:

  1. I create a branch off of master for my feature
  2. Do some work...
  3. I rebase from master into my local branch with:

    git fetch
    git rebase origin/master
    
  4. I take care of any merge conflicts, then continue the rebasing with

    git mergetool
    git rebase --continue
    
  5. Since I'm the only person working on my branch, I want to be able to just push my current state with

    git push origin myBranch
    
  6. However, when I try to push, I get the following

    $ git push origin myBranch 
    To <github address>
     ! [rejected]        myBranch -> myBranch (non-fast-forward)
    error: failed to push some refs to <github address>
    hint: Updates were rejected because the tip of your current branch is behind
    hint: its remote counterpart. Integrate the remote changes (e.g.
    hint: 'git pull ...') before pushing again.
    hint: See the 'Note about fast-forwards' in 'git push --help' for details.
    
  7. If I try git rebase origin/myBranch I get many conflicts, because the commits that I've pulled from master are complaining about commits that existed in my branch from a previous rebase (but that I never personally changed).
like image 810
rarrarrarrr Avatar asked Feb 19 '15 20:02

rarrarrarrr


2 Answers

If you don't want to force push (push -f) your feature branch, you'll have to avoid rebasing your feature branch after you've already pushed it to GitHub.

I use this workflow without getting the error you mention in step 6.

  • git checkout master
  • git pull
  • git checkout -b feature_branch
  • git commit -m "Finish my work"
  • git checkout master
  • git pull
  • git checkout feature_branch
  • git rebase master
  • Resolve merge conflicts.
  • git push origin feature_branch
  • Open pull request.

As mipadi says in his answer, you'll have to force push your feature branch if you do the following while you have your feature branch checked out:

  • git push origin feature_branch
  • git rebase master
  • git push origin feature_branch # error
  • git push -f origin feature_branch

The last command — git push -f origin feature_branch — should not cause any problems if you're the only one working on that branch.

like image 108
Nicholas J. Matiasz Avatar answered Sep 19 '22 12:09

Nicholas J. Matiasz


You have to force the push, using git push -f. By default, git push only allows a fast-forward merge (that is, it only allows you to push a branch if the new commits are direct descendants of the commits already in the remote repo). git rebase rewrites history, which changes all the commits on the branch, so you have to force the push instead.

like image 44
mipadi Avatar answered Sep 22 '22 12:09

mipadi