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?
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
beforegit add
, and could then revert the bit withgit 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With