Git 不再跟踪及提交某个文件

查看文件修改状态

1
2
3
4
5
6
7
8
git status

On branch feature/branch_1
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: dir/file1.txt
modified: dir/file2.txt

不再跟踪指定文件

1
git update-index --assume-unchanged dir/file1.txt

查看文件修改状态

1
2
3
4
5
6
git status

On branch feature/branch_1
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: dir/file2.txt

查看本地不再跟踪的文件列表

1
2
3
git ls-files -v | grep '^h\ '

h dir/file1.txt

恢复已忽略文件的追踪

1
2
3
4
5
6
7
8
9
10
11
12
# 恢复指定文件
git update-index --no-assume-unchanged dir/file1.txt
# 全部恢复
git ls-files -v | grep '^h\ ' | awk '{print $2}' | xargs git update-index --no-assume-unchanged

git status

On branch feature/branch_1
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: dir/file1.txt
modified: dir/file2.txt