158

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://git@coderepo.com:7999/repo/myproject.git
 ! [rejected]        feature/my_feature_branch -> feature/my_feature_branch (non-fast-forward)
error: failed to push some refs to 'ssh://git@coderepo.com: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.

allenski
  • 1,652
  • 4
  • 23
  • 39
Frankline
  • 40,277
  • 8
  • 44
  • 75
  • 1
    Related: [What's a “fast-forward” in Git?](http://stackoverflow.com/q/4684352/456814). –  May 18 '14 at 17:00

21 Answers21

121

It looks, that someone pushed new commits between your last git fetch and git push. In this case you need to repeat your steps and rebase my_feature_branch one more time.

git fetch
git rebase feature/my_feature_branch
git push origin feature/my_feature_branch

After the git fetch I recommend to examine situation with gitk --all.

Haralan Dobrev
  • 7,617
  • 2
  • 48
  • 66
Boris Brodski
  • 8,425
  • 4
  • 40
  • 55
67

Warning: you can potentially loose work by using this solution. Especially in a team environment since it can potentially overwrite / delete other people's contributions. Look at this other answer for a better way: https://stackoverflow.com/a/20467414/39648

try this command

$ git push -f -u origin <name of branch>

i.e $ git push -f -u origin master

Sandeep Datta
  • 28,607
  • 15
  • 70
  • 90
ivever timothy
  • 689
  • 5
  • 2
  • 21
    That worked for my case when the others didn't. Sometimes you just have to tell git -f -u – gcr Aug 01 '20 at 20:13
  • 2
    this erased the previous commit and replaced it with my local changes. I had a backup of the previous commit, so not a big deal, but still not what I had expected: keep the previous commit and add a new commit with my local changes. – user1255933 Aug 06 '22 at 00:53
38

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
Engineer
  • 47,849
  • 12
  • 88
  • 91
28

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
Aurelio A
  • 1,767
  • 1
  • 10
  • 4
  • 13
    This screwed me, I pushed stuff directly to master and pushed the deploy from an entire day... and everyone is pissed.... AWESOME TIPS! – Mike Q Feb 08 '17 at 20:46
  • 9
    Probably want to explain what you're doing in summary before giving someone a dangerous tool. – Mirv - Matt May 10 '17 at 11:09
19

I'm late to the party but I found some useful instructions in github help page and 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
Bilal
  • 2,883
  • 5
  • 37
  • 60
14

I had a similar problem and I resolved it with: git pull origin

GChuf
  • 1,135
  • 1
  • 17
  • 28
William Rossier
  • 873
  • 9
  • 15
4

after you get the non fast forward error , just do below :

1> git pull --rebase origin <name-of-the-remote-branch>

This will fetch the remote changes in to your local branch . On top of that it will apply your local commits .

2> git push origin <name-of-the-remote-branch>

This will apply your local changes in the local copy of the remote branch to the actual remote branch repo in git

Som
  • 4,618
  • 4
  • 29
  • 32
3

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:

Checked option for: Push changes immediately to origin

...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.

The GUI Solution

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.

STEP 1

Right-click on the commit right before yours to undo your locally committed changes and select Reset current branch to this commit like so:

Sourcetree window with right-clicked commit and selecting: Reset current branch to this commit

STEP 2

Once all the loading spinners disappear and Sourcetree is done loading the previous commit, at the top-left of window, click on Pull button...

Sourcetree window with with the Pull button highlighted

...then a dialog popup will appear, and click the OK button at bottom-right:

Sourcetree window dialog popup with the OK button highlighted

STEP 3

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:

Sourcetree window showing the error hint: Updates were rejected because the tip of your current branch is behind

...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:

Sourcetree window with Stash button highlighted and dialog popup showing input to name your stash with OK button highlighted

...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:

Sourcetree window with the Stashes section expanded and changes right-clicked with Apply Stash highlighted

Sourcetree dialog popup ask your to confirm if you would like to apply stash you your local copy

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:

enter image description here

Then back to Sourcetree, click on the Commit button at top:

enter image description here

then right-click on the conflicted file(s), and under Resolve Conflicts select the Mark Resolved option:

enter image description here

STEP 4

Finally!!! We are now able to commit our file(s), also checkmark the Push changes immediately to origin option before clicking the Commit button:

enter image description here

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.

allenski
  • 1,652
  • 4
  • 23
  • 39
1

Write lock on shared local repository

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.

Boris Zinchenko
  • 2,142
  • 1
  • 24
  • 34
1

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
Mike Q
  • 6,716
  • 5
  • 55
  • 62
1
  1. move the code to a new branch - git branch -b tmp_branchyouwantmergedin
  2. change to the branch you want to merge to - git checkout mycoolbranch
  3. reset the branch you want to merge to - git branch reset --hard HEAD
  4. merge the tmp branch into the desired branch - git branch merge tmp_branchyouwantmergedin
  5. push to origin
richard
  • 12,263
  • 23
  • 95
  • 151
1

This problem comes when you have commit some changes in your GitHub repository using https://github.com/ which are not pulled into your local repository

To solve this problem -

  1. First pull your repository from GitHub and update commit or changes in your local repository
  2. Then push your commits to GitHub repository

You can follow below steps:

git pull <git repo HTTPS LINK>
git commit -m "updates and bugs fixed"
git push
desertnaut
  • 57,590
  • 26
  • 140
  • 166
1
git checkout master

git fetch [the temporal branch of the company before pushing to master]

git pull --rebase [the temporal branch of the company before pushing to master] master

git checkout -b [new-branch]

Then add your files and do the following:

git add .

git commit -m "added article"

git push -u origin [new-branch]

This worked for me.

smci
  • 32,567
  • 20
  • 113
  • 146
King Chudi
  • 61
  • 2
1
  • The git pull --rebase command performs a rebase instead of a merge when incorporating the remote changes into your local branch. This can help in situations where there are conflicts that need to be resolved before pushing your changes.
$ git pull --rebase origin main
  • git push command to push your local branch to the remote repository.
$ git push -u origin main
Jorge Luis
  • 813
  • 6
  • 21
0

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.

MansoorShaikh
  • 913
  • 1
  • 6
  • 19
0

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

you can fix this by:

git pull origin YOUR_BRANCH_NAME
Nikhith sunil
  • 229
  • 3
  • 4
0

Only this worked for me:

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

Follow this for more insights

Dani Banai
  • 1,080
  • 4
  • 14
  • 33
0

I was getting this

* branch            main       -> FETCH_HEAD
fatal: refusing to merge unrelated histories

What worked for me was.

git remote add origin repositry-web-url
git branch -M main
git pull origin main --allow-unrelated-histories

git push -u origin main
Shah Fahad
  • 61
  • 3
0

This happens if you have not pulled the latest from the repository. I solved this with the below steps:

git pull origin <branch_name>
git push origin <branch_name>
desertnaut
  • 57,590
  • 26
  • 140
  • 166
-1

Here is another solution to resolve this issue

git pull
git commit -m "any meaning full message"
git push
desertnaut
  • 57,590
  • 26
  • 140
  • 166
Sheo Dayal Singh
  • 1,591
  • 19
  • 11
-3
  1. Undo the local commit. This will just undo the commit and preserves the changes in working copy
git reset --soft HEAD~1
  1. Pull the latest changes
git pull
  1. Now you can commit your changes on top of latest code
alk453
  • 125
  • 1
  • 7