Saturday, February 4, 2017
By: Matthew Doucette
I was having troubles pushing a local project to a new GitHub repository. I could set up a repo on GitHub no problem, however I could not sync it with a local repo. Check out the following two methods:
Ideal Method (Which Failed for Me):
Here is what failed for me:
- Create repo on GitHub, then come back to local Git Shell:
- git init
- git add *.*
- git commit -m "Initial commit"
- git remote add origin https://github.com/<user>/repo
- git push origin master
The "git push" would fail with the following error:
~<path> [master +1076 ~0 -0 !]> git push origin master
To https://github.com/<user>/repo
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/<user>/repo'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
I would try to do a "git pull" to fix it, but it would fail with an error I did not record. Contact me if you run into this same issue.
Alternative Method:
Here is an alternate method, proposed by friend Wayne Muise of MudMonger Games. Three steps:
STEP 1:
Prepare empty folder:
- Move your local project to another folder.
- Create an emtpy folder where you want your project to go.
STEP 2:
Create repo on GitHub:
-
- Visit https://github.com/.
- Login; I log in via my "Xona Games" company account -- so it owns the repo -- and access it via my personal "Xonatron" account.
- Click "New repository"; You will see it above a list of your existing repositories, if you have some.
- Name the repository; Keep in mind that spaces become dashes; It will show you; Example, Infinite Ammo became "Infinite-Ammo"; I am not sure if it causes issues, however I recommend not ending in periods, as Windows cannot create folders that end in periods. In the case of The Exploration Thereof..., it became "The-Exploration-Thereof..." with the periods intact, however I decided against it and removed the periods.
- Leave "Description" blank; You can edit this afterwards.
- Choose "Private", unless you wish to publicly share your repository; The "Private" option may be reserved only to paid subscriptions to GitHub, as we have.
- Enable "Initialize this repository with a README"; It will create a "README.md" file; If you do not choose this, you can likely create one later.
- If you are using Unity or another supported engine/platform/codebase:
-
- Click "Add .gitignore: None" dropdown box.
- Scroll down and click on "Unity".
- "Add .gitignore: Unity" should appear.
- Click "Create repository".
- "Creating repository..." should appear.
- You should be brought to your new repository; In the case of The Exploration Thereof..., it brings me to: https://github.com/XonaGames/The-Exploration-Thereof, which should be private; You should see your "README.md" populated with your project title.
- Click "Clone or download" dropdown box.
- Copy the web URL; In the case of The Exploration Thereof..., it is "https://github.com/XonaGames/The-Exploration-Thereof.git"; There is a convenient icon that copies it automatically.
- Invite your personal account -- if you have one -- as a contributor.
STEP 3:
Add local project to repository via local Git Shell:
- Load Git Shell.
- Note: In GitHub Desktop, make sure you are logged in as the same user (my "Xona Games" company account for me, not my personal "Xonatron" account); Under options, I have it set so "Xona Games" is the main account, yet "Xonatron" is the account used for commits.
- Change directory to folder in question (the empty folder you wish your project to exist in).
- Note: The idea here is to move your project into the folder after you download the GitHub repo into the empty folder.
- git clone https://github.com/<user>/repo .
- Note: This downloads the "origin" branch on GitHub to your local "master" branch on your computer, into your empty folder. You should see your readme file and .gitignore locally if you have chosen to create them. You will also get a .git folder. Note the dot, ".". Without the dot, it creates a folder. With the dot, it downloads to the current folder. If you make a mistake, you can delete the local mistake (delete the files via Windows Explorer) and do it again.
- Note: Now the folder is no longer empty. It created it's own folder, which has git files in it, including a ".git" folder, ".gitignore" file, and a "README.md" file.
- Test: Run "dir" and you should see "[master ≡]" appear. This is good!
- Copy local project into this (now not emtpy) folder where you want your project to go.
- Note: Git will think you just made all these project files and treat them as new.
- git add *.*
- git commit -m "Initial commit"
- git push origin master
- Note: You could also use "git push -u origin master" and, with using "-u", never have to specific "origin master" again.
- Done!
- Test: Go to https://github.com/ and go to your commits; In the case of The Exploration Thereof..., it is https://github.com/XonaGames/The-Exploration-Thereof/commits/master; You should see an "Initial commit" from your company account and a second commit that you just pushed; Plus, all your files should be uploaded to your git repository. Check and see!
DONE.
If you really wish to be certain, download your repo to a new folder and see if your project can be built again. This way, you know you have all you need on Git.
That is all!
About the Author: I am Matthew Doucette of Xona Games, an award-winning indie game studio that I founded with my twin brother. We make intensified arcade-style retro games. Our business, our games, our technology, and we as competitive gamers have won prestigious awards and received worldwide press. Our business has won $190,000 in contests. Our games have ranked from #1 in Canada to #1 in Japan, have become #1 best sellers in multiple countries, have won game contests, and have held 3 of the top 5 rated spots in Japan of all Xbox LIVE indie games. Our game engines have been awarded for technical excellence. And we, the developers, have placed #1 in competitive gaming competitions -- relating to the games we make. Read about our story, our awards, our games, and view our blog.