97

I have a directory with all my coding projects.

I want to upload (correct terminology?) it to GitHub using the command line.

I have already looked at Old question.

I know how to clone an existing project, and how to push it after making any changes.

But in this case, I want to make a new project and add files to that.

How can I accomplish this using the command line?

Community
  • 1
  • 1
Lazer
  • 90,700
  • 113
  • 281
  • 364

7 Answers7

79
git init
git add .
git commit -m "Initial commit"

After this, make a new GitHub repository and follow on-screen instructions.

Veeti
  • 5,270
  • 3
  • 31
  • 37
  • So, the repository willbe created using GitHub gui only, not through command line? – Lazer May 19 '10 at 15:31
  • 8
    @eSKay: you'll first have to have a local repo, then add a remote to it, and push to that remote. All of this is done from the command line. Pushing to github has some pre-requisites, such as creating a project on github and adding ssh keys to identify yourself. – hasen May 19 '10 at 16:12
  • This didn't work. I get the following error message for the git push -u origin master : "error: failed to push some refs to 'git@github.com:xxxx/yyyy.git' To prevent you from losing history, non-fast-forward updates were rejected Merge the remote changes before pushing again. See the 'Note about fast-forwards' section of 'git push --help' for details." – chmike May 20 '12 at 13:56
  • 6
    I guess this problem results because github created a README.md in its repository. The problem was solved with the instruction 'git pull -u origin master'. This merged the github repository into my local one. Then I could upload the new version with 'git push -u origin master' – chmike May 20 '12 at 14:15
  • 16
    This answer leaves out too much information for a beginner's "How do it commit in GitHub?" question. – Andrew Koper Oct 15 '12 at 19:59
28

If you haven't already created the project in Github, do so on that site. If memory serves, they display a page that tells you exactly how to get your existing code into your new repository. At the risk of oversimplification, though, you'd follow Veeti's instructions, then:

git remote add [name to use for remote] [private URI] # associate your local repository to the remote
git push [name of remote] master # push your repository to the remote
Rob Wilkerson
  • 40,476
  • 42
  • 137
  • 192
  • 2
    I recommend to first do a `pull`, if the user created a ReadMe for their repository, otherwise they will have to do a merge. – knownasilya Dec 26 '12 at 23:26
  • 1
    Nope, just created a repo and it gives **absolutely no** instructions on what to do next – puk Dec 09 '13 at 19:55
  • @puk Not sure where you are or what you're seeing, but when I create a new repo on Github, I see a very clear set of instructions. 1 set to "Create a new repository on the command line", another to "Push an existing repository from the command line" and yet another for using the Github desktop application. – Rob Wilkerson Dec 12 '13 at 17:05
22

Just to add on to the other answers, before i knew my way around git, i was looking for some way to upload existing code to a new github (or other git) repo. Here's the brief that would save time for newbs:-

Assuming you have your NEW empty github or other git repo ready:-

cd "/your/repo/dir"
git clone https://github.com/user_AKA_you/repoName # (creates /your/repo/dir/repoName)
cp "/all/your/existing/code/*" "/your/repo/dir/repoName/"
git add -A
git commit -m "initial commit"
git push origin master

Alternatively if you have an existing local git repo

cd "/your/repo/dir/repoName"
#add your remote github or other git repo
git remote set-url origin https://github.com/user_AKA_you/your_repoName
git commit -m "new origin commit"
git push origin master
mrmoje
  • 3,714
  • 3
  • 20
  • 18
6

You can create GitHub repositories via the command line using their Repositories API (http://develop.github.com/p/repo.html)

Check Creating github repositories with command line | Do it yourself Android for example usage.

blade19899
  • 727
  • 1
  • 8
  • 32
ddewaele
  • 22,363
  • 10
  • 69
  • 82
3

It seems like Github has changed their layout since you posted this question. I just created a repository and it used to give you instructions on screen. It appears they have changed that approach.

Here is the information they used to give on repo creation:

Create A Repo · GitHub Help

blade19899
  • 727
  • 1
  • 8
  • 32
Matt
  • 381
  • 4
  • 13
1

In Linux use below command to upload code in git
1 ) git clone repository
ask for user name and password.
2) got to respositiory directory.
3) git add project name.
4) git commit -m ' messgage '.
5) git push origin master.
- user name ,password

Update new Change code into Github

->Goto Directory That your github up code
->git commit ProjectName -m 'Message'
->git push origin master.

Sanjay Bhalani
  • 2,424
  • 18
  • 44
0

From Github guide: Getting your project to Github:(using Github desktop version)

Set up your project in GitHub Desktop

The easiest way to get your project into GitHub Desktop is to drag the folder which contains your project files onto the main application screen.

If you are dragging in an existing Git repository, you can skip ahead and push your code to GitHub.com.

If the folder isn’t a Git repository yet, GitHub Desktop will prompt you to turn it into a repository. Turning your project into a Git repository won’t delete or ruin the files in your folder—it will simply create some hidden files that allow Git to do its magic.

enter image description here

In Windows it looks like this:(GitHub desktop 3.0.5.2)

enter image description here

this is not the most geeky way but it works.

WesternGun
  • 11,303
  • 6
  • 88
  • 157