.gitignore 不忽略目录

发布于 2025-01-10 15:04:26 字数 991 浏览 0 评论 0原文

我做了什么:

我认为 github gui 中有一些奇怪的配置导致了这个问题,并阻止我从命令行轻松使用 git 甚至 git-bash。

我最终只是卸载了 github 和 git,然后重新安装了 Windows 版 git。我现在所有的东西都可以从命令行运行(除了我从 git-bash 运行的 ssh )。比 github gui 更容易、更可靠。

感谢 mu 无 花时间尝试解决这个问题。我最终没有使用他的答案,但如果我不需要重新安装 git,这就是我需要做的。


我在本地计算机上使用 github gui。我刚刚注意到我即将进行的提交将更新我最近更新的所有节点模块。我将 .gitignore 设置为忽略整个 node_modules/ 目录。

我不知道该怎么办。我包含在 .gitignore 中的所有文件类型都被忽略。这只是它似乎忽略的目录。

这是我的 .gitignore 文件:

#################
## Sublime Text
#################

*.sublime-project
*.sublime-workspace

#################
## Images
#################

*.jpg
*.jpeg
*.png
*.gif
*.psd
*.ai

#################
## Windows detritus
#################

# Windows image file caches
Thumbs.db
ehthumbs.db

# Folder config file
Desktop.ini

# Recycle Bin used on file shares
$RECYCLE.BIN/

# Mac crap
.DS_Store

#################
## Directories
#################

dev/
cms/core/config/
node_modules/

What I did:

I think there were some weird configurations from the github gui that caused this issue and prevented me from being able to easily use git from command line or even git-bash.

I ended up just uninstalling github and git then reinstalling just git for windows. I now have everything running off the command line(except ssh which I run from git-bash). Much easier and more reliable that the github gui.

Thanks to mu 無 for taking the time to try to figure this out. I didn't end up using his answer, but if I hadn't needed to do a reinstall of git it would have been what I needed to do.


I am using the github gui on my local machine. I just noticed that a commit I was about to make was going to update all of my recently update node modules. I set up my .gitignore to ignore the entire node_modules/ directory.

I'm not sure what to do about this. All the file types I included in .gitignore were ignored. It's just the directories that it seems to ignore.

Here is my .gitignore file:

#################
## Sublime Text
#################

*.sublime-project
*.sublime-workspace

#################
## Images
#################

*.jpg
*.jpeg
*.png
*.gif
*.psd
*.ai

#################
## Windows detritus
#################

# Windows image file caches
Thumbs.db
ehthumbs.db

# Folder config file
Desktop.ini

# Recycle Bin used on file shares
$RECYCLE.BIN/

# Mac crap
.DS_Store

#################
## Directories
#################

dev/
cms/core/config/
node_modules/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

笑红尘 2025-01-17 15:04:26

由于 node_modules 目录已作为存储库的一部分进行跟踪,因此 .gitignore 规则将不适用于它。

您需要使用 git 取消跟踪目录

git rm -r --cached node_modules
git commit -m "removing node_modules"

。您可以在 git-bash 中运行上述 2 步。

此后,.gitignore 规则将忽略该目录。

请注意,一旦您拉取更改,这将从其他存储库中删除目录 node_modules。只有您进行提交的原始存储库仍会包含 node_modules 文件夹。

Since the node_modules directory is already tracked as part of the repository, the .gitignore rule will not apply to it.

You need to untrack the directory from git using

git rm -r --cached node_modules
git commit -m "removing node_modules"

You can run the above 2 in git-bash.

After this, the .gitignore rule will ignore the directory away.

Note that this will remove the directory node_modules from your other repos once you pull the changes in. Only the original repo where you made that commit will still have the node_modules folder there.

束缚m 2025-01-17 15:04:26

与 Zach 类似,我也使用了 echo "node_modules/" >> .gitignore

问题是它使用编码 UCS-2 LE BOM 创建了文件。使用 notepad++ 我将编码更改为 UTF-8,瞧 - node_modules 现在被忽略了。

输入图像描述这里

Similar to Zach, I also used echo "node_modules/" >> .gitignore.

The problem was it had created the file with encoding UCS-2 LE BOM. Using notepad++ I changed the encoding to UTF-8 and voila - node_modules is now ignored.

enter image description here

仅冇旳回忆 2025-01-17 15:04:26

如果文件已经被跟踪,.gitignore 文件将不会覆盖它。您将需要使用 git rm --cached

请参阅 git rm 的完整详细信息:
https://www.kernel.org/pub/software/scm/git/文档/git-rm.html
我自己在早期使用 git 时就遇到过一两次这样的情况,这也不太符合我的预期。

If the files are already tracked the .gitignore file will not override this. You will need to use git rm --cached <files>

See the full details on git rm at
https://www.kernel.org/pub/software/scm/git/docs/git-rm.html
I ran into this once or twice myself early on with git and it was not quite what I expected either.

相思故 2025-01-17 15:04:26

如果您使用节点项目,我喜欢这个 .gitignore

# See http://help.github.com/ignore-files/ for more about ignoring files.

# dependencies
node_modules

# testing
coverage

# production
build

# misc
.DS_Store
.env
npm-debug.log

If you work with node projects, I like this .gitignore:

# See http://help.github.com/ignore-files/ for more about ignoring files.

# dependencies
node_modules

# testing
coverage

# production
build

# misc
.DS_Store
.env
npm-debug.log
彩虹直至黑白 2025-01-17 15:04:26

我遇到了这个问题。不知何故,当我生成文件(使用 echo "node_modules" > .gitignore)时,它在文件开头插入了一个垃圾字符(我责怪 powershell)。

因此,如果您遇到此问题,请尝试删除 .gitignore 并重新开始。

I had this problem. Somehow when I generated the file (using echo "node_modules" > .gitignore) it had inserted a garbage character at the beginning of the file (I blame powershell).

So, if you run into this problem try deleting your .gitignore and starting over.

蓝戈者 2025-01-17 15:04:26

上面的解决方案都不适合我,我不知道为什么

最终我所做的是:

  1. 复制到项目文件夹以便不与主解决方案一起使用
  2. 然后我删除了 .git & 。 .gitignore 文件(CMD+SHIFT+DOT 显示隐藏文件)
  3. 在根目录添加了带有 touch .gitignore 的 .gitignore 文件,并添加了 node_modules
  4. 使用 git init< 再次初始化了 git /code>
  5. 使用 git remote add origin your_repo 再次添加远程
  6. git add .
  7. git commit -m 'addedignore file'
  8. git Push -u origin master

并继续使用这个新目录。
请注意,这对我来说是一个解决方案,因为这是我的第一次提交。

None of the solutions above worked for me and I don't know why

Eventually what I did was:

  1. Duplicated to project folder in order to don't work with the main one
  2. Then I removed .git & .gitignore file (CMD+SHIFT+DOT shows hidden files)
  3. Added .gitignore file with touch .gitignore at root and added node_modules
  4. Initialized git again with git init
  5. Added remote again with git remote add origin your_repo
  6. git add .
  7. git commit -m 'added ignore file'
  8. git push -u origin master

And continue working with this new directory.
Please note that it was a solution for me because it was my first commit.

撩起发的微风 2025-01-17 15:04:26

那么对我有用的是将“node_modules”添加到 .gitignore

backend\node_modules
frontend\your_proyect\node_modules
node_modules

Well what works for me was add "node_modules" to .gitignore

backend\node_modules
frontend\your_proyect\node_modules
node_modules
[浮城] 2025-01-17 15:04:26

只需从文件夹中删除初始 git 配置即可
rm -rf .git。使用创建的 .gitignore 文件并添加所有要添加的文件。然后使用 git init 重新初始化文件夹中的 git 配置。
这将解决您的问题。

Just remove the initial git configuration from your folder by
rm -rf .git. Use the created .gitignore file and add all the file you want to add. Then reinitialize the git configuration in your folder with git init.
This will solve your problem.

誰ツ都不明白 2025-01-17 15:04:26

尝试这个命令,它将解决您的问题:

git config core.ignorecase true

try this command, it will resolve your issue:

git config core.ignorecase true
月光色 2025-01-17 15:04:26

我还没有在任何地方看到过这篇文章,因为也许它应该是显而易见的,直到我忘记了,但在 gitignore 行末尾添加注释实际上并不是注释!自己评论就行了!

# .gitignore (this is a comment)
my_bad_dir/ # This is not a comment, and my_bad_dir/ won't be ignored
~$ man gitignore | grep comment
       •   A line starting with # serves as a comment. 

I haven't seen this posted anywhere, as perhaps it should be obvious, until I forgot, but adding a comment to the end of a gitignore line is NOT actually a comment! Comments on their own line!

# .gitignore (this is a comment)
my_bad_dir/ # This is not a comment, and my_bad_dir/ won't be ignored
~$ man gitignore | grep comment
       •   A line starting with # serves as a comment. 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文