0

I'm stuck in this code. After git fetch I met a wall and don't know what a next step should I make to get all my commited files to GitHub server - now there is only one file README.md that was automatically created when saving a new repository. Thanks for help.

Freeware Sys@DESKTOP-VK97GA5 MINGW64 ~/Desktop/Coding Staff/MustBeDone (master)
$ git status
On branch master
nothing to commit, working tree clean

Freeware Sys@DESKTOP-VK97GA5 MINGW64 ~/Desktop/Coding Staff/MustBeDone (master)
$ git remote add origin https://github.com/adambugaj/Lets-code.git

Freeware Sys@DESKTOP-VK97GA5 MINGW64 ~/Desktop/Coding Staff/MustBeDone (master)
$ git push -u origin master
To https://github.com/adambugaj/Lets-code.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://github.com/adambugaj/Lets-code.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.

Freeware Sys@DESKTOP-VK97GA5 MINGW64 ~/Desktop/Coding Staff/MustBeDone (master)
$ git fetch origin master
warning: no common commits
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From https://github.com/adambugaj/Lets-code
 * branch            master     -> FETCH_HEAD
 * [new branch]      master     -> origin/master

GitBash code

Adam
  • 33
  • 1
  • 8
  • Possible duplicate of [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 Oct 07 '17 at 14:43

2 Answers2

5

If you want to keep the readme file you can pull and rebase it

git pull --rebase origin master

Then push your code back

git push origin master

If you don't want the readme file just push it forcefully

git push origin master -f
Samuel Robert
  • 10,106
  • 7
  • 39
  • 60
  • So, all fault is made by readme file? I shouldn't have created this than. Thank you for a fast response and successful solution. – Adam Oct 07 '17 at 12:03
  • When you create a readme file while creating repository you're making a commit there. So your local repo and upstream repo will have conflict in commit histories. you have to make either of them same as the other one by force or by rebase – Samuel Robert Oct 07 '17 at 12:08
1

Looks like you only have the default README.md file which came with the creation of the github repo. I would say since this is your first commit and push to the repository just go ahead and do a force push

git push origin master -f

There is another easier way for working with fresh repositories. You can first do a git clone https://github.com/adambugaj/Lets-code.git into any local repository. Then place all your files in that directory (or start coding there instead), and then the git push origin master will work just fine.

After the initial clone, you can edit/remove the README.md file as you wish. (but it's always good to have a README.md file for your repo)

codePrady
  • 110
  • 7
  • Thanks for a good explanation. Would it be better, if I didn't create a readme file at the beginning, but after I do a first commit? – Adam Oct 07 '17 at 12:10
  • Oh you always have that option. _**"Initialize this repository with a README This will let you immediately clone the repository to your computer. Skip this step if you’re importing an existing repository."**_ This checkbox is there always. But with the same advice that I mentioned in my answer above (having the file will let you clone it immediately) – codePrady Oct 07 '17 at 12:47
  • Ok, so I will not check this next time. Thanks for the explanation. – Adam Oct 07 '17 at 13:27