Git 推送看不到我的所有更改

Git push not seeing all my changes

我创建了一个私人 GitHub 存储库,比如 https://github.com/myuser/myprivrepo,并选择让它为我生成 .gitignoreREADME.md。然后我在本地克隆了这个 repo 并添加了一堆文件。我还修改了 .gitignore 以及 GitHub 为我生成的 README.md

然后我这样做了:

git commit -a -m "Initial commit with basic project structure."

然后是这个:

git push

它要求我进行身份验证,我做了,"push" 似乎是成功的。所以我回到我的 GitHub 存储库,刷新页面后,我仍然只看到我的两个 GitHub 生成的文件(.gitignoreREADME.md),但是它们有已根据我的更改进行了更新,甚至在它们旁边显示了正确更新的时间戳和提交消息(“具有基本项目结构的初始提交。”)。

所以这就像推送尊重了两个 GitHub 生成的文件被更改,但忽略了我的所有其他更改。这是我的 .gitignore:

*.class
.mtj.tmp/
*.jar
*.war
*.ear
hs_err_pid*
.metadata
.gradle/
bin/
tmp/
*.tmp
*.bak
*.swp
*~.nib
local.properties
.settings/
.loadpath
.project
.externalToolBuilders/
*.launch
.cproject
.classpath
.buildpath
.target
.texlipse

我添加但 GitHub 上不存在的文件示例是 build.gradle,它不应与忽略文件中的任何内容匹配。想法?

注意:当我右键单击本地存储库根目录并转到 Git Gui 时,我看到 GitHub 上丢失的所有文件在本地显示为 "Unstaged"。不确定这是否意味着什么。

git commit -a 不会添加以前未跟踪的文件。您可以使用 git add . 在提交之前添加所有内容。 -a 就不再需要了。

Quoting the commit manual(强调我的):

-a
--all

Tell the command to automatically stage files that have been modified and deleted, but new files you have not told Git about are not affected.

看来GitHub还可以。当您执行 git commit -a 时,git 已经跟踪的所有文件都将被提交,在您的情况下,这只是 GitHub 生成的文件。除非您 git add <file>git commit <file>,否则不会提交新文件。要检查 git 正在跟踪哪些文件,请使用 git ls-tree -r master --name-only。在提交之前执行 git status 也可能有用:

$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        ### Files which will be committed with git commit -a appear here

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

        ### Files which will NOT be committed with git commit -a appear here

no changes added to commit (use "git add" and/or "git commit -a")

要解决您当前的问题,您可以这样做:

$ # uncommit last commit
$ git reset HEAD~1 --soft
$ # add missing file
$ git add build.gradle
$ # recommit
$ git commit -a -m "Initial commit with basic project structure."
$ # force the push, as git will complain about overwriting a commit
$ git push --force