简体   繁体   English

忽略对跟踪文件的更改,而不必“git add”文件

[英]Ignore changes to a tracked file without making it impossible to `git add` the file

You may like to skip the preamble. 您可能想跳过序言。


I have an npm package with the following structure: 我有一个npm包,具有以下结构:

lib/index.js
src/index.coffee

The source file is a CoffeeScript file. 源文件是CoffeeScript文件。 I have a make target for building the JavaScript file from the source file. 我有一个make目标,用于从源文件构建JavaScript文件。 Until recently I included changes to the JavaScript file in each commit, but I now use xyz with a prepublish script: 直到最近,我在每次提交中都包含了对JavaScript文件的更改,但现在我将xyz与preublish脚本一起使用:

XYZ = node_modules/.bin/xyz --script scripts/prepublish

.PHONY: release-major release-minor release-patch
release-major: LEVEL = major
release-minor: LEVEL = minor
release-patch: LEVEL = patch

release-major release-minor release-patch
    @$(XYZ) --increment $(LEVEL)

The prepublish script is straightforward: 预发布脚本很简单:

#!/usr/bin/env bash
set -e

rm -f   lib/index.js
make    lib/index.js
git add lib/index.js

This means every time I publish a new release (by running make release-patch , for example), lib/index.js will be rebuilt and added to the release commit. 这意味着每次我发布新版本时(例如,通过运行make release-patch ), lib / index.js将被重建并添加到发布提交中。 Everything works fine until this point. 到目前为止,一切正常。


Since the release script automatically updates the compiled file, I no longer want to update it manually each time I commit. 由于发布脚本会自动更新已编译的文件,因此我不再希望每次提交时都手动更新它。 The problem is that after running make test , git status lists lib/index.js as a modified tracked file. 问题是在运行make testgit statuslib / index.js列为修改后的跟踪文件。 I'd like to avoid accidentally committing this file, so I want to prevent it from being listed by git status . 我想避免意外提交此文件,因此我想阻止它被git status列出。

The first thing I tried was adding /lib/ to .gitignore . 我尝试的第一件事是添加/lib/.gitignore This doesn't work, though, since lib/index.js is already being tracked. 但这不起作用,因为已经跟踪了lib / index.js

I then discovered git update-index --assume-unchanged . 然后我发现了git update-index --assume-unchanged I ran this command with lib/index.js as an argument, and the file is no longer listed by git status . 我使用lib/index.js作为参数运行此命令,并且git status不再列出该文件。 So far, so good. 到现在为止还挺好。 Now though, git add lib/index.js no longer works, so the prepublish script will fail to do its job. 现在, git add lib/index.js不再起作用,因此预发布脚本将无法完成其工作。

I believe the prepublish script could run git update-index --no-assume-unchanged lib/index.js before git add , and could then revert the bit with git update-index --assume-unchanged lib/index.js . 我相信preublish脚本可以在git add之前运行git update-index --no-assume-unchanged lib/index.js ,然后可以使用git update-index --assume-unchanged lib/index.js恢复该位。 Is this really the best approach? 这真的是最好的方法吗?

To summarize, how does one ignore changes to a tracked file while retaining the ability to git add the file? 总结一下,如何在保留git add文件的能力的同时忽略对被跟踪文件的更改?

Check simply if a git add -f ( git add --force ) would be able to add the file without the need to unset the assume-unchanged bit. 只需检查git add -fgit add --force )是否能够添加文件而无需取消设置assume-unchanged位。

If that doesn't work, then: 如果这不起作用,那么:

I believe the prepublish script could run git update-index --no-assume-unchanged lib/index.js before git add , and could then revert the bit with git update-index --assume-unchanged lib/index.js . 我相信preublish脚本可以在git add之前运行git update-index --no-assume-unchanged lib/index.js ,然后可以使用git update-index --assume-unchanged lib/index.js恢复该位。

With this approach, it is the best you can do. 通过这种方法,您可以做到最好。

The only other approach is git update-index --(no-)skip-worktree lib/index.js (that I present here ), but in your case, that would be the same. 唯一的另一种方法是git update-index --(no-)skip-worktree lib/index.js我在这里提到),但在你的情况下,这将是相同的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM