This question has been answered from another perspective here. But my question is about trying to understand why this problem arises in the first place. I have been running into this issue at work and the suggested solution is not overly satisfying as it doesn't really address the issue itself and comes with the danger of losing commits.
Below you'll find the shortest sequence of git interaction I found to reproduce the error:
git clone git@github.com:joyofdata/test3.git
cd test3
echo "1" > m
git add .
git commit -m "m1"
git push origin master
git checkout -b feature
git push -u origin feature
echo "1" > f
git add .
git commit -m "f1"
git rebase master
git push origin feature
git checkout master
echo "2" >> m
git add .
git commit -m "m2"
git push origin master
git checkout feature
echo "2" >> f
git add .
git commit -m "f2"
git rebase master
git push origin feature (error - see next code box)
I simply version a file m in master, then a file f in feature, then I commit a change in master, then I commit a change in feature. Now before pushing my change to remote feature branch I want to rebase it on master.
This is the command and the error message of the last command in above list:
➜ test3 git:(feature) git push origin feature
To github.com:joyofdata/test3.git
! [rejected] feature -> feature (non-fast-forward)
error: failed to push some refs to 'git@github.com:joyofdata/test3.git'
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.
Now - I try to solve the issue by first pulling from remote feature (as suggested in the message), then I resolve the conflict, push to feature:
➜ test3 git:(feature) git pull origin feature
From github.com:joyofdata/test3
* branch feature -> FETCH_HEAD
Auto-merging f
CONFLICT (add/add): Merge conflict in f
Automatic merge failed; fix conflicts and then commit the result.
➜ test3 git:(feature) ✗ vim f
➜ test3 git:(feature) ✗ git add .
➜ test3 git:(feature) git commit -m "deconflicted"
[feature 3fb647e] deconflicted
➜ test3 git:(feature) git push origin
Counting objects: 5, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (5/5), 565 bytes | 565.00 KiB/s, done.
Total 5 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), done.
To github.com:joyofdata/test3.git
0f31f60..3fb647e feature -> feature
But - from now on - I'm going to run again and again and again into that error every time I rebase feature on master - again a merge conflict and again that error.
➜ test3 git:(feature) git rebase master
First, rewinding head to replay your work on top of it...
Applying: f1
Applying: f1
Using index info to reconstruct a base tree...
Falling back to patching base and 3-way merge...
No changes -- Patch already applied.
Applying: f2
➜ test3 git:(feature) git push origin
To github.com:joyofdata/test3.git
! [rejected] feature -> feature (non-fast-forward)
error: failed to push some refs to 'git@github.com:joyofdata/test3.git'
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.
I don't get it!
I mean - what I want to do is simply applying the following workflow:
- change in local feature branch
- stage and commit that change
- git rebase master
- git push origin feature
I simple terms - before pushing a commit to remote feature I want to rebase feature on master. If I merge master into feature (git pull origin master
or git merge master
if local master is up to date) then I don't run into that issue.
I hope this isn't too confusing but I don't know how to put it more simple and it's really annoying me.