233

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.
6
  • 2
    Can you make use of git hooks to have a pre-commit hook that would abort a commit if the file modified is default_values.txt (say) ?
    – sateesh
    Commented Dec 3, 2010 at 18:37
  • 2
    Git purists would say don't be lazy and use the staging area correctly, that is what it is for.
    – Xint0
    Commented Dec 3, 2010 at 18:44
  • 1
    Git purists would say use smudge/clean scripts. It's the most maintainable solution. Commented Jan 26, 2011 at 21:22
  • 1
    Xint0: true. but how to you prevent other people from accidentally checking in?
    – Alan
    Commented Jan 9, 2014 at 1:17
  • possible duplicate of Committing Machine Specific Configuration Files
    – Senseful
    Commented Sep 3, 2014 at 19:32

6 Answers 6

265

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.

10
  • 8
    If someone else pulls the repo and edits the file, are changes in their directory ignored? I'm hoping they'd have to type --no-skip-worktree to add their changes.
    – neaumusic
    Commented Dec 7, 2017 at 23:46
  • 6
    What is done with their changes is controlled by them. In other words, they would have to set skip-worktree on the file in their repo if they did not want their changes to be pushed. If it is a file that is meant to be sent out to everyone and then have all subsequent changes ignored, everyone would have to follow these same instructions.
    – moodboom
    Commented Dec 8, 2017 at 21:41
  • 3
    Note that you may have to undo the --skip-worktree status of a file before you can switch branches, if that same file is tracked in the other branch.
    – moodboom
    Commented Dec 7, 2018 at 16:22
  • 5
    hmm, it works... after I did some change in file it was not shown in git status, but when I tried to checkout to different branch, I've got error: Your local changes to the following files would be overwritten by checkout: , even -f does not help error: Entry 'wix-stores-merchant-app/demo/credentials.js' not uptodate. Cannot merge.
    – ykravv
    Commented Nov 28, 2019 at 9:02
  • 7
    I have these aliased to git ignore and git unignore. Commented Jan 9, 2020 at 20:56
56

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

6
  • 11
    this does not work. although it makes git add . ignore the file on the local branch, a clone of the archive does not have this behavior (if you change default_values.txt in the cloned archive, it will be added to the commit with "git add .")
    – Marc
    Commented Dec 3, 2010 at 22:06
  • 7
    Yes, because you are setting it just for the local repo. You cannot push this kind of information.
    – tamasd
    Commented Dec 3, 2010 at 22:08
  • 11
    @Indradhanush - this solution does not satisfy criterion 3 -- "the behavior should be passed on to any clones of the repository" -- which is why I did not accept it. Doesn't mean it's not a good answer.
    – Marc
    Commented Sep 8, 2014 at 13:07
  • I never noticed the 3rd criterion. Because I wasn't looking for it. :) Commented Sep 8, 2014 at 13:47
  • 5
    For local config files with private app settings, you likely want to use skip-worktree instead of assume-unchanged more info stackoverflow.com/questions/13630849/… Commented Dec 11, 2015 at 19:56
25

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.

8
  • hmmm... maybe I could write a hook to automatically copy default_values_template to default_values if default_values doesn't exist?
    – Marc
    Commented Dec 3, 2010 at 18:47
  • 3
    This is the most common way to solve this in my experience. It's pretty much the path of least resistance in that it "just works", and you can easily make your code check whether the local config file exists and provide a helpful error if it doesn't. Commented Dec 3, 2010 at 18:49
  • I think the solution is indeed to do something like this, preferably with a script executed whenever you pull or clone. One idea would be that anything with a particular extension (say .basefile) gets copied to a file with the extension dropped and then the file name gets added to .gitignore in that directory. So I would create a file default_values.txt.basefile and commit that. I don't have the git or perl chops to do this, but I'll ask a friend who does and let you know how it works out.
    – Marc
    Commented Dec 3, 2010 at 22:11
  • 1
    @AdamDymitruk: Yes, clean/smudge can be used in this case, but it's far from clear that that is the best option. For example, this will make it rather hard if people really want to change the file, as the clean/smudge will get in the way. I would actually prefere the approach described here.
    – sleske
    Commented Aug 31, 2013 at 8:28
  • 1
    I take a cue from git itself (specifically git hooks) and use the .sample suffix. So in your case default_values.txt.sample
    – tir38
    Commented Apr 3, 2020 at 19:36
6

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.

4
  • Are Clean and Smudge scripts local or part of the repo?
    – Alan
    Commented Jan 9, 2014 at 1:20
  • yes. :) ... that is, you can share the smudge clean via the repo but it's not a good idea when they contain sensitive data like production passwords. If that's not a concern, git requires you to explicitly enable the script. Otherwise, people could do malicious things via github and other shared repos to other users. Commented Jan 10, 2014 at 17:46
  • I need to read a bit more about this. Basically I want to setup a project that has a default user.json that needs to be overwritten with each developers creds, but I don't want the dev to accidentally check in their creds.
    – Alan
    Commented Jan 10, 2014 at 18:27
  • I would google around for clean smudge example scripts. See what comes up. Also, jump on the git irc room on freenode. You'll get help immediately. Commented Jan 10, 2014 at 19:44
1

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.

-3

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

3
  • this is a good idea, but then I also have to put the single file repository on the git server too, which is less than optimal, only because we're using github and have a limited number of repositories.
    – Marc
    Commented Dec 3, 2010 at 18:48
  • @Marc have a look at Visual Studio Team Services, unlimited free private projects and git repositories. Kanban boards, work item and bug tracking, associate checkins with work items, manage sprints if your into scrum or other project types. As well as that it has excellent build tools for building against multiple platforms, too many things to mention that are all free. Some people slag it because its Microsoft but it hands down beats what github has to offer in terms of tools apart from just repository hosting. There are limits of what you can do for free but I rarely exceed them. Commented Jan 3, 2017 at 12:15
  • @Marc one more thing that I find really handy about it is that I can set up as many accounts as I want, so if I am writing a project for a client who wants to own the source control I can create an account, use it to plan, design and execute the project and when I am finished I can pass ownership of the account to the client. Commented Jan 3, 2017 at 12:24

Not the answer you're looking for? Browse other questions tagged or ask your own question.