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?
How to Fix the error: failed to push some refs to Error in Git. We can fix the error: failed to push some refs to [remote repo] error in Git using the git pull origin [branch] or git pull --rebase origin [branch] commands. In most cases, the latter fixes the error.
A commit gets rejected and causes a failed to push some refs to error because the remote branch contains code that you do not have locally. What this means is that your local git repository is not compatible with the remote origin.
To push the branch or you can say to push the changes in the branch to the Github repo you have to run this command “git push origin <the branch name>” in our case the branch name is “main”. After pushing the changes the repo will look like and this is how you can push a branch to a remotely hosted GitHub repository.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With