Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Have git "assume-unchanged" a file for everyone, not just locally

Tags:

git

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.


Steps to reproduce

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
like image 204
Captain Man Avatar asked Oct 23 '15 15:10

Captain Man


People also ask

How to assume unchanged Git?

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.

Why Git is showing modified files without changes?

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.

What is Skip Worktree in Git?

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.

What does Git update index do?

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.


3 Answers

This is a bit of a hack, but it's a git-only solution that will work.

  1. Create a new repository that contains only this file and include this new repository in your main repository as a submodule.
  2. Modify .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.

like image 180
houtanb Avatar answered Oct 24 '22 18:10

houtanb


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.

like image 30
8bittree Avatar answered Oct 24 '22 18:10

8bittree


This cannot be done using only git.

like image 1
Captain Man Avatar answered Oct 24 '22 18:10

Captain Man