0

I just went to github and created a new project, so far it only has a readme and gitignore files. But I have a project in local which I added to git using

git init
git add . 
git commit -m "initial commit"
git remote add origin https://github.com/user/repo.git

I would like to push my project using git push -u origin master but I get error message containing this

$ git push -u origin master
To https://github.com/user/project.com
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://github.com/user/project.com'
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 understand the logic behind this error, but what is the solution? I don't want to pull, as it might erase my local .gitignore file, is there anyway disregard this without force push?

user7342807
  • 323
  • 6
  • 21
  • 1
    you can git pull --rebase, and it will tell you if there are conflicts. – martin jakubik Mar 06 '17 at 10:21
  • the --force flag when pushing should do it – Matt Mar 06 '17 at 10:23
  • @martinjakubik doesn't --rebase erase some file likes? – user7342807 Mar 06 '17 at 10:23
  • Possible duplicate of [How do I properly force a Git push?](http://stackoverflow.com/questions/5509543/how-do-i-properly-force-a-git-push) – Matt Mar 06 '17 at 10:24
  • @Matt For this occasion, yes, but if there are other people that commit to the repo, it might reset their commit history – user7342807 Mar 06 '17 at 10:24
  • No, git pull will compare the remote to local and will first 1) try to merge automatically, then 2) if it finds conflicts, it will mark the conflicts and ask you to correct them. – martin jakubik Mar 06 '17 at 10:24
  • @martinjakubik As I SAID, `git pull --rebase` just deleted 9 files from my project, now thanks to you I have no idea how to recover these file and will likely spend a long time to make my site work again. Please refrain from giving similar answers if you don't have a clue of what people are asking – user7342807 Mar 06 '17 at 10:43
  • Let me try to help you recover them... can we move to chat – martin jakubik Mar 06 '17 at 10:46

1 Answers1

0

update: for some reason this suggestion deleted the user's files from their local filesystem.

I prefer to leave it up for now while I investigate... but I recommend you do NOT follow my answer.


This should work:

git pull (or I prefer git pull --rebase)

<manually resolve any conflicts by editing the text files>

git merge --continue (or git rebase --continue if you used rebase above)

git push
martin jakubik
  • 4,168
  • 5
  • 29
  • 42
  • Did `git pull --rebase` and it has been stuck for 4 minutes on `First, rewinding head to replay your work on top of it...` – user7342807 Mar 06 '17 at 10:31
  • are you behind a proxy? – martin jakubik Mar 06 '17 at 10:33
  • ok... weird. Looks like a network problem. At this point I would ctrl-C and try again. – martin jakubik Mar 06 '17 at 10:44
  • Thank god I did `ctrl-C` then, that's how I managed to save my entire project from being wiped out. I lost 8-9 files before stoping the script – user7342807 Mar 06 '17 at 10:46
  • chat? http://chat.stackoverflow.com/rooms/137327/how-to-push-new-project-in-git-while-disregarding-new-files-in-remote-like-read – martin jakubik Mar 06 '17 at 10:51
  • @user7342807 feel free to downvote this since it deleted some of your files. sorry about that. – martin jakubik Mar 06 '17 at 11:33
  • I think the issue was simple, although not sure (because I don't want to do it again) what I think happens when you do `git pull --rebase` is that, it will pull all files from remote and for some reason, delete your local file that are not in that remote... – user7342807 Mar 06 '17 at 11:41
  • That's exactly what git should NOT do... but it did. I just created a new repo to test this out and I'm going to post a new question. – martin jakubik Mar 06 '17 at 11:48
  • See http://stackoverflow.com/questions/42625566/what-would-make-git-delete-local-files-during-git-pull-rebase – martin jakubik Mar 06 '17 at 12:24