How to use GitHub with Android Studio
This article provides a basic overview of how to use GitHub with Android Studio.
Video Demonstration
Firstly, let’s login to github.com and create a new repository. From the top right corner of the screen, click the + sign and select New repository.
Now complete the form to create a new repository called testproject. Leave the Initialize this repository with a README unticked.
Next, open Android Studio and create a new project. Call your new application TestProject. Click Next to continue.
Leave the next page as default and click Next.
On the Add an activity to Mobile screen select Blank Activity and click Next.
In the next screen, leave the default activity name of MyActivity and click Finish.
Your new project will open in Android Studio. On the top menu, select VCS > Import into Version Control > Create Git Repository.
On the next screen, leave it all as default and click OK.
Now use Windows Explorer and navigate to the root of your projects folder. Right click and select Git Bash (If you do not see this option, then first install Git for Windows).
When the Git bash screen appears, type:
git remote add origin https://github.com/[username]/[project_name].git
An example of a Git repository URL is: https://github.com/markwint/testproject.git
Then press enter. The GitHub remote will be added to your Git repository.
Next, jump back into Android Studio, right click your projects root directory and select Git > Add. This will add all your project files to your Git repository.
It will seem like nothing has happened, but trust me, the project files are added.
Now right click the project name again and this time select Git > Commit Directory.
In the next screen, type a Commit Message and select Commit.
If a Code Analysis warning appears, click Commit. (Unless it’s a real project, in which case review and fix the issue before committing!)
Now, right click the project name, select Git > Repository > Push.
Check the box Push current branch to alternative branch and leave the branch name as master. Then select push.
Now enter your GitHub Login (email address) and Password. Then click OK.
If it’s all good, you will see this message.
Now your code is pushed to your GitHub repository. Don’t believe me? Logon and check for yourself.
And that’s it! I hope you found this post helpful? Let me know in the comments below.
Cheers,
Mark
Thank you for sharing this, it really helped me!
Great, I’m glad you found it useful. How are you finding working with Android Studio so far?
Android Studio is really horrible to work with. Eclipse was the way we were doing Android, but they deliberately broke Eclipse.
bro you responded 4 years later and if you have a decent computer android studio is definitively better than eclipse. especially in 2018. no clue what you’re going on about
I’m doing same but suddenly it asks for ssh key and then when i type passphrase it shows “failed”
Please help me !!
thank you sir
You’re welcome, thanks for reading!
Thank you.. it works
Glad you found it useful!
I may have entered a wrong password, it says “Push Rejected”. The enter password popup is not showing again. How can I re-enter my password?
Hi amd. Did you try to close Android Studio and re-open? This comment is a bit old so did you figure the solution already? If not, let me know.
Thank you. Just what I needed.
That’s brilliant, glad you found it helpful 🙂
Excellent article – thank you very much!
Happy you found it useful, thank you for reading!
Thank you so much.. it works!
Excellent!
I’m trying to do this with my own project and it keeps giving me “Push rejected: master: rejected”. Any idea why?
How did you fix this? I cant seem to get around this issue, even after restarting and setting up everything from scratch!
Having the same issue with push rejection and not sure how to proceed. Any suggestions? Thanks.
Nicely done. I had serious issues with rejections when I was trying from my home but I tried this tutorial again from my office with a different project and it worked flawlessly.
I have one question: My final layout ( the project tree structure, I mean ) looks slightly different from your screenshot at the end. Does that matter at all?
Glad it helped you. Not sure regarding the layout, can you send a screenshot to my email: mark (at) winterbottom.me…
Thanks sir,
Very helpful tutorial…
Very good tutorial with screenshots, I was stuck on “Nothing to push” message while pushing to github, you advice to Check the box Push current branch to alternative branch before pushing helped me a lot.
Thanks again.
Why do you have green Windows ?
I like green windows 🙂
Thanks, good work
Great article. I have a query, I wonder if you are aware of the answer to this. In the Android Studio “Commit Changes” option image in this blog the author field is empty, clicking on the dropdown shows my name and I select it every commit. Is there a way to set my name as default so that it automatically fills in when I goto VCS -> Commit Changes?
It’s really good.
Perfect 😉
that was awesome …point of correction not internet explorer its window explorer.Great job again and again now about to google on how to check out through git hub
Thanks man, I’ve fixed it now 🙂 (No one want’s to use Internet Explorer for anything haha)
It was sooooo helpful. Thank you!
Its really nice tutorial. It helped me a lot. Thanks for sharing. 🙂
Thanks!
Thank you. That was a perfect set of instructions and job done easy.
Android Studio keeps on saying “Push to origin/master Rejected” I have no idea why and I have to submit some Android code soon.. to a tutorial.. please help
Try and do pull first and then push?
Thanks for the tutorial, really helped me.
Hi,
Why I don’t have that option in my Android Studio?
“the check box Push current branch to alternative branch”
Thank you.
It’s helpful to me Thank You Mark
Thanks so much> This was very helpful!
Thank you so mach it’l very useful. i have one doubt how to create new branch and update project in my new branch via android studio.
Hi Rajesh. Did you figure it out in the end?
I got error when I push
Failed with error: fatal: unable to access ‘https://github.com/Harin-Kaklotar/MyProject.git/’: Failed to connect to 127.0.0.1 port 8087: Connection refused
Hi Harin. That is strange. It looks like github.com is resolving to 127.0.0.1 for you. In command prompt can you try and ping github.com and see what IP address it resolves?
Hi
I tried the steps you mentioned above and they were really helpful. I am facing a problem with Github 2 factor authentication, which does not allow me to login through android studio. Any idea how to resolve this ?
Thanks
Hi Shilpa. Great question. I just enabled Two Factor auth to test this myself. You are correct, Android Studio does not support GitHub Two Factor authentication. You can get round this pretty easily by using the GitHub private key authentication to clone your repository, and then open the repository in Android Studio.
Let me know if this works for you or if you need more details.
when I run my application by Android Virtual Devices (AVDs)
I have problem
there are Error message ” this AVD’s Configuration is Missing a kernal File ”
I am waiting the Replying from Your And thank you on your Subject
Hi Omar. That sounds like a problem with your Android Studio or Android SDK installation.
I would need more details to figure out exactly what the issue is. My suggestion would be to re-install Android Studio and the Android SDK completely.
Hello Mark,
Thanks for the post. I have been able to create the GitHub repository for the project present in the Andriod studio located in my local machine. Your blog is wonderful. I have one query into it. I would be requiring the outputs folder content to be pushed into the GitHub as well, so can you let me know how to do it, as when I push the code currently, the output folder is not going into the Github.
Hi Sayom, thanks for the comment and kind words about my blog. Did you try going to ‘VCS > Git > Add’ and ensuring the ‘output’ folder is added? If it doesn’t work, open your .gitignore folder and check that ‘outputs’ isn’t added to .gitignore.
Hi Mark,
thanks for the nice tutorial. It makes my work more easy
It was really helpful, thank you so much!
Happy to be of help!
It was really helpful,That was a perfect set of instructions and job done easy.
Thank you
Thanks for the feedback. Glad to be of service 🙂
After following all your steps Push commits shows that ‘master -> Empty repository’. Will you help in this, i am using Android Studio 1.5.1
Hi Nandha, are you still having this issue? Sounds like the files are not added before you commit?
Push Rejected like quite few other people here.
I had to rename branch from master to master2 and it worked
Nice! Works with AndroidStudio 2.0 as well!!
Only differenc is if you want to commit to different branch, leave the checkmark emty. It is has the oposit meaning in android 2.0.
Thank You! I have no experience with eather Android studio or Git started googleing and found your tutorial fiirst thing probably saved me a couple of DAYS!! 😀 😀
Hi Patrik, happy that you found it useful! Thanks for reading.
Thank you so much! It works.
The only problem is that my Github account has 2-step verification, so I encountered some trouble, fortunately I solved it.
whenever i am writing in the shell program it says fatal:not a git repository
Hi Sam, it sounds like the git repository hasn’t been initialized? Did you click “Create Git Repository”?
Nice video! is there a follow-up to it? maybe something explaining how a different user then checks out the repository?
Hi David, actually we are working on a follow up right now 🙂
Thanks !! Made my life easier.
Thanks Raman 🙂
Thanks. Very good guide
Thanks Abish 🙂
Thank you sir , it’s really helpful tutorial sir.
You’re welcome!
That was awesome …point of correction not internet explorer its window explorer.Great job again and again now about to google on how to check out through git hub.It is useful information that i need this..
Thanks for sharing this information to all……
oh my god ,such a good tutorial,though I have config more than you mentioned but anyway it worked ,thank u so much ,sir
thats not the default, its where ever you were last, so i add drawable. change the instructions to ensure pointing to the right place
Thanks a lot mark, a great direction for the newbi user of android studio who want to switch from eclipse.
Thanks once again
Seemed to be working fine but on the final step (pushing the files) I get an error message that says:
Push failed: Failed with error: fatal: repository ‘https://github.com/silverben10/henrysmith.git/’ not found
Please would you be able to help?
Hi Ben, thanks for the comment. Can you try removing the trailing slash from the end?
thanks man. was helpful
Thank you! Super helpful 🙂
Thanks for the post. It was great. (Cheers)
i am getting error of this type
dheerendra@DHEERU MINGW64 ~
$ git remote add origin https://github.com/lobheshdhakar/Ki.git
fatal: Not a git repository (or any of the parent directories): .git
Sounds like maybe you need to initialise the project as a Git repo within the VCS menu at the top of Android Studio?
Thanks,It’s very helpfull
Man ,can you tell me how to use git from two android studio to work on a same project and make changes from two systems.what all steps are there for doing that.
Hi Pritz, assuming you already have the project on one computer, you would push to GitHub using the instructions on this post, and then on the second computer you would clone it. Then when you make a change on either one, you push the changes and then on the other machine you pull the changes. Does that make sense?
Yeah man ,i am convinced with your answer but i want more deep information about this thing…
As we are working on the firebase app so.. in order to fully access the codes privilege from the 2 devices(me and my team partner).. what are the things we need to set up..
pushing ins’t a success cause i get the error ” Failed with error: fatal: unable to access ‘https://github.com/darkfly0010/Darknote.git/’: Unknown SSL protocol error in connection to github.com:443 ” .please help
Do you know how how to get ssh key to gain access in github
Thanks! That was really healpgul!
Great work marks. Thanks for this worthy information.
If I supply a git ignore file will that be recognized by android studio and the defined files will not be uploaded to git ?
It will be recognised by Git which Android Studio will use so the answer is yes. Although note that if you have already added the files to Git before you set the .gitignore then you will need to remove them from git manually.
Not sure what happened, but I followed the steps and ended up having several errors in my code whereas before the code was fine.
Thank you so much.. it works! 🙂 🙂 🙂
Hey Mark, I needed to execute ‘git init’ before I could add remote add the repository to Github. I’m not sure if this is to do with the way I went about adding my project to git. It’s a pre-existing Android project, and the project name is different to that of the repository. I had to call git init just after selecting ‘Creating Git Repository’ in Android Studio.
Thanks,good work and it helped me a lot..
now i wanted to ask that how can we import complete project from GitHub into Android Studio with gradle build and repositries..
Looking forward for your reply…..Thanks
excellent
thanks for ur good work and it helped me a lot..but i am getting this error while sharing android project through different gmail account than previous one.error::”Can’t connect to repository from configured remote. You could want to check .git config.Do you want to proceed anyway?”.project is already on github
Thank you for the comment. Can you link me to the git repo? Is it public?
Thank you for sharing this, it really helped me! thanks again
but I found this error when i was ready to push
Push failed
Failed with error: fatal: The remote end hung up unexpectedly
fatal: The remote end hung up unexpectedly
error: RPC failed; curl 56 SSL read: error:00000000:lib(0):func(0):reason(0), errno 10054
Amazing tutorial, very simple and straightforward. Thank you very much!
Thanks for the tutorial and it’s really helpful. However when I did it, my git directory cannot find all the java files. My .gitignore looks fine and there’s no .git in the java file directory. Do you happen to know what may cause this? Thanks in advance.
Error path not valid
How to rectify?
Extremely helpful article, thanks!
can not run git.exe : Create process error=2, The system cannot find the file specified
This error is coming
when I click on Share on GitHub
Its really good to learn github with android studio.Thanks Man 🙂
Hi, Mark, this article was very helpful. Thanks for putting this effort.
amazing assistance from you
Thanks a lot for the post! This really helped me too. Much appreciated.
Hello Mark,
I have been using these instructions to add android studio projects to Github and worked fine. But now when I have added one project to github from one pc and then copied the files to another pc using pendrive – it was showing error when adding to github in another account(error: project already exists in git). So, i deleted the .git directory and tried to add it from the beginning. everything was successful but at the last stage when i pushed the files from android studio, it shows error: (Failed with error: fatal: unable to access ‘https://github.com/xxx/myProject.git/’: The requested URL returned error: 403). Can you give a solution?
thanks man! very easy tutorial
Thanks. It helped me also:)
Thank u so much. Simple and clear:)
Hi
Thanks for this wonderful tutorial. I have a question for you in case we want to ignore the target classes or the generated classes how can we ignore them while pushing the code.
Thanks in advance
Thanks a lot, it helped me.
Nice to see post has its pdf also .Thanks again.
THANKS BRO!
Great article – easy to follow and works great. Thanks for sharing it.
can we add same project to diffrent developer github repo at a same time???