让 Git 对我的更改视而不见的更好方法

发布于 2024-08-21 10:07:57 字数 515 浏览 1 评论 0原文

有没有一些更简洁的方法让 Git 忽略我的一些更改并且从不提交它们? .gitattributes:

config_to_be_deviated.xml filter=qqq

.git/config:

[filter "qqq"]
 clean = "perl -ne 'print unless /git_please_dont_look_here/'"
 smudge = (Q=$(mktemp) && cat > $Q && patch -s $Q < /tmp/pp && cat $Q && rm $Q)

补丁 /tmp/pp 在每一行中添加了我的更改,其中包含“git_please_dont_look_here”。 Git 在将文件放入存储库之前删除所有此类行,并在签出文件时重新添加我的更改;我可以继续向 config_to_be_devied.xml 添加和提交有用的更改,但 Git 将看不到补丁中的更改。

Is there some cleaner way to make Git just ignore some of my changes and never commit them?
.gitattributes:

config_to_be_deviated.xml filter=qqq

.git/config:

[filter "qqq"]
 clean = "perl -ne 'print unless /git_please_dont_look_here/'"
 smudge = (Q=$(mktemp) && cat > $Q && patch -s $Q < /tmp/pp && cat $Q && rm $Q)

The patch /tmp/pp adds my changes with "git_please_dont_look_here" in each line.
Git removes all such lines before getting the file into repository and readds my changes when checking out it; I can continue adding and committing useful changes to config_to_be_deviated.xml, but changes in the patch will not be seen by Git.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

枯寂 2024-08-28 10:07:57

看起来这种“过滤”方法最适合我。

优点:

  1. 无需每次都使用自定义脚本或特殊命令。一次性设置。
  2. 历史记录或隐藏中没有额外的提交。清理差异和补丁。
  3. 犯错的几率很低。
  4. 相对容易做出微小的偏差(例如覆盖配置文件中的某些端口号),例如用于涂抹和清洁的简单 sed 脚本。

缺点:

  1. 每次 Git 读取或写入文件时都会运行过滤程序,速度可能很慢(尤其是在 Windows 中)。

It looks like this "filter" approach is the best suited for me.

Pros:

  1. No need to use custom scripts or special commands every time. One-time setup.
  2. No extra commits in history or in stash. Clean diffs and patches.
  3. Low rick of committing the wrong thing.
  4. Relatively easy to make minor deviations (like overriding some port number in config file), like simple sed script both for smudge and for clean.

Cons:

  1. Filter programs runs every time Git reads or writes the file, it can be slow (especially in Windows).
遮云壑 2024-08-28 10:07:57

将它们放入单独的文件中,忽略 git 中的文件并将其连接到您的构建以便包含它?

Put them into separate file, ignore the file in git and hook it up to your build so it gets included?

走过海棠暮 2024-08-28 10:07:57

尝试 git update-index --assume-unchanged --。对于源代码控制下的文件,它的作用类似于 gitignore。不过,该功能的最初目的是提高 git 检查包含大量文件的文件夹中的修改的性能。这是 doco:

--假设不变
--no-假设不变

当指定这些标志时,记录的对象名称
路径未更新。相反,这些
选项设置和取消设置“假设
路径的“未更改”位。当
“假设不变”位已打开,git
停止检查工作树文件
对于可能的修改,所以你
需要手动取消设置该位来告诉
git 当你改变工作树时
文件。有时这会很有帮助
正在处理一个大项目
lstat(2) 非常慢的文件系统
系统调用(例如cifs)。

此选项也可以用作粗略的文件级机制来忽略
跟踪文件中未提交的更改
(类似于 .gitignore 的作用
未跟踪的文件)。你应该记住
显式 git add 操作
仍然会导致文件
从工作树中刷新。 git
会(优雅地)失败,以防万一
需要修改索引中的这个文件
例如,在合并提交时;因此,
如果假设的未跟踪文件是
更改上游,您将需要
手动处理这种情况。

try git update-index --assume-unchanged --<path>. It acts like gitignore for files under source control. The original purpose of the feature though, was to improve performance of git checking modifications in a folder with lots of files. Here is the doco:

--assume-unchanged
--no-assume-unchanged

When these flags are specified, the object names recorded for the
paths are not updated. Instead, these
options set and unset the "assume
unchanged" bit for the paths. When the
"assume unchanged" bit is on, git
stops checking the working tree files
for possible modifications, so you
need to manually unset the bit to tell
git when you change the working tree
file. This is sometimes helpful when
working with a big project on a
filesystem that has very slow lstat(2)
system call (e.g. cifs).

This option can be also used as a coarse file-level mechanism to ignore
uncommitted changes in tracked files
(akin to what .gitignore does for
untracked files). You should remember
that an explicit git add operation
will still cause the file to be
refreshed from the working tree. Git
will fail (gracefully) in case it
needs to modify this file in the index
e.g. when merging in a commit; thus,
in case the assumed-untracked file is
changed upstream, you will need to
handle the situation manually.

软糯酥胸 2024-08-28 10:07:57
  • 将规范的默认配置放入 git 树中,但不要用它玩游戏。它在树中,如果您对其进行更改,它们就会被提交。
  • 该软件会查找默认配置,但也会在开发人员的主目录中查找配置。如果找到两者,则会将两者合并。开发人员配置文件中找到的任何配置项都会覆盖默认配置文件中的配置项。

现在,默认配置已被跟踪,而您对默认配置的自定义则不会被跟踪。

  • Put the canonical, default configuration in the git tree, but play no games with it. It's in the tree, and if you make changes to it, they'll be committed.
  • The software looks for the default configuration, but it also looks for a configuration in the developer's home directory. If it finds both, it merges the two. Any configuration items found in the developer's config file override those in the default config file.

Now the default config is tracked, and your customizations of the default config are not.

乖乖哒 2024-08-28 10:07:57

我对 git 不太熟悉。对于 Mercurial,我使用补丁队列 (MQ) 或搁置(类似于 git stash)来处理此类事情。

如果您的“可本地化”信息很容易找到(例如全部在一个配置文件中),那么 git stash 使用起来会很方便。

使用补丁队列稍微复杂一些,但一旦设置正确,效果会更好,因为您可以以透明的推送/弹出方式管理可本地化的信息。您可以在 git 此处 上找到补丁队列列表。

I am not all to familiar with git. With Mercurial I use a patch queue (MQ) or the shelve (similar to git stash) for such things.

git stash can be convenient to use, if you're "localizable" information is easy to locate (e.g. all in a single config file).

Using a patch queue is a little more complex but once setup correctly superior because you can manage localizable information in a transparent push/pop manner. You can find a list of patch queues on top of git here on SO.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文