57

I'm trying to uploading my Android Project on github but I get this error when I try to push:

Push rejected: Push to origin/master was rejected

I made this steps:

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Cosimo Sguanci
  • 1,251
  • 2
  • 14
  • 25

16 Answers16

96

after push follow these,

VCS --> Git --> Branches

then select origin/master --> Rebase

after finish rebase successful message, you could able to push to remote directory.

starball
  • 20,030
  • 7
  • 43
  • 238
Narandhran Thangavel
  • 1,392
  • 1
  • 12
  • 20
  • 1
    This did the trick for me..I needed to add origin/master as a new branch first though, because I had no branches for some reason.. – Matan Dahan May 31 '17 at 15:23
  • 4
    I Don't know how that happened but this working for me. You saved my day. Thank You. – Mehul Solanki May 14 '18 at 09:36
  • 1
    don't now why this worked, but thanks. Could you explain it? – Saul_programa Aug 07 '18 at 05:03
  • This worked, thank God. Please explain? I will look up rebasing in meantime. – Jeff Padgett Nov 19 '18 at 21:40
  • 1
    It worked and this is a clean and efficient way to resolve the conflict and the overall issue. Thanks. – Danger Feb 05 '19 at 07:00
  • 1
    you're getting this error usually when your local has not been sync properly. So rebase is one of the solution to fix the sync problem. You can learn more about rebase from here (https://www.atlassian.com/git/tutorials/rewriting-history/git-rebase) @Saul_programa – Narandhran Thangavel Mar 25 '19 at 11:38
  • This worked for me also. I had no branches initially so I needed to create a master branch. – Ivan Z. Mar 16 '20 at 11:31
93

As suggested in the comments you should pull the changes first, if you are working on a new repository that could happened when you create your remote repo with a readme or a licence file. To pull the changes from the user interface you should use, VCS > Git > Pull then select the master branch (depends)

enter image description here

after that you can Git > Push without problem.

SamTh3D3v
  • 9,854
  • 3
  • 31
  • 47
  • 4
    this is not working still in android studio 2.3.1 !! it is giving me this error ..From https://bitbucket.org/UserName/ProjectName * branch master -> FETCH_HEAD = [up to date] master -> origin/master fatal: refusing to merge unrelated histories – kinsley kajiva May 01 '17 at 07:39
  • @Elhamer I don't see any option in that window. I'm using gitlab and private ripo. – Mahdi-Malv Aug 22 '17 at 16:51
  • 1
    I create a new repository on Bitbucket with a readme.md file. And when i push for the first time my project i receive and error that say to me that i need to pull (i think referred to readme file). But when i try to pull i receive an error. I solved by start a new repo without readme file...but for the next time how can i pull without problems if the readme file exist? – Dario Apr 28 '18 at 15:42
  • 1
    Fetch first to see remote branch – Qamar Jun 06 '18 at 05:19
  • git pull origin master --allow-unrelated-histories – João Aug 17 '19 at 22:25
  • Tray rebase, then push. This worked for me in Android Studio 3.5.3 – sreedwish k jagan Dec 29 '19 at 07:21
  • If you are on the first commit. Rebasing wrongly can remove the whole branch. MAKE SURE TO REBASE WITH EXTREME CAUTION. – hellowahab Dec 31 '19 at 09:43
16

Just go to the root directory of your project and follow below steps:

  • Right Click-> Git Bash Here

  • Type git init (For initializing git).

  • Type git add -A (Get all files in the staging area).

  • Type git commit -m "First Commit"(Commit Changes)

  • Type git remote add origin https://your_git_url.git (Your repo URL)

  • Type git push -u origin master(master - Branch name)

    OR

  • Type git push -f origin master(master - Branch name) (-f for Force push and make sure there are no changes in your branch to push code.)

You can also make use of Git GUI client:

  • Github Desktop for github users Download
  • GitKracken for Windows, Mac and Linux Download (Free for public repositories)
  • SourceTree for Windows and Mac Download
Community
  • 1
  • 1
Sumit Shukla
  • 4,116
  • 5
  • 38
  • 57
  • Thanks it works. And if one have *already* commit in IDE, then only have to `git push -f origin master` in git bash, that's more convenient and safe. Another notice is that do not override others' work, only do this for one's *own* repo. – Meow Cat 2012 Jan 06 '19 at 04:33
  • This is the answer! Thank you! – Kenan Begić Oct 14 '20 at 17:27
11

For Android Studio if your "Push" is rejected, go ahead and try this:

Try to Pull.(Origin/Master).Right after that:

VCS-->Git-->Branches..-->Remote Branches(origin/master)-->Rebase Current onto Selected

enter image description here

This will eradicate all your contradicting configurations.

7

Try this

This type of error was also coming in my project because i have create a new project and paste my old config and some classes file in new project.

I have pushed the code by creating new branch

Go to VCS->Git->Branches->New Branch

Community
  • 1
  • 1
Sunil
  • 3,785
  • 1
  • 32
  • 43
5

If none of the above solution works then you can check this out..

  1. Log in to the GitHub account and go the settings.
  2. In settings open Emails section and check whether it is public or private.
  3. If it is public then other solution will work out finely and if your email is private then declare it public.

Because of the private email you aren't able to push the files in the repository from your IDE. So, make it public

Another Solution.!!

The unversioned code is not versioned, the changes are committed but don't push. Following are the steps to make the files versioned.

  1. Open the project tab(must be in the left side toolbar, if not then press 'Alt + F1')
  2. At the top, there is a dropdown with 'Android', open the dropdown and select the 'Project'
  3. In the list shown, right click your project folder
  4. There must be a option of 'Git', select it
  5. Choose the 'Add' option. And there it is!. Now simply try to commit and push operation as usual.

**NOTE :**You've to add files everytime using the above steps to upload the files successfully.!!

Thank you folks :)

Programmer X
  • 53
  • 1
  • 4
  • 1
    Thanks! I had set my email as private on GitHub just yesterday and was wondering why my push was being rejected! Thanks a lot! – Yashovardhan99 Jun 25 '18 at 10:12
  • this should be among the top solution! thanks!!!stupid me i set my email private haha! yo thanks very much man! – Cousin Roy Jun 16 '20 at 02:44
1

I had this same problem. I finally tried it from the command line and and received this message:

    remote: error: GH007: Your push would publish a private email address.
    remote: You can make your email public or disable this protection by visiting: 
    remote: http://github.com/settings/emails

There you should see two checkboxes. You will need to uncheck one of them.

  • Keep my email address private
  • Block command line pushes that expose my email
tamon76
  • 9
  • 5
1

This is the case when you make a repository with a readme file default and so you can either pull first or make a new repository without a readme file and dont forget to change the repo URL. Hope it helps. Cheers.

1

I had the same problem, just solve it by doing this:-

Go to Github account settings

(1)

enter image description here

(2) enter image description here

M. H.
  • 960
  • 11
  • 13
0

I had the same issue when I Fork from my another github account and clone it in Android Studio, I changed some file and commit(success) and pushed and that is rejected.

The problem was, I have logged into GitHub in Android Studio with my other account, not the one I'm fork the project. Check out your's from:

File -> Setting -> Version Control -> GitHub

If account information is not match with the repo cloned account, change it.

Then you can Push without a problem.

Blasanka
  • 21,001
  • 12
  • 102
  • 104
0

We can get around this "limitation" by editing the .git/config on the destination server. Add the following to allow a git repository to be pushed to even if it is "checked out":

[receive]
denyCurrentBranch = warn

or

[receive]
denyCurrentBranch = false
Ali mohammadi
  • 1,839
  • 26
  • 27
0

If you are pushing for the first time, Delete the repository from git, Share the project again on Git from Android Studio 3

Rithik Sabhal
  • 59
  • 1
  • 10
0

When I encountered the same error in Android Studio; I just simply renamed my remote name and it fixed the error. Steps which I followed are:

  1. Right click on project name and go to Repository option.
  2. Then select Remotes.
  3. Then you will be asked to Define Remote by providing a name of the remote and URL of your GitHub repository. Just change the name according to your choice if 'origin' is already filled and then fill the URL of your GitHub repository.
vimuth
  • 5,064
  • 33
  • 79
  • 116
0

I am giving clear note here skip steps you already done..

first create new repository in github. (dont close the tab until work done)

then you have to check whether you are log into your github account in android studio. if not you can log in setting-->Version Control --> github.

Then go to VCS -> Import into Version Control.

now in android studio project tab change it from android to project .Android to Project.

step 3: then Right click on your project select git then +add. again Right click -> commit and again Right click -->git--> repository--> push. in the new window select origin ->master then push.. if you already do this part and get the error in here do this.(i think this is the first time you try to add your project to github and github repo is empty.)

goto terminal (bottom of the ide) remove origin using this command. git remote rm origin

now do step 3 again. after that you will asked to enter github repository url. whenever you add url make sure to add .git .at the end of the url.

like this. https://github.com//xylophoneApp.git

HeshanHH
  • 653
  • 7
  • 9
0

for anyone maybe Suffers with this issue like me, the entire problem was with executionHistory.bin file, and don't ask me why because I don't know at all.

just create commit without this file and push it, this was the solution for my issue.

Hope this help someone.

Amr Jyniat
  • 345
  • 5
  • 12
0

In my case initially, I had created a new repository with a Readme file.That's causing the issue. Next time when I create new repo without the Readme file, everything works fine

Tarun Anchala
  • 2,232
  • 16
  • 15