暂时在git中隐藏一些更改的文件 [英] Temporarily hide some changed files in git

查看:102
本文介绍了暂时在git中隐藏一些更改的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我执行工作任务时,我都会在git中创建一个本地分支,然后需要修改一堆配置文件(主要包含到数据库的连接字符串)以设置为我的本地环境,然后再进行实际工作.完成这些任务后,我不想将这些更改推送到存储库中.

实际上,我更希望在查看更改的文件( git status )时将它们过滤掉.

我可以对本地分支进行第一次提交,然后在推送之前将其还原.但是我有机会忘记它.有更好的方法吗?

解决方案

最推荐的解决方案是使用:

  git update-index --skip-worktree< file> 

文档:

-[no-] skip-worktree

当指定这些标志之一时,为路径记录的对象名称不会更新.相反,这些选项设置和取消设置路径的跳过工作树"位.见章节有关更多信息,请参见下面的跳过工作树位".

跳过工作树位:

跳过工作树位

可以在一个(长)句子中定义跳过工作树"位:阅读条目时,如果将其标记为跳过工作树",则Git会假装其工作目录版本是最新的,而是读取索引版本./p>

为了详细说明,读取"是指检查文件是否存在,读取文件属性或文件内容.工作目录版本可能存在或不存在.如果存在,其内容可能与索引版本不匹配.写入不受此位影响,内容安全仍然是第一要务.请注意,如果安全的话,Git可以更新标记为skip-worktree的工作目录文件(即工作目录版本与索引版本匹配)

尽管此位看起来与假定不变的位相似,但其目标与假定不变的位不同.设置两者时,跳过工作树"也优先于假定不变的位.

-假定未更改标志用于提高性能,而-skip-worktree 则用于您确实更改文件并希望git执行的情况.还是跳过吧.

要撤消,请使用:

  git update-index --no-skip-worktree< file> 

Whenever I do a work task, I create a local branch in git, then I need to modify a bunch of configuration files (mostly containing connection strings to databases) to set to my local environment before the actual work. These changes I don't want to push to the repository later after I finish my task.

Actually I would prefer to filter them out when reviewing changed files (git status).

I could do a first commit to my local branch and revert it before pushing. But there's a chance I forget it. Is there a better way for this?

解决方案

The most recommended solution is to use:

git update-index --skip-worktree <file>

Documentation:

--[no-]skip-worktree

When one of these flags is specified, the object name recorded for the paths are not updated. Instead, these options set and unset the "skip-worktree" bit for the paths. See section "Skip-worktree bit" below for more information.

And the skip-worktree bit:

Skip-worktree bit

Skip-worktree bit can be defined in one (long) sentence: When reading an entry, if it is marked as skip-worktree, then Git pretends its working directory version is up to date and read the index version instead.

To elaborate, "reading" means checking for file existence, reading file attributes or file content. The working directory version may be present or absent. If present, its content may match against the index version or not. Writing is not affected by this bit, content safety is still first priority. Note that Git can update working directory file, that is marked skip-worktree, if it is safe to do so (i.e. working directory version matches index version)

Although this bit looks similar to assume-unchanged bit, its goal is different from assume-unchanged bit’s. Skip-worktree also takes precedence over assume-unchanged bit when both are set.

The --assume-unchanged flag is intended for performance improvement, whereas --skip-worktree is intended for cases where you do change the file and want git to skip it anyway.

To undo, use:

git update-index --no-skip-worktree <file>

这篇关于暂时在git中隐藏一些更改的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆