git pull keeping local changes

后端 未结 6 1505
南旧
南旧 2020-11-30 17:14

How can I safely update (pull) a git project, keeping specific files untouched, even if there\'s upstream changes?

myrepo/config/config.php

Is there a way, o

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-30 17:31

    To answer the question : if you want to exclude certain files of a checkout, you can use sparse-checkout

    1. In .git/info/sparse-checkout, define what you want to keep. Here, we want all (*) but (note the exclamation mark) config.php :

      /* !/config.php

    2. Tell git you want to take sparse-checkout into account

      git config core.sparseCheckout true

    3. If you already have got this file locally, do what git does on a sparse checkout (tell it it must exclude this file by setting the "skip-worktree" flag on it)

      git update-index --skip-worktree config.php

    4. Enjoy a repository where your config.php file is yours - whatever changes are on the repository.


    Please note that configuration values SHOULDN'T be in source control :

    • It is a potential security breach
    • It causes problems like this one for deployment

    This means you MUST exclude them (put them in .gitignore before first commit), and create the appropriate file on each instance where you checkout your app (by copying and adapting a "template" file)

    Note that, once a file is taken in charge by git, .gitignore won't have any effect.

    Given that, once the file is under source control, you only have two choices () :

    • rebase all your history to remove the file (with git filter-branch)

    • create a commit that removes the file. It is like fighting a loosing battle, but, well, sometimes you have to live with that.

提交回复
热议问题