使用 "git commit -am" 的初始提交失败并在推送到 GitHub 时导致错误

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

我是 Git/GitHub 的新手,很难将我的新回购推送到 GitHub。

我在本地创建了我的项目,它基本上具有以下目录结构:

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

为了更好的衡量,这是我的 .gitignore(以防它是罪魁祸首):

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

我确认 Git 是由 运行 git —version 安装的,它产生:

git version 2.3.2 (Apple Git-55)

然后我创建了一个新的 GitHub 存储库(私有)并将其留空,这样我就不必合并任何东西。

然后我尝试将我的项目初​​始化为一个新的 git 存储库,这是命令行历史记录:

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'

如你所见,我尝试过:

  1. 运行 git init
  2. 添加更改并将更改推送到本地存储库,但必须首先设置我的电子邮件和用户名(同样,我是 Git 的新手)
  3. 已添加并提交
  4. 添加了新的远程仓库
  5. 尝试推送到那个远程代表

根据this answer,这个“src ref spec master does not match any”错误可能是因为我还没有实际提交任何东西,这可能与我在其上方看到的“未跟踪文件”errors/warnings 相得益彰。

无论哪种方式,我哪里出错了,我该怎么做才能解决它?

根据您的日志,您似乎没有在提交之前添加文件(待跟踪)。

使用 git commit -am 是不够的,因为它 only adds modified files(即,文件已经是您的存储库的一部分)。您正在进行初始提交,因此索引中还没有任何文件。

要添加(暂存)当前目录中的所有文件,请在提交前使用 git add .。或者,使用 git add <filename1> <filename2> ... 暂存单个文件以供提交。

然后您可以使用 git status 来验证您要添加到提交中的文件是否已准备好提交。如果它们被正确暂存(在 "Changes to be committed" 的标题下),它们应该显示为绿色。未暂存提交的文件将显示为红色,在 "Changes not staged for commit".[= 的标题下16=]

有关 git 工作流程的更多信息,请点击此处:https://git-scm.com/book/en/v2/Git-Basics-Recording-Changes-to-the-Repository

您必须使用 git add 添加未跟踪的文件。

使用命令 git add .

Git 的输出应如下所示:

On branch master

Initial commit

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    foo.txt

nothing added to commit but untracked files present (use "git add" to track)

(注意括号之间的提示,这将回答您的问题)。

某人或某物似乎已将配置变量 advice.statusHints 设置为 false。