git 添加。-> 新文件仍然“没有提交”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10859632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 13:50:41  来源:igfitidea点击:

git add . -> still "nothing to commit" with new files

gitaddinit

提问by RandyMy

I am struggling with Git, I can't seem to add my files. I ran lsto show that the files are in the current directory, then ran git add .then git statuswhich showed "nothing to commit".

我正在努力使用 Git,我似乎无法添加我的文件。我运行ls以显示文件在当前目录中,然后运行git add .然后git status显示“没有任何提交”。

JJ-Computer:first_app JJ$ git init

Reinitialized existing Git repository in /Users/JJ/rails_projects/first_app/.git/

JJ-Computer:first_app JJ$ ls

Diary.txt README.rdoc config.ru log   tmp
Gemfile   Rakefile  db    public    vendor
Gemfile.lock  app   doc   script
README    config    lib   test

JJ-Computer:first_app JJ$ git add .

JJ-Computer:first_app Jenn$ git status

# On branch master
nothing to commit (working directory clean)

JJ-Computer:first_app JJ$ 

回答by quux00

Your commands look correct (I've done them many times that way).

你的命令看起来是正确的(我已经这样做了很多次)。

First try

第一次尝试

git add --all

and then try git status. I don't think that will solve it, but worth trying next.

然后尝试git status。我认为这不会解决它,但值得接下来尝试。

Next try looking at your .gitignore file, if you have one (in the top level where you did git init).

接下来尝试查看您的 .gitignore 文件,如果您有一个(在您所做的顶级git init)。

cat .gitignore

Remove any listings there that are causing your files to be ignored. For example is there an entry with just *?

删除导致您的文件被忽略的所有列表。例如,是否有一个只有*?

Next try:

下一次尝试:

git add --force

and then try git status.

然后尝试git status

If none of those work, I notice that your output from git initsays "reinitialized" rather than "initialized", so something may have gotten messed up. If you've just initialized it and don't mind losing history, start over by removing the .git dir:

如果这些都不起作用,我注意到您的输出显示git init“重新初始化”而不是“初始化”,因此某些内容可能被搞砸了。如果您刚刚初始化它并且不介意丢失历史记录,请通过删除 .git 目录重新开始:

rm -rf .git

And then reexecute your same commands above. If that doesn't work, some more information about your setup will be required. For example, you might have a global .gitignore file: ~/.gitignore_globalthat needs to edited (or removed if you don't want it).

然后重新执行上面相同的命令。如果这不起作用,则需要有关您的设置的更多信息。例如,您可能有一个全局 .gitignore 文件:~/.gitignore_global需要对其进行编辑(如果您不想要,则将其删除)。

回答by jimjoebob123

Since I've experienced a similar issue multiple times I would just like to add that you should double check that you're trying to add from and are currently in the root folder of your project.

由于我多次遇到类似的问题,我只想补充一点,您应该仔细检查您是否正在尝试添加并且当前位于项目的根文件夹中

回答by Wtower

In case somebody else bumps into this, another reason that git does not add a directory is that maybe there is a .gitdirectory within that directory.

万一其他人碰到这个,git 不添加目录的另一个原因是该目录中可能有一个.git目录。

In this case, git expects you to treat that as submodule. Otherwise, simply remove that .git(in the directory, not the root) and retry.

在这种情况下,git 希望您将其视为submodule。否则,只需将其删除.git(在目录中,而不是根目录中)并重试。

Handy command:

方便的命令:

find . -name '.git' -type d

回答by Pablo

In my case I solved it by stashing and immediately afterwards applying the stash:

在我的情况下,我通过隐藏并立即应用存储来解决它:

git stash
git stash apply

This sequence will leave the repo exactly like it was before. After this, doing git add [file]worked correctly.

这个序列将使回购完全像以前一样。在此之后,做git add [file]工作正常。

(Using git version 1.7.12.4 under suse linux)

(在 suse linux 下使用 git 版本 1.7.12.4)

回答by GarlicFries

For some reason, Git worked well for me when I had repos in directories in Google Drive File Stream for quite a while. Yesterday, I ran into the same issue. git add .would report nothing to commit, but git add <some explicit filename>worked without a problem. I moved the directory to my local hard drive (off of Google Drive File Stream), and git add .picked up all my pending changes. Note that when the directory was still on Google Drive File Stream, setting the directory to be "Available offline" had no effect.

出于某种原因,当我在 Google Drive File Stream 的目录中存储仓库很长一段时间时,Git 对我来说效果很好。昨天,我遇到了同样的问题。git add .不会报告任何要提交的内容,但git add <some explicit filename>没有问题。我将目录移到了本地硬盘驱动器(从 Google Drive File Stream 中移除),并git add .获取了所有待处理的更改。请注意,当目录仍在 Google Drive File Stream 上时,将目录设置为“离线可用”不起作用。

回答by Vincent

In my case, caused by nesting a git directory in the current git directory:

就我而言,由于在当前 git 目录中嵌套了一个 git 目录导致:

A1.java
B1.java
.git
someDirectory
  C1.java
  .git        
cd someDirectoy
rm .git -rf

回答by TracNav

Supplementing quux00's .gitignoreanswer, I found that my directory wasn't being tracked due to my build manager's (Maven's) temporary 'build' directory. Maven added 'build' to the .gitignorewhen building my program and thus my own directory (coincidentally named 'build') was then not tracked. This was solved by renaming my 'build' directory.

补充 quux00 的.gitignore答案,我发现由于我的构建管理器(Maven 的)临时“构建”目录,我的目录没有被跟踪。Maven.gitignore在构建我的程序时添加了“build” ,因此我自己的目录(巧合地命名为“build”)没有被跟踪。这是通过重命名我的“build”目录解决的。

回答by Andrey Kovalenko

I had this kind of situation just now. In my case, it was the file (lower/upper)case issue. The thing was - I had the NuGet.configfile modified, and after that, Visual Studio re-created this file for some reason, but with name NuGet.Config(pay attention, not .config, but .Config).

我刚才也有这种情况。就我而言,这是文件(小写/大写)问题。问题是 - 我NuGet.config修改了文件,之后,由于某种原因,Visual Studio 重新创建了这个文件,但使用了名称NuGet.Config(注意,不是.config,而是.Config)。

The simplest fix I found was to remove (actually, move) this file to another location, and commit without this file at all. The git statusmessage was

我发现的最简单的解决方法是删除(实际上是移动)这个文件到另一个位置,并且完全不提交这个文件。该git status消息是

[deleted] NuGet.config
[deleted] NuGet.Config

Then I copied my file back to repo directory and committed, everything worked as it should be in the very beginning.

然后我将我的文件复制回 repo 目录并提交,一切都在最开始时正常工作。

Long story short, in my case it was file naming strategy collision. In Windows, uppercase/lowercase chars in file names don't mater, but for UNIX-originated gitcommand do.

长话短说,就我而言,这是文件命名策略冲突。在 Windows 中,文件名中的大写/小写字符无关紧要,但对于源自 UNIX 的git命令却如此。

回答by Alta

I had a similar issue adding a new file to an existing initialized git folder. git add <filename>didn't work. What worked for me was git add .I use bash 5 and git 2.25 for mac. I hope this will help someone else, too...cheers.

我在将新文件添加到现有的初始化 git 文件夹时遇到了类似的问题。git add <filename>没有用。对我git add .有用的是我使用 bash 5 和 git 2.25 for mac。我希望这也能帮助其他人......干杯。