Failed to push some refs when pushing feature branch

13,767

Solution 1

Git says

hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull')
hint: before pushing again.

You might just need to use the rebase workflow to pull rebase from the remote branch after you have made the commit, then push to remote.

git commit -m 'add another date again'
git pull --rebase

This may lead to rebase conflicts which you would have to solve and then continue rebase. This is primarily cause the version of tree in the sprint_branch1 is behind the feature_branch.

You should make sure the right commits are going in as well. In a general sense when you are merging with the sprint_branch1, its better to do a

git checkout feature_branch
git merge sprint_branch1

This instead of rebase, since rebase rewrites your commits which could cause issues. After you merge if you just push it should work fine.

EDIT 1:

If you need to rebase and still avoid this error, you can use

git push --force origin feature_branch

However this is not recommended especially in distributed teams, as it will rewrite the remote branch with your local branch irrespective of any changes others might have pushed to it.

Solution 2

This is where you went wrong:

git rebase origin/sprint_branch1

You should not rebase published branches. This command should have been a

git merge origin/sprint_branch1

In general you should be careful with git rebase – there seems to be some kind of religion around it, even though it is a very dangerous tool.

How can you proceed?

  • If you are absolutely sure nobody else is going to touch the feature branch again and nobody did any changes to it since your last pull, you can just do

    git push -f
    

    That will overwrite the HEAD on the server with your HEAD.

  • If you are sure that there were no changes since your last pull, but other people use your branch, you can do the above and tell everybody with a copy of your branch that they need to run

    git fetch origin
    git checkout feature_branch
    git reset --hard origin/feature_branch
    

    That will erase all their local changes since their last push though.

  • The safest way would be for you to rename your local feature_branch to somethengi else, find the commits you added, branch of the current origin/feature_branch and cherry-pick all your changes.

Run gitk feature_branch origin/feature_branch to get an understanding of what is going on.

Share:
13,767
Hurve
Author by

Hurve

Updated on July 05, 2022

Comments

  • Hurve
    Hurve almost 2 years

    What can I do to avoid getting the following message when I push a feature branch a second time:

    To https://github.com/xxx/git_test.git
    ! [rejected]        feature_branch -> feature_branch (non-fast-forward)
    error: failed to push some refs to 'https://github.com/xxx/git_test.git'
    hint: Updates were rejected because the tip of your current branch is behind
    hint: its remote counterpart. Merge the remote changes (e.g. 'git pull')
    hint: before pushing again.
    hint: See the 'Note about fast-forwards' in 'git push --help' for details.
    

    What I do is this:

    git pull origin sprint_branch1
    git checkout -b feature_branch
    
    date > a.txt
    git add a.txt 
    git commit -m 'added date'
    
    git push origin feature_branch
    

    Somebody do a code review for my feature and somebody else do changes to the sprint_branch in the mean time:

    git checkout sprint_branch1
    date > a.txt 
    git add a.txt 
    git commit -m 'added another date'
    git push origin sprint_branch1
    

    I need to improve my feature so I do

    git checkout feature_branch
    git fetch origin
    git rebase origin/sprint_branch1
    

    I get merge conflicts and do:

    nano a.txt # removing inserted merge tags
    git add a.txt 
    git rebase --continue
    

    then I improve my feature

    date >> a.txt 
    git add a.txt 
    git commit -m 'add another date again'
    

    I like to push my feature_branch for a second review

    git push origin feature_branch
    

    However I get the error message mentioned at the top. Git recommend me to use git pull, but other people recommends me to use the rebase workflow. So what should I do to push the feature_branch? Should I create a new branch named feature_branch_v2 and push that? Do I manually need to remember what files to git add in that case or should I add everything (creating a messy commit)? Is there a better way to push without getting this error message?