I am fairly new to git
, yet currently using it to manage our code in a team environment. I had some rebasing issues, and I fixed them using:
git checkout --ours filename.txt
git add filename.txt
git rebase --continue
Now I wish to push my changes, and so running the following command:
$ git push origin feature/my_feature_branch
Gave me the following error:
To ssh://[email protected]:7999/repo/myproject.git
! [rejected] feature/my_feature_branch -> feature/my_feature_branch (non-fast-forward)
error: failed to push some refs to 'ssh://[email protected]:7999/repo/myproject.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 can I do to get rid of this error?
Note: I am avoiding the use of --force
option as much as possible.
I had a similar problem and I resolved it with:
git pull origin
Here is another solution to resolve this issue
>git pull
>git commit -m "any meaning full message"
>git push
try this command
$ git push -f -u origin <name of branch>
i.e $ git push -f -u origin master
In Eclipse do the following:
GIT Repositories > Remotes > Origin > Right click and say fetch
GIT Repositories > Remote Tracking > Select your branch and say merge
Go to project, right click on your file and say Fetch from upstream.
In my case for exact same error, I was not the only developer as well.
So I went to commit & push my changes at same time, seen at bottom of the Commit
dialog popup:
...but I made the huge mistake of forgetting to hit the Fetch
button to see if I have latest, which I did not.
The commit successfully executed, however not the push, but instead gives the same mentioned error; ...even though other developers didn't alter same files as me, I cannot pull latest as same error is presented.
Most of the time I prefer sticking with Sourcetree's GUI (Graphical User Interface). This solution might not be ideal, however this is what got things going again for me without worrying that I may lose my changes or compromise more recent updates from other developers.
Right-click on the commit right before yours to undo your locally committed changes and select Reset current branch to this commit
like so:
Once all the loading spinners disappear and Sourcetree is done loading the previous commit, at the top-left of window, click on Pull
button...
...then a dialog popup will appear, and click the OK
button at bottom-right:
After pulling latest, if you do not get any errors, skip to STEP 4 (next step below). Otherwise if you discover any merge conflicts at this point, like I did with my Web.config
file:
...then click on the Stash
button at the top, a dialog popup will appear and you will need to write a Descriptive-name-of-your-changes, then click the OK
button:
...once Sourcetree is done stashing your altered file(s), repeat actions in STEP 2 (previous step above), and then your local files will have latest changes. Now your changes can be reapplied by opening your STASHES
seen at bottom of Sourcetree left column, use the arrow to expand your stashes, then right-click to choose Apply Stash 'Descriptive-name-of-your-changes'
, and after select OK
button in dialog popup that appears:
IF you have any Merge Conflict(s) right now, go to your preferred text-editor, like Visual Studio Code, and in the affected files select the Accept Incoming Change
link, then save:
Then back to Sourcetree, click on the Commit
button at top:
then right-click on the conflicted file(s), and under Resolve Conflicts
select the Mark Resolved
option:
Finally!!! We are now able to commit our file(s), also checkmark the Push changes immediately to origin
option before clicking the Commit
button:
P.S. while writing this, a commit was submitted by another developer right before I got to commit, so had to pretty much repeat steps.
Probably you did not fetch the remote changes before the rebase or someone pushed new changes (while you were rebasing and trying to push). Try these steps:
#fetching remote 'feature/my_feature_branch' branch to the 'tmp' local branch
git fetch origin feature/my_feature_branch:tmp
#rebasing on local 'tmp' branch
git rebase tmp
#pushing local changes to the remote
git push origin HEAD:feature/my_feature_branch
#removing temporary created 'tmp' branch
git branch -D tmp
git reset --soft HEAD~1
git pull
Well I used the advice here and it screwed me as it merged my local code directly to master. .... so take it all with a grain of salt. My coworker said the following helped resolve the issue, needed to repoint my branch.
git branch --set-upstream-to=origin/feature/my-current-branch feature/my-current-branch
I had this problem! I tried: git fetch + git merge, but dont resolved! I tried: git pull, and also dont resolved
Then I tried this and resolved my problem (is similar of answer of Engineer):
git fetch origin master:tmp
git rebase tmp
git push origin HEAD:master
git branch -D tmp
I'm late to the party but I found some useful instructions in github help page and I wanted to share them here.
Sometimes, Git can't make your change to a remote repository without losing commits. When this happens, your push is refused.
If another person has pushed to the same branch as you, Git won't be able to push your changes:
$ git push origin master
To https://github.com/USERNAME/REPOSITORY.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/USERNAME/REPOSITORY.git'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes (e.g. 'git pull') before pushing again. See the
'Note about fast-forwards' section of 'git push --help' for details.
You can fix this by fetching and merging the changes made on the remote branch with the changes that you have made locally:
$ git fetch origin
# Fetches updates made to an online repository
$ git merge origin YOUR_BRANCH_NAME
# Merges updates made online with your local work
Or, you can simply use git pull
to perform both commands at once:
$ git pull origin YOUR_BRANCH_NAME
# Grabs online updates and merges them with your local work
I had this problem and none of above advises helped me. I was able to fetch everything correctly. But push always failed. It was a local repository located on windows directory with several clients working with it through VMWare shared folder driver. It appeared that one of the systems locked Git repository for writing. After stopping relevant VMWare system, which caused the lock everything repaired immediately. It was almost impossible to figure out, which system causes the error, so I had to stop them one by one until succeeded.
Source: Stackoverflow.com