git init
git add .
git commit -m "Initial commit"
git remote add origin <project url>
git push -f origin master
The -f
option on git push
forces the push. If you don't use it, you'll see an error like this:
To [email protected]:roseperrone/project.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to '[email protected]:roseperrone/project.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 merge the remote changes (e.g.,
hint: 'git pull') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
My answer is not different but I am adding more information because those that are new could benefit from filling in the gaps in information.
After you create the repo on github they have instructions. You can follow those. But here are some additional tips because I know how frustrating it is to get started with git.
Let's say that you have already started your project locally. How much you have does not matter. But let's pretend that you have a php project. Let's say that you have the index.php, contact.php and an assets folder with images, css, and fonts. You can do it this way (easy), but there are many options:
Login to your github account and create the repo.
In the following screen you can copy it down where you need it if you click the button (right side of screen) to "clone in desktop".
You can (or do it another way) then copy the contents from your existing project into your new repo. Using the github app, you can just commit from there using their GUI (that means that you just click the buttons in the application). Of course you enter your notes for the commit.
first do this to initialize git (version control).
git init
then do this to add all your files to be "monitored." If you have files that you want ignored, you need to add a .gitignore
but for the sake of simplicity, just use this example to learn.
git add .
Then you commit and add a note in between the ""
like "first commit" etc.
git commit -m "Initial Commit"
Now, here is where you add your existing repo
git remote add github <project url>
But do not literally type <project url>
, but your own project URL. How do you get that? Go to the link where your repo is on github, then copy the link. In my case, one of my repos is https://github.com/JGallardo/urbanhistorical so my resulting url for this command would just add .git after that. So here it would be
git remote add github https://github.com/JGallardo/urbanhistorical.git
Test to see that it worked by doing
git remote -v
You should see what your repo is linked to.
Then you can push your changes to github
git push github master
or
git push origin master
If you still get an error, you can force it with -f
. But if you are working in a team environment, be careful not to force or you could create more problems.
git push -f origin master
you will need to specify which branch and which remote when pushing:
➤ git init ./
➤ git add Readme.md
➤ git commit -m "Initial Commit"
➤ git remote add github <project url>
➤ git push github master
Will work as expected.
You can set this up by default by doing:
➤ git branch -u github/master master
which will allow you to do a git push
from master without specifying the remote or branch.
If you're on a Mac (and this probably works the same on a PC), here's a very easy way to do this. Strangely enough I've looked high and low for this simple process and never found it.
I know it is not recommended to use the project folder as the repo folder. I do it all the time, it always works, it makes it simple, and I never have any trouble with it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With