5

You may like to skip the preamble.


I have an npm package with the following structure:

lib/index.js
src/index.coffee

The source file is a CoffeeScript file. I have a make target for building the JavaScript file from the source file. Until recently I included changes to the JavaScript file in each commit, but I now use xyz with a prepublish script:

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. 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. I'd like to avoid accidentally committing this file, so I want to prevent it from being listed by git status.

The first thing I tried was adding /lib/ to .gitignore. This doesn't work, though, since lib/index.js is already being tracked.

I then discovered 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. So far, so good. Now though, git add lib/index.js no longer works, so the prepublish script will fail to do its job.

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. 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?

davidchambers
  • 23,918
  • 16
  • 76
  • 105

1 Answers1

3

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.

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.

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.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks, @VonC. `git add -f lib/index.js` has no effect, unfortunately. If and when I run into problems with `assume-unchanged` I'll see whether `skip-worktree` is a better option. I'm unclear as to how these two approaches differ (even having read the responses to the [relevant question](http://stackoverflow.com/q/6138076/312785)). – davidchambers May 03 '14 at 15:25