概述
前言
当我们提交了一些文件远程,但是又想加到忽略文件禁止下次提交
可以用git命令的如下的姿势来实现。
姿势
主要是用到了 git rm
来实现
- 带上
--cached
该命令并不会删除实际的代码文件。 - 没有
--cached
会删除实际的代码和索引,慎用!
先在.gitignore 里面增加新的忽略范围,然后再删除对应的缓存索引提交即可生效!
AME
git-rm - Remove files from the working tree and from the index
SYNOPSIS
git rm [-f | --force] [-n] [-r] [--cached] [--ignore-unmatch] [--quiet] [--] <file>...
DESCRIPTION
Remove files from the index, or from the working tree and the index. git rm will not remove a file from just your working directory. (There is no option to remove a
file only from the working tree and yet keep it in the index; use /bin/rm if you want to do that.) The files being removed have to be identical to the tip of the
branch, and no updates to their contents can be staged in the index, though that default behavior can be overridden with the -f option. When --cached is given, the
staged content has to match either the tip of the branch or the file on disk, allowing the file to be removed from just the index.
OPTIONS
<file>...
Files to remove. Fileglobs (e.g. *.c) can be given to remove all matching files. If you want Git to expand file glob characters, you may need to shell-escape
them. A leading directory name (e.g. dir to remove dir/file1 and dir/file2) can be given to remove all files in the directory, and recursively all
sub-directories, but this requires the -r option to be explicitly given.
-f, --force
Override the up-to-date check.
-n, --dry-run
Don't actually remove any file(s). Instead, just show if they exist in the index and would otherwise be removed by the command.
-r
Allow recursive removal when a leading directory name is given.
--
This option can be used to separate command-line options from the list of files, (useful when filenames might be mistaken for command-line options).
--cached
Use this option to unstage and remove paths only from the index. Working tree files, whether modified or not, will be left alone.
--ignore-unmatch
Exit with a zero status even if no files matched.
-q, --quiet
git rm normally outputs one line (in the form of an rm command) for each file removed. This option suppresses that output.
忽略某个文件进入索引
# 这个 filename 就是单个文件
git rm --cached filename
# 若是要忽略某个目录所有文件则需要递归
git rm --cached -r dirname
# 若是要强制忽略再加个-f
git rm --cached -rf dirname
重新建立整个项目的索引
若是有大量的文件且类型众多的需要忽略,
一般重建整个git的索引是比较快的。
不然就只能一点点的排除。
git rm -rf --cached .
git add .
git commit -m "refresh project git tree and index"
有不对之处请及时留言,会及时修正,谢谢阅读。
最后
以上就是忧心汉堡为你收集整理的git强制.gitignore重新生效的全部内容,希望文章能够帮你解决git强制.gitignore重新生效所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复