0

I was just starting a new android studio project. Executed

git init
git add .
git commit -m "First Commit
git remote add origin http://....

and successfully create a remote thread to push.
The GitHub repository of my project had already been created, and had been added 3 files including .gitignore license & README by default. Then I executed this:

git push origin master

That's when the err coming... I can't push as usual even after I executed

git pull origin master

They just claimed that there's another tip behind my branch, perhaps because the pull order added a new branch called origin/master and now I'm confused with it.
In fact, I tried push -f once but it didn't ends well because my default files in the GitHub repository were deleted by doing so.
Are there any solutions could let me both keep my repository files and push my commits successfully?

Vampire
  • 35,631
  • 4
  • 76
  • 102
Daniel Dai
  • 63
  • 1
  • 3
  • 8

1 Answers1

1

Sure, easy as pie:

git fetch origin
git checkout origin/master
git cherry-pick master
git branch -f master
git checkout master
git push -u origin master
Vampire
  • 35,631
  • 4
  • 76
  • 102
  • What does "cherry-pick" do? Seems like it doesn't work – Daniel Dai May 18 '17 at 18:56
  • After executed your code, seems like my local directory left nothing but those 3 default files – Daniel Dai May 18 '17 at 19:05
  • The cherry-pick takes the commit you tell it and applies it on top of your current commit, hence your "First Commit" that adds your files. If it "does not work", tell me the error message. – Vampire May 18 '17 at 20:46
  • Alright, I solved it by using --rebase and --unrelated-histories after git pull. Thanks anyway. – Daniel Dai May 19 '17 at 04:09