Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

git-index

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?

like image 417
davidchambers Avatar asked Oct 20 '22 09:10

davidchambers


1 Answers

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.

like image 68
VonC Avatar answered Oct 23 '22 06:10

VonC