0

I was working on my project and pushed it successfully to the GitHub. Everything was going smoothly, that somehow I messed in the project and I deleted the local version and went to GitHub and download the whole project using the download icon.

After working on the project and making handful of changes, when I tried to push it to the original it showed me the following error.

To https://gitlab.rz.uni-bamberg.de/abc/def/bla.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://gitlab.rz.uni-bamberg.de/abc/def/bla.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

I followed the following sequence after some research which showed me the above error.

git init
git add . 
git commit -m "changes" 
git remote add origin https://gitlab.rz.uni-bamberg.de/abc/def/bla.git
git push -u origin master

Please help me out how can I upload this one now.

mxmissile
  • 11,464
  • 3
  • 53
  • 79
Shani Mughal
  • 105
  • 13
  • To avoid getting in this situation again in the future, use the `git clone` feature, rather than download button. – Mehdi Dec 10 '19 at 21:14
  • Does this answer your question? [git: updates were rejected because the remote contains work that you do not have locally](https://stackoverflow.com/questions/24357108/git-updates-were-rejected-because-the-remote-contains-work-that-you-do-not-have) – phd Dec 10 '19 at 22:28
  • https://stackoverflow.com/search?q=%5Bgit%5D+hint%3A+Updates+were+rejected+because+the+remote+contains+work+that+you+do – phd Dec 10 '19 at 22:29

1 Answers1

2

You are getting this error because as the error is trying to tell you: "remote branch has commits that your local branch does not have".

And based on the commands that you show, right now you also have local commits that the remote does not have. If you do git pull --rebase, that will sort your local branch for you, then the push will work fine.

If you get merge conflicts, that means the remote commits edited the same file/line that your local commits did, and you will have to resolve those conflicts OR undo all your work -> then pull -> then redo your work.

Khalil Khalaf
  • 9,259
  • 11
  • 62
  • 104
  • Having local commits that origin does not have is normal. The error message is explicit : Updates were rejected because the remote contains work that you do not have locally. – Poutrathor Dec 10 '19 at 21:03
  • @Poutrathor what would you recommend? – Shani Mughal Dec 10 '19 at 21:05
  • 1
    `git pull --rebase origin master` will fetch the work upstream that you do not have, and rebuild your commits on top of it, and should resolve the issue. The advice is sound, just the words are mixed up. Instead it should read: "The remote has commits your local does not" – Max Friederichs Dec 10 '19 at 21:07
  • @Poutrathor It is the case: local has commits that the remote does not have, **and** remote has commits that local does not have. My answer is the fix. – Khalil Khalaf Dec 10 '19 at 21:07
  • He is getting this error *only* because of origin has commits unknown to local. It's important, IMHO, to make the distinction. Maybe the current answer could be rephrased? – Poutrathor Dec 10 '19 at 21:13
  • @MaxFriederichs this has resulted in a lot of merge conflicts – Shani Mughal Dec 10 '19 at 21:17
  • @Poutrathor Done. – Khalil Khalaf Dec 10 '19 at 22:32
  • @MuhammadUsman I can imagine, which is why you see the error in the first place. You will need to resolve these, and push. – Max Friederichs Dec 16 '19 at 21:34