442

Every developer on my team has their own local configuration. That configuration information is stored in a file called devtargets.rb which is used in our rake build tasks. I don't want developers to clobber each other's devtargets file, though.

My first thought was to put that file in the .gitignore list so that it is not committed to git.

Then I started wondering: is it possible to commit the file, but ignore changes to the file? So, I would commit a default version of the file and then when a developer changes it on their local machine, git would ignore the changes and it wouldn't show up in the list of changed files when you do a git status or git commit.

Is that possible? It would certainly be a nice feature...

3

5 Answers 5

537

Sure, I do exactly this from time to time using

git update-index --assume-unchanged [<file> ...]

To undo and start tracking again (if you forgot what files were untracked, see this question):

git update-index --no-assume-unchanged [<file> ...]

Relevant documentation:

--[no-]assume-unchanged
When this flag is specified, the object names recorded for the paths are not updated. Instead, this option sets/unsets the "assume unchanged" bit for the paths. When the "assume unchanged" bit is on, the user promises not to change the file and allows Git to assume that the working tree file matches what is recorded in the index. If you want to change the working tree file, you need to unset the bit to tell Git. This is sometimes helpful when working with a big project on a filesystem that has very slow lstat(2) system call (e.g. cifs).

Git will fail (gracefully) in case it needs to modify this file in the index e.g. when merging in a commit; thus, in case the assumed-untracked file is changed upstream, you will need to handle the situation manually.

Fail gracefully in this case means, if there are any changes upstream to that file (legitimate changes, etc.) when you do a pull, it will say:

$ git pull
…
From https://github.com/x/y
   72a914a..106a261  master     -> origin/master
Updating 72a914a..106a261
error: Your local changes to the following files would be overwritten by merge:
                filename.ext
 

and will refuse to merge.

At that point, you can overcome this by either reverting your local changes, here’s one way:

 $ git checkout filename.ext

then pull again and re-modify your local file, or could set –no-assume-unchanged and you can do normal stash and merge, etc. at that point.

14
  • 19
    does this command do its thing locally of in the .git folder? I mean, if I run this command for a config.php file, will this propagate to other users that are using the repo?
    – Magus
    Commented Feb 10, 2015 at 0:01
  • 23
    @Magus: No. This will only operate for you. Commented Feb 10, 2015 at 12:18
  • 5
    And soon later you are going to want to know how to determine is a file is assumed unchanged: stackoverflow.com/questions/2363197/… Commented Feb 20, 2015 at 15:52
  • 10
    Changes to files ignored in this way are lost when "git stash" is used . Is there a way around that?
    – Alexis
    Commented Oct 4, 2016 at 10:41
  • 6
    This is not what git update-index --assume-unchanged is for. public-inbox.org/git/…
    – jsageryd
    Commented Aug 2, 2018 at 15:30
119

If you are in control of the repository, prefer the method from this answer. If not, read on.

The preferred way to do this is to use git update-index --skip-worktree <file>, as explained in this answer:

assume-unchanged is designed for cases where it is expensive to check whether a group of files have been modified; when you set the bit, git (of course) assumes the files corresponding to that portion of the index have not been modified in the working copy. So it avoids a mess of stat calls. This bit is lost whenever the file's entry in the index changes (so, when the file is changed upstream).

skip-worktree is more than that: even where git knows that the file has been modified (or needs to be modified by a reset --hard or the like), it will pretend it has not been, using the version from the index instead. This persists until the index is discarded.

To undo this, use git update-index --no-skip-worktree <file>

Since git version 2.25.1, this is no longer the recommended way either, quoting:

Users often try to use the assume-unchanged and skip-worktree bits to tell Git to ignore changes to files that are tracked. This does not work as expected, since Git may still check working tree files against the index when performing certain operations. In general, Git does not provide a way to ignore changes to tracked files, so alternate solutions are recommended.

For example, if the file you want to change is some sort of config file, the repository can include a sample config file that can then be copied into the ignored name and modified. The repository can even include a script to treat the sample file as a template, modifying and copying it automatically.

8
  • 2
    Does this work across all users that checkout the repository? Will they get certain files but no longer be able to add changes, by mistake, unless explicitly saying so?
    – mjs
    Commented Feb 4, 2019 at 12:48
  • 3
    @momomo the flag is stored in the index so no, it is only for a single user. See erjiang's answer for something that works across all users.
    – 1615903
    Commented Feb 5, 2019 at 5:04
  • 2
    The Git documentation specifically says not use git update-index --skip-worktree for this purpose.
    – bk2204
    Commented Apr 28, 2020 at 22:57
  • 1
    @Emir this method works just as good as it always has, git has just updated the documentation to note that they do not recommend it. Other answers cover other options - this is mainly for cases where you are not in control of the remote and need some sort of workaround.
    – 1615903
    Commented Feb 18, 2022 at 7:23
  • 1
    Thanks, good to know. I think it's my mistake. I misunderstood not recommended as not working correctly. I appreciate your follow-up. In fact, this answer solved my problem.
    – Emir
    Commented Feb 18, 2022 at 9:15
53

Common practice seems to be to create a devtargets.default.rb and commit it, and then instruct each user to copy that file to devtargets.rb (which is on the .gitignore list). For example, CakePHP does the same for its database configuration file which naturally changes from machine to machine.

7
  • 6
    You can't .gitignore a file that is being tracked. .gitignore only has an effect for files that aren't in the index.
    – CB Bailey
    Commented Jul 23, 2010 at 16:30
  • 1
    i was trying to avoid doing this, though i really don't have a good reason. we're doing it now and i think it's a pain to remember that i need to create my own version without ".default" in the name. Commented Jul 23, 2010 at 17:39
  • 12
    @DerickBailey But to be fair, it's easier to remember to copy the file than to remember using the --assume-unchanged option for everyone who clones the repository.
    – Dan
    Commented Jan 15, 2016 at 13:15
  • 2
    @DerickBailey you could also set up your rake build to default to devtargets.default.rb if devtargets.rb doesn't exist. Commented Sep 30, 2016 at 20:07
  • 1
    Only practice I found satisfactory (simple, doesn't require a bunch of people to execute a bunch of git-commands in sync...). I still don't know why git wouldn't have this feature, it's clearly desired by many. Commented Jan 27, 2023 at 14:42
6

It is not possible to ignore changes to a tracked file with Git. The Git FAQ explains this:

Git doesn’t provide a way to do this. The reason is that if Git needs to overwrite this file, such as during a checkout, it doesn’t know whether the changes to the file are precious and should be kept, or whether they are irrelevant and can safely be destroyed. Therefore, it has to take the safe route and always preserve them.

It’s tempting to try to use certain features of git update-index, namely the assume-unchanged and skip-worktree bits, but these don’t work properly for this purpose and shouldn’t be used this way.

If your goal is to work with a configuration file, then the best thing to do is add an example or template file and then either have the user copy it into place or have a script create the appropriate file. You should then ignore the location of the actual configuration file and only check in the example or the template.

2

For IntelliJ IDEA users: If you want to ignore changes for a file (or files) you can move it to different Change Set.

  • Head over to Local Changes (Cmd + 9)
  • Select file(s) you want to ignore
  • F6 to move them to another Change Set

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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