I have some partially generated file foo that I want to be checked in to the repository in an initial state but I don't want any changes to ever be noticed by git.
I thought putting it in the .gitignore
file and git add -f foo
would be enough, but git status
and git diff
show the changes in foo as well as git add .
adding it. Is there anyway I can do this?
Something like git update-index --assume-unchanged foo
but have it automatically for everyone, not just locally.
Hindsight edit: After dealing with similar problems to this for a while,
--skip-worktree
is preferable to--assume-unchanged
, see this question for why. The problem of magically making it apply to everyone still exists.
To be a little more specific it's some file that keeps track of an in-memory database for unit tests that needs the lines CREATE USER SA PASSWORD ""
and GRANT DBA TO SA
, if I delete this file when it gets regenerated those lines aren't there. It keeps track of the state of the database between runs which is why I don't want its changes to be checked into the repository.
I'm aware this is an odd case and that figuring out how to have it generated with those lines or not have it keep track of the state is probably a better solution.
mkdir test
cd test
git init
echo "hey" > foo
git add .
git commit -m "Add hey to foo"
echo "foo" > .gitignore
git add .
git commit -m "Ignore foo"
echo "bye" > foo
git diff # will show foo's change
git status # will show foo has changed
git add . # will add foo's change to index
In order to set "assume unchanged" bit, use --assume-unchanged option. To unset, use --no-assume-unchanged . To see which files have the "assume unchanged" bit set, use git ls-files -v (see git-ls-files[1]). The command looks at core.
These changes mean that metadata about your file changed, however the content of your file did not. If you're working in a group, this may start to intefere with pushes or just add noise to your commits.
The –skip-worktree option ignores uncommitted changes in a file that is already tracked. Regardless of any modification made in the working tree, git will always use the file content and attributes from the staging area.
Modifies the index or directory cache. Each file mentioned is updated into the index and any unmerged or needs updating state is cleared. See also git-add(1) for a more user-friendly way to do some of the most common operations on the index.
This is a bit of a hack, but it's a git-only solution that will work.
.gitmodules
in your main repository with the ignore=dirty
tag for this new submodule.So now, when this file is modified, the submodule will be marked as dirty but people using the repository won't see the submodule marked as dirty when checking git status
. What's more, they can pull and push at will without sharing the changes made to the file in the submodule.
You could create a copy of the initial file and place it elsewhere in the repository, so that the copy won't be changed by your tools. Then, in your build script or test script, it can copy it into the proper location (which should probably be ignored by git) and modify at will.
It's not a perfect git-only solution, but running git mv tests/foo test-templates/ && echo "tests/*" >> .gitignore
and adding cp test-templates/foo tests/
to your build or test script should be a pretty easy change.
This cannot be done using only git.
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