忽略 git 提交的文件,但保护它不被清理
Ignore file from git commit but protect it from clean
在我的工作树中,我混合了 .gitignore 中的文件:
- 构建输出
- 不应包含在存储库中的敏感文件(例如密码、本地服务器的连接详细信息)
如果我想从头开始重建我的项目,我可以git clean -x
删除 (1) 中的构建输出。但这也会删除 (2) 中的敏感文件。
有没有办法标记一个文件,使其不受 git clean
的影响,同时仍被 git commit
忽略?
git-clean
的文档说 -x
:
-x
Don’t use the standard ignore rules read from .gitignore (per directory) and $GIT_DIR/info/exclude, but do still use the ignore rules given with -e options.
因此,如果您想保留 keys.pem
,您可以调用
git clean -fdxe keys.pem
您还可以为命令创建alias。
在Interactive mode中使用git clean
:
选择 filter by pattern 子命令:
This shows the files and directories to be deleted and issues an
"Input ignore patterns>>" prompt. You can input space-separated
patterns to exclude files and directories from deletion. E.g. "*.c
*.h" will excludes files end with ".c" and ".h" from deletion. When you are satisfied with the filtered result, press ENTER (empty) back
to the main menu.
拍一张look at this answer from @Borealid to the question Git - Difference Between 'assume-unchanged' and 'skip-worktree'。
看起来使用 skip-worktree
标志是解决我的问题的好方法。我通过寻找我的问题的答案来解决你的问题,我认为它们本质上是同一个问题。但是,使用skip-worktree
标志,在执行某些操作时仍然需要注意,并且文件在本地或远程发生了变化。
在了解 skip-worktree
标志之前,我能想到的最佳解决方案是结合使用 .gitignore
和 git clean
的别名。这有点 hack,但如果您唯一的选择是将排除选项与 git clean
一起使用,那么这可能会有所帮助。
- 为您正在修改但不想提交的任何文件添加一致且唯一的前缀and/or 后缀。例如,将
myusername.
添加到文件名的开头。
- 始终使用
git clean -dx --exclude=myusername.*
(为此创建一个别名)。
- 编写一个脚本,递归遍历您的文件并将以
myusername.
开头的文件复制到没有 myusername.
的文件。
现在,每当您运行清理时,desired/true文件名的文件将被清理,但是 您可以通过 运行 脚本恢复它们。此外,将具有真实文件名的文件视为只读。始终使用乱七八糟的文件名编辑文件,然后 运行 脚本。
将这些结合起来也可以减轻使用 skip-worktree
标志时的边缘情况。
在我的工作树中,我混合了 .gitignore 中的文件:
- 构建输出
- 不应包含在存储库中的敏感文件(例如密码、本地服务器的连接详细信息)
如果我想从头开始重建我的项目,我可以git clean -x
删除 (1) 中的构建输出。但这也会删除 (2) 中的敏感文件。
有没有办法标记一个文件,使其不受 git clean
的影响,同时仍被 git commit
忽略?
git-clean
的文档说 -x
:
-x
Don’t use the standard ignore rules read from .gitignore (per directory) and $GIT_DIR/info/exclude, but do still use the ignore rules given with -e options.
因此,如果您想保留 keys.pem
,您可以调用
git clean -fdxe keys.pem
您还可以为命令创建alias。
在Interactive mode中使用git clean
:
选择 filter by pattern 子命令:
This shows the files and directories to be deleted and issues an "Input ignore patterns>>" prompt. You can input space-separated patterns to exclude files and directories from deletion. E.g. "*.c *.h" will excludes files end with ".c" and ".h" from deletion. When you are satisfied with the filtered result, press ENTER (empty) back to the main menu.
拍一张look at this answer from @Borealid to the question Git - Difference Between 'assume-unchanged' and 'skip-worktree'。
看起来使用 skip-worktree
标志是解决我的问题的好方法。我通过寻找我的问题的答案来解决你的问题,我认为它们本质上是同一个问题。但是,使用skip-worktree
标志,在执行某些操作时仍然需要注意,并且文件在本地或远程发生了变化。
在了解 skip-worktree
标志之前,我能想到的最佳解决方案是结合使用 .gitignore
和 git clean
的别名。这有点 hack,但如果您唯一的选择是将排除选项与 git clean
一起使用,那么这可能会有所帮助。
- 为您正在修改但不想提交的任何文件添加一致且唯一的前缀and/or 后缀。例如,将
myusername.
添加到文件名的开头。 - 始终使用
git clean -dx --exclude=myusername.*
(为此创建一个别名)。 - 编写一个脚本,递归遍历您的文件并将以
myusername.
开头的文件复制到没有myusername.
的文件。
现在,每当您运行清理时,desired/true文件名的文件将被清理,但是 您可以通过 运行 脚本恢复它们。此外,将具有真实文件名的文件视为只读。始终使用乱七八糟的文件名编辑文件,然后 运行 脚本。
将这些结合起来也可以减轻使用 skip-worktree
标志时的边缘情况。