Marc
Marc

Reputation: 5625

How can I make git ignore future revisions to a file?

I have created a default version of a file included in a git repository. It's important that when someone clones the repository, they get a copy of this file. However, I would like to set git so that it ignores changes to this file later. .gitignore works only on untracked files.

My motivation is that this file contains machine-specific information. I would like to provide default values, while allowing people to make local changes that won't get pushed back to the origin repository, creating merge conflicts when we pull new changes.

We are generally pretty lazy and use git add . a lot, so I'm pretty sure if I can't tell git to ignore this file, changes to it will end up getting committed and pushed.

To summarize,

  1. I would like to create a file, call it default_values.txt that is added to my git repository and is included when someone clones that repository.
  2. git add . should not add default_values.txt to the commit.
  3. This behavior should be passed on to any clones of the repository.

Upvotes: 242

Views: 45477

Answers (6)

moodboom
moodboom

Reputation: 6922

As many others have mentioned, a good modern solution is:

git update-index --skip-worktree default_values.txt

That will ignore changes to that file, both local and upstream, until you decide to allow them again with:

git update-index --no-skip-worktree default_values.txt

You can get a list of files that are marked skipped with:

git ls-files -v . | grep ^S

Note that unlike --skip-worktree, the --assume-unchanged status will get lost once an upstream change is pulled.

Upvotes: 276

fruitcoder
fruitcoder

Reputation: 1228

I found a solution that works for my team. We share our githooks via symlinks and after adding a template file to git I added a pre-commit hook that checks whether the template file has been modified and if so I git reset -- templatefile.txt. If it's the only changed file I also abort the commit.

Upvotes: 1

Adam Dymitruk
Adam Dymitruk

Reputation: 129762

Take a look at smudge/clean scripting. This way you can version control the file but when it is checked out, you will "smudge" it by replacing the generic/place-holder data with machine specific data in the file.

When you commit it, you will "clean" it by replacing the machine specific information with generic or place-holder information.

Smudge/clean scripts must be deterministic in that applying them multiple times, in different orders will be the equivalent of just running the last one in the sequence.

The same can be applied with passwords if you need to expose your repository but the contents may contain sensitive information.

Upvotes: 6

tamasd
tamasd

Reputation: 5923

What you are searching for is git update-index --assume-unchanged default_values.txt.

See the docs for more details: http://www.kernel.org/pub/software/scm/git/docs/git-update-index.html

Upvotes: 57

ivanpro
ivanpro

Reputation: 13

I suggest looking into submodules. If you place the machine specific files in a submodule, git add should ignore it.

Upvotes: -3

Laurence Gonsalves
Laurence Gonsalves

Reputation: 143314

The approach I've generally seen is to create a file with a different name, eg: default_values_template.txt and put default_values.txt in your .gitignore. Instruct people to copy default_values_template.txt to default_values.txt in their local workspaces and make changes as needed.

Upvotes: 27

Related Questions