笔者一般在linux上做版本控制。
近期,在windows上使用,发现:
即使没提交到staging area,.gitignore仍然没有起作用。
(base) PS E:\idm下载\test_git> ls
(base) PS E:\idm下载\test_git> echo 1 > 1.sh
(base) PS E:\idm下载\test_git> echo 2 > 2.sh
(base) PS E:\idm下载\test_git> echo 1.sh > .gitignore
(base) PS E:\idm下载\test_git> git status
On branch main
Your branch is ahead of 'origin/main' by 1 commit.
(use "git push" to publish your local commits)
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
deleted: .gitignore
Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore
1.sh
2.sh
Make sure your
.gitignorefile usesANSIorUTF-8encoding. If it uses something else likeUnicode BOM, it's possible that Git can't read the file.
以上是Stack Overflow上的一则评论,说明是编码问题。

确实如此。
用vscode修改一下编码,改为utf-8。

在Windows环境下,用户发现即使未将文件提交到暂存区,.gitignore也无法正常工作。问题源于文件编码,.gitignore可能使用了非UTF-8的编码,如Unicode BOM,导致Git无法识别。通过VSCode将文件编码更改为UTF-8后,问题得到解决。
1654

被折叠的 条评论
为什么被折叠?



