Search code examples
gitmacosgithubgit-pushgit-commit

Initial commit using "git commit -am" failing and causing error when pushing to GitHub


I am new to Git/GitHub and am having enormous difficulty pushing my new repo up to GitHub.

I created my project locally, which essentially has the following directory structure:

myapp/
    src/
        <A bunch of source files>
    build.grade
    gradle.properties
    README.md
    .gitignore

For good measure, here's my .gitignore (in case its the culprit):

.idea
*.iml
*.ipr
*.iws
/out
.gradle
/build
/files
/*.yaml
/*.sh

I confirmed Git was installed by running git —version which produces:

git version 2.3.2 (Apple Git-55)

I then created a new GitHub repo (private) and left it empty so I wouldn’t have to merge anything.

I then attempted to init my project as a new git repo and here’s the command-line history:

myuser@mac:~/sandbox/eclipse/workspace/myapp$ git init
Initialized empty Git repository in /Users/myuser/sandbox/eclipse/workspace/myapp/.git/
myuser@mac:~/sandbox/eclipse/workspace/myapp$ git commit -am "Initial barebones commit."

*** Please tell me who you are.

Run

    git config --global user.email "you@example.com"
    git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.

fatal: unable to auto-detect email address (got 'myuser@mac.(none)')
myuser@mac:~/sandbox/eclipse/workspace/myapp$ git config --global user.email "myuser@example.org”
myuser@mac:~/sandbox/eclipse/workspace/myapp$ git config --global user.name “mygithubusername”
myuser@mac:~/sandbox/eclipse/workspace/myapp$ git commit -am "Initial barebones commit."
On branch master

Initial commit

Untracked files:
.classpath
.gitignore
.project
.settings/
README.md
bin/
build.gradle
gradle.properties
gradle/
gradlew
gradlew.bat
src/

nothing added to commit but untracked files present
myuser@mac:~/sandbox/eclipse/workspace/myapp$ git remote add origin https://github.com/mygithubusername/myapp.git
myuser@mac:~/sandbox/eclipse/workspace/myapp$ git push -u origin master
error: src refspec master does not match any.
error: failed to push some refs to 'https://github.com/mygithbusername/myapp.git'

So as you can see I tried:

  1. Running git init
  2. Adding and pushing changes to the local repo, but had to first setup my email and username (again I’m brand new to Git here)
  3. Added & committed
  4. Added the new remote repo
  5. Tried pushing to that remote rep

According to this answer, this “src ref spec master does not match any” error may be due to the fact that I have’t actually committed anything yet, which might jive with the “untracked file” errors/warnings I see above it.

Either way, where did I go awry, and what do I do to fix it?


Solution

  • According to your log, it doesn't look like you added your files (to be tracked) before committing.

    Using git commit -am isn't enough, because it only adds modified files (that is, files that are already part of your repo). You're on your initial commit, so there aren't any files in the index yet.

    To add (stage) all the files in your current directory, use git add . before you commit. Alternately, use git add <filename1> <filename2> ... to stage individual files for commit.

    Then you can use git status to verify the files you want to add to your commit are staged to be committed. They should show up as green if they are properly staged (under a heading that says "Changes to be committed". Files that are not staged to be committed will show up as red, under a heading that says "Changes not staged for commit".

    More info on git workflow here: https://git-scm.com/book/en/v2/Git-Basics-Recording-Changes-to-the-Repository