Can I 'git commit' a file and ignore its content changes?

前端 未结 4 793
抹茶落季
抹茶落季 2020-11-22 07:17

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

相关标签:
4条回答
  • 2020-11-22 07:32

    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.

    0 讨论(0)
  • 2020-11-22 07:40

    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.

    0 讨论(0)
  • 2020-11-22 07:43

    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.

    0 讨论(0)
  • 2020-11-22 07:44

    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
    0 讨论(0)
提交回复
热议问题